Sort

  1 //============================================================================
  2 // Name        : Qsort.cpp
  3 // Author      : 
  4 // Version     :
  5 // Copyright   : Your copyright notice
  6 // Description : Hello World in C++, Ansi-style
  7 //============================================================================
  8 
  9 #include <ctime>
 10 #include <cstdlib>
 11 #include <iostream>
 12 using namespace std;
 13 
 14 void Bubble(int s[], int len)
 15 {
 16     bool flag = true;
 17     for(int i = len; i > 0 && flag; i--)
 18     {
 19         flag = false;
 20         for(int j = 0; j < i; j++)
 21         {
 22             if(s[j] > s[j+1])
 23             {
 24                 int temp = s[j];
 25                 s[j] = s[j+1];
 26                 s[j+1] = temp;
 27                 flag = true;
 28             }
 29         }
 30     }
 31 }
 32 
 33 void Chose(int s[], int len)
 34 {
 35     for(int i = 0; i < len; i++)
 36     {
 37         int min = i;
 38         for(int j = i + 1; j < len; j++)
 39             if(s[min] > s[j])
 40                 min = j;
 41         int temp = s[min];
 42         s[min] = s[i];
 43         s[i] = temp;
 44     }
 45 }
 46 
 47 void Insert(int s[], int len)
 48 {
 49     for(int i = 1; i < len; i++)
 50     {
 51         if(s[i-1] > s[i])
 52         {
 53             int j, temp = s[i];
 54             for(j = i -1; j >= 0 && s[j] > temp; j--)
 55             {
 56                 s[j+1] = s[j];
 57             }
 58             s[j+1] = temp;
 59         }
 60     }
 61 }
 62 
 63 void Shell(int s[], int len)
 64 {
 65     int step = len/2;
 66     while(step >= 1)
 67     {
 68         for(int i = step; i < len; i ++)
 69         {
 70             if(s[i-step] > s[i])
 71             {
 72                 int j, temp = s[i];
 73                 for(j = i - step; j >= 0 && s[j] > temp; j = j - step)
 74                 {
 75                     s[j+step] = s[j];
 76                 }
 77                 s[j+step] = temp;
 78             }
 79         }
 80         step = step/2;
 81     }
 82 }
 83 
 84 void HeapAdj(int s[], int i, int len)
 85 {
 86     int child;
 87     for(; 2*i+1 < len; i = child)
 88     {
 89         child = 2*i+1;
 90         if(child+1 < len && s[child] < s[child+1])
 91             child++;
 92         if(s[i] < s[child])
 93         {
 94             int temp = s[i];
 95             s[i] = s[child];
 96             s[child] = temp;
 97         }
 98     }
 99 }
100 
101 void Heap(int s[], int len)
102 {
103     for(int i = len/2 - 1; i >= 0; i--)
104         HeapAdj(s, i, len);
105     for(int i = len - 1; i > 0; i--)
106     {
107         int temp = s[0];
108         s[0] = s[i];
109         s[i] = temp;
110         HeapAdj(s, 0, i);
111     }
112 }
113 
114 void MergeFun(int s[],int t[], int m, int n)
115 {
116     if(m == n)
117         return;
118     int mid = (n+m)/2;
119     MergeFun(s, t, m, mid);
120     MergeFun(s, t, mid+1, n);
121     int k = m, a = m, b = mid+1;
122     while(a <= mid && b <= n)
123     {
124         while(s[a] <= s[b] && a <= mid)
125             t[k++] = s[a++];
126         while(s[b] <= s[a] && b <= n)
127             t[k++] = s[b++];
128     }
129     if(b == n+1)
130     {
131         while(k <= n)
132             t[k++] = s[a++];
133         for(k = m; k <= n; k++)
134             s[k] = t[k];
135     }
136     else
137     {
138         for(int i = m; i < k; i++)
139             s[i] = t[i];
140     }
141 }
142 
143 void Merge(int s[], int len)
144 {
145     int *t = new int[len];
146     MergeFun(s, t, 0, len-1);
147 }
148 
149 void QuitFun(int s[], int low, int high)
150 {
151     if(low >= high)
152         return;
153 
154     int p = s[low];
155     int l = low, h = high;
156     while(l < h)
157     {
158         while(s[h] >= p && l < h)
159             h--;
160         int temp = s[h];
161         s[h] = s[l];
162         s[l] = temp;
163         while(p >= s[l] && l < h)
164             l++;
165         temp = s[h];
166         s[h] = s[l];
167         s[l] = temp;
168     }
169     QuitFun(s, low, l);
170     QuitFun(s, l+1, high);
171 }
172 
173 void Quit(int s[], int len)
174 {
175     QuitFun(s, 0, len-1);
176 }
177 
178 clock_t start, stop;
179 
180 #define LEN 30000
181 int num[LEN];
182 int main() {
183     srand(time(NULL));
184     for(int i = 0; i < LEN; i++)
185     {
186         //num[i] = rand()%LEN;
187         num[i] = LEN - i;
188     }
189 
190     start = clock();
191     Merge(num, LEN);
192     stop = clock();
193     cout<<"time : "<< stop - start << endl;
194 
195     for(int i = 0; i < LEN - 1; i++)
196     {
197         if(num[i] > num[i+1])
198             cout<<"error"<<endl;
199     }
200     cout<<"done"<<endl;
201     return 0;
202 }

 

你可能感兴趣的:(Sort)