1. 程式人生 > >(C語言)八大排序之:堆排序、快速排序

(C語言)八大排序之:堆排序、快速排序

堆排序( heap sort )  reference:http://mp.weixin.qq.com/s/mY_bVJPWhzZWL5ZdooA6tw

  1 /* FILE: heapSort.c
  2  * DATE: 20180116
  3  * --------------
  4  */
  5 
  6 #include <stdio.h>
  7 
  8 #define swap(a, b) do{ \
  9         (a) = (a) ^ (b); \
 10         (b) = (b) ^ (a); \
 11         (a) = (a) ^ (b); \
 12         }while(0)
 13 
 14 void heapAdjust(int *array, int parent, int length);
 15 void heapSort(int *array, int length);
 16 void print(int *array, int length);
 17 
 18 int main(int argc, char *argv[])
 19 {
 20         int a[] = {1,3,4,5,2,6,9,7,8,0};
 21         int len = sizeof(a) / sizeof(a[0]);
 22         printf("origin: ");
 23         print(a, len);
 24         heapSort(a, len);
 25         printf("heapSort: ");
 26         print(a, len);
 27         return 0;
 28 }
 29 
 30 void heapAdjust(int *array, int parent, int length)
 31 {
 32         int temp = array[parent];       // 儲存當前父節點
 33         int child = 2 * parent + 1;     // 先獲得左孩子
 34         while(child < length)
 35         {
 36                 if(child+1<length && array[child]<array[child+1])
 37                         child++;
 38                 // 如果父節點的值已經大於孩子節點的值,則直接結束
 39                 if(temp >= array[child])
 40                         break;
 41                 // 孩子節點的值賦給父節點
 42                 array[parent] = array[child];
 43                 parent = child;
 44                 child = 2 * parent + 1;
 45         }
 46         array[parent] = temp;
 47 }
 48 
 49 void heapSort(int *array, int length)
 50 {
 51         int i;
 52         // 迴圈建立初始堆
 53         for(i=length/2; i>=0; i--)
 54         {
 55                 heapAdjust(array, i, length);
 56                 printf("initialization: ");
 57                 print(array, length);
 58         }
 59         // n-1次迴圈完成排序
 60         for(i=length-1; i>0; i--)
 61         {
 62                 // 第一個和最後一個元素交換
 63                 swap(array[i], array[0]);
 64                 // 篩選[0]節點,得到i-1個節點的堆
 65                 heapAdjust(array, 0, i);
 66                 print(array, length);
 67         }
 68 }
 69 
 70 void print(int *array, int length)
 71 {
 72         int i;
 73         for(i=0; i<length; i++)
 74                 printf("%d ", array[i]);
 75         printf("\n");
 76 }

快速排序( quick sort )

  1 #include <stdio.h>
  2 
  3 #define swap(a, b) do{ \
  4         (a) = (a) ^ (b); \
  5         (b) = (b) ^ (a); \
  6         (a) = (a) ^ (b); \
  7         }while(0)
  8 
  9 void quickSort(int *array, int begin, int end);
 10 void print(int *array, int length);
 11 
 12 int main(int argc, char *argv[])
 13 {
 14         int a[] = {2,6,9,1,0,25,6};
 15         int len = sizeof(a) / sizeof(a[0]);
 16         print(a, len);
 17         quickSort(a, 0, len-1);
 18         print(a, len);
 19 
 20         return 0;
 21 }
 22 
 23 void quickSort(int *array, int begin, int end)
 24 {
 25         int left = begin;
 26         int right = end;
 27         int pivot = array[begin];
 28         if(begin >= end)
 29                 return;
 30         while(left < right)
 31         {
 32                 while((left<right) && (array[right] >= pivot))
 33                         right--;
 34                 swap(array[right], array[left]);
 35                 while((left<right) && (array[left] < pivot))
 36                         left++;
 37                 swap(array[left], array[right]);
 38         }
 39         array[left] = pivot;
 40         quickSort(array, begin, left-1);        // 遞迴
 41         quickSort(array, left+1, end);
 42 }
 43 
 44 void print(int *array, int length)
 45 {
 46         int i;
 47         for(i=0; i<length; i++)
 48                 printf("%d ", array[i]);
 49         printf("\n");
 50 }