码迷,mamicode.com
首页 > 编程语言 > 详细

冒泡法、选择法、快速、归并、堆排序算法实现

时间:2016-01-18 17:34:24      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

  1 #include <iostream>
  2 #include <string.h>
  3 
  4 using namespace std;
  5 
  6 #define ARRAY_SIZE 10
  7 
  8 int g_bubble_sort[ARRAY_SIZE] = {3, 1, 5, 9, 12, 7, 8, 13, 4, 2};
  9 int g_selection_sort[ARRAY_SIZE] = {3, 1, 5, 9, 12, 7, 8, 13, 4, 2};
 10 int g_quick_sort[ARRAY_SIZE] = {3, 1, 5, 9, 12, 7, 8, 13, 4, 2};
 11 int g_merge_sort[ARRAY_SIZE] = {3, 1, 5, 9, 12, 7, 8, 13, 4, 2};
 12 int g_heap_sort[ARRAY_SIZE] = {3, 1, 5, 9, 12, 7, 8, 13, 4, 2};
 13 int g_binary_search[ARRAY_SIZE] = {1, 2, 3, 4, 5, 7, 8, 9, 12, 13};
 14 
 15 int binary_search(int arr[], int startIdx, int endIdx, int ele);
 16 void bubble_sort(int arr[], int startIdx, int endIdx);
 17 void selection_sort(int arr[], int startIdx, int endIdx);
 18 int partion(int arr[], int startIdx, int endIdx);
 19 int partion2(int arr[], int startIdx, int endIdx);
 20 void quick_sort(int arr[], int startIdx, int endIdx);
 21 void merge_sort(int arr[], int startIdx, int endIdx);
 22 void merge(int arr[], int startIdx, int mid, int endIdx);
 23 void heap_sort(int arr[], int startIdx, int endIdx);
 24 void build_max_heap(int arr[], int len);
 25 void max_heapify(int arr[], int len, int root);
 26 
 27 
 28 int main()
 29 {
 30     cout << "bubble_sort: \t";
 31     bubble_sort(g_bubble_sort, 0, ARRAY_SIZE - 1);
 32     for(int i=0; i<ARRAY_SIZE; i++)
 33     {
 34         cout<<g_bubble_sort[i] << "\t";
 35     }
 36     cout << "\n";
 37 
 38     cout << "selction_sort: \t";
 39     selection_sort(g_selection_sort, 0, ARRAY_SIZE - 1);
 40     for(int i=0; i<ARRAY_SIZE; i++)
 41     {
 42         cout<<g_selection_sort[i] << "\t";
 43     }
 44     cout << "\n";
 45 
 46     cout << "quick_sort: \t";
 47     quick_sort(g_quick_sort, 0, ARRAY_SIZE - 1);
 48     for(int i=0; i<ARRAY_SIZE; i++)
 49     {
 50         cout<<g_quick_sort[i] << "\t";
 51     }
 52     cout << "\n";
 53 
 54     cout << "merge_sort: \t";
 55     merge_sort(g_merge_sort, 0, ARRAY_SIZE - 1);
 56     for(int i=0; i<ARRAY_SIZE; i++)
 57     {
 58         cout<<g_merge_sort[i] << "\t";
 59     }
 60     cout << "\n";
 61 
 62     cout << "heap_sort: \t";
 63     heap_sort(g_heap_sort, 0, ARRAY_SIZE - 1);
 64     for(int i=0; i<ARRAY_SIZE; i++)
 65     {
 66         cout<<g_heap_sort[i] << "\t";
 67     }
 68 
 69     cout << "\n";
 70     cout << binary_search(g_bubble_sort, 0, ARRAY_SIZE - 1, 8) << endl;
 71     cout << binary_search(g_selection_sort, 0, ARRAY_SIZE - 1, 8) << endl;
 72     cout << binary_search(g_quick_sort, 0, ARRAY_SIZE - 1, 8) << endl;
 73     cout << binary_search(g_merge_sort, 0, ARRAY_SIZE - 1, 8) << endl;
 74     cout << binary_search(g_heap_sort, 0, ARRAY_SIZE - 1, 8) << endl;
 75     return 0;
 76 }
 77 
 78 
 79 void bubble_sort(int arr[], int startIdx, int endIdx)
 80 {
 81     int len = endIdx - startIdx + 1;
 82     int i, j;
 83     int tmp;
 84     for(i=1; i<len; ++i)
 85     {
 86         for(j=startIdx; j<startIdx + len - i; ++j)
 87         {
 88             if(arr[j] > arr[j+1])
 89             {
 90                 tmp = arr[j];
 91                 arr[j] = arr[j+1];
 92                 arr[j+1] = tmp;
 93             }
 94         }
 95     }
 96 }
 97 
 98 void selection_sort(int arr[], int startIdx, int endIdx)
 99 {
100     int len = endIdx - startIdx + 1;
101     int i, j;
102     int max, idx, tmp;
103 
104     for(i=1; i<len; ++i)
105     {
106         idx = startIdx;
107         max = arr[idx];
108         for (j = startIdx+1; j <= startIdx + len - i; ++j)
109         {
110               if (arr[j] >= max)
111               {
112                   max = arr[j];
113                  idx = j;
114              }
115          }
116          tmp = arr[startIdx + len - i];
117          arr[startIdx + len - i] = max;
118          arr[idx] = tmp;
119     }
120 }
121 
122 int binary_search(int arr[], int startIdx, int endIdx, int ele)
123 {
124     if(startIdx > endIdx)
125     {
126         return -1;
127     }
128     int midIdx;
129     while(startIdx <= endIdx)
130     {
131         midIdx = (startIdx + endIdx) / 2;
132         if(arr[midIdx] == ele)
133         {
134             return midIdx;
135         }
136         else if(arr[midIdx] < ele)
137         {
138             startIdx = midIdx + 1;
139         }
140         else
141         {
142             endIdx = midIdx - 1;
143         }
144     }
145     return -1;
146 }
147 
148 void quick_sort(int arr[], int startIdx, int endIdx)
149 {
150     if(startIdx >= endIdx)
151     {
152         return;
153     }
154     int pivot = partion2(arr, startIdx, endIdx);
155     quick_sort(arr, startIdx, pivot - 1);
156     quick_sort(arr, pivot + 1, endIdx);
157 }
158 
159 
160 int partion(int arr[], int startIdx, int endIdx)
161 {
162     int i = startIdx;
163     int j = endIdx - 1;
164     int tmp;
165     while(i<=j)
166     {
167         if(arr[i] < arr[endIdx])
168         {
169             ++i;
170             continue;
171         }
172         if(arr[j] >= arr[endIdx])
173         {
174             --j;
175             continue;
176         }
177         tmp = arr[i];
178         arr[i] = arr[j];
179         arr[j] = tmp;
180         ++i;
181         --j;
182     }
183 
184     tmp = arr[endIdx];
185     arr[endIdx] = arr[i];
186     arr[i] = tmp;
187     return i;
188 }
189 
190 
191 int partion2(int arr[], int startIdx, int endIdx)
192 {
193     int i = startIdx + 1;
194     int j = endIdx;
195     int tmp;
196     int pivot;
197 
198     while(true)
199     {
200         while(i<=endIdx && arr[i] <= arr[startIdx])
201         {
202             ++i;
203         }
204         while(j>=startIdx+1 && arr[j] > arr[startIdx])
205         {
206             --j;
207         }
208         if(i<j)
209         {
210             tmp = arr[i];
211             arr[i] = arr[j];
212             arr[j] = tmp;
213             ++i;
214             --j;
215         }
216         else
217         {
218             tmp = arr[startIdx];
219             arr[startIdx] = arr[j];
220             arr[j] = tmp;
221             pivot = j;
222             break;
223         }
224     }
225     return pivot;
226 }
227 
228 void merge_sort(int arr[], int startIdx, int endIdx)
229 {
230     if(startIdx == endIdx)
231     {
232         return;
233     }
234     int mid = (startIdx + endIdx)/2;
235     merge_sort(arr, startIdx, mid);
236     merge_sort(arr, mid + 1, endIdx);
237     merge(arr, startIdx, mid, endIdx);
238 }
239 void merge(int arr[], int startIdx, int mid, int endIdx)
240 {
241     int len = endIdx - startIdx + 1;
242     int *arrTmp = new int[len];
243     memset(arrTmp, 0, len*sizeof(int));
244     int i = startIdx;
245     int j = mid + 1;
246     int k;
247     for(k=0; k<len; ++k)
248     {
249         if(j > endIdx || (i <= mid && arr[i] <= arr[j]))
250         {
251             arrTmp[k] = arr[i];
252             ++i;
253         }
254         else
255         {
256             arrTmp[k] = arr[j];
257             ++j;
258         }
259     }
260 
261     for(k=0; k<len; ++k)
262     {
263         arr[startIdx + k] = arrTmp[k];
264     }
265 
266     delete[] arrTmp;
267 }
268 
269 
270 void heap_sort(int arr[], int startIdx, int endIdx)
271 {
272     int *arrRef = &arr[startIdx];
273     int len = endIdx - startIdx + 1;
274     build_max_heap(arrRef, len);
275 
276     int i, tmp;
277     for(i=1; i<len; ++i)
278     {
279         tmp = arrRef[0];
280         arrRef[0] = arrRef[len-i];
281         arrRef[len-i] = tmp;
282         max_heapify(arrRef, len-i, 0);
283     }
284 }
285 
286 void build_max_heap(int arr[], int len)
287 {
288     int startIdx = (len - 2) / 2;
289     int i;
290     for(i=startIdx; i>=0; --i)
291     {
292         max_heapify(arr, len, i);
293     }
294 }
295 
296 void max_heapify(int arr[], int len, int root)
297 {
298     int last = (len-2) / 2;
299     if(root > last)
300         return;
301 
302     int left = root*2 + 1;
303     int right = root*2 + 2;
304     int max = arr[root];
305     int idx = root;
306 
307     if(left < len && arr[left] > max)
308     {
309         max = arr[left];
310         idx = left;
311     }
312     if(right < len && arr[right] > max)
313     {
314         max = arr[right];
315         idx = right;
316     }
317 
318     if(idx != root)
319     {
320         arr[idx] = arr[root];
321         arr[root] = max;
322         max_heapify(arr, len, idx);
323     }
324 }

 

冒泡法、选择法、快速、归并、堆排序算法实现

标签:

原文地址:http://www.cnblogs.com/yi-fei/p/5139776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!