常见排序算法实现

1.快速排序:

 1 class QuickSort {
 2 public:
 3     int* quickSort(int* A, int n) {
 4         quickSort(A, 0, n-1);
 5         return A;
 6     }
 7     void quickSort(int* arr, int begin, int end){
 8         if(begin >= end) return;
 9         int index = partition(arr, begin, end);
10         quickSort(arr, begin, index-1);
11         quickSort(arr, index+1, end);
12     }
13     int partition(int* arr, int begin, int end){
14          int pivot=arr[begin];
15          while(begin < end){
16              while(begin < end && arr[end] > pivot)
17                  --end;
18              if(begin < end) arr[begin] = arr[end];
19              while(begin < end && arr[begin] <= pivot)
20                  ++begin;
21              if(begin < end) arr[end] = arr[begin];
22          }
23          arr[begin] = pivot;
24          return begin;
25      }
26 };

2. 归并排序

 1 class MergeSort {
 2 public:
 3     int* mergeSort(int* A, int n) {
 4         if(n < 2) return A;
 5         mergeSort(A, 0, n-1);
 6         return A;
 7     }
 8     void mergeSort(int* A, int l, int r){
 9         if(l == r) return;
10         int mid = (l + r) / 2;
11         mergeSort(A, l, mid);
12         mergeSort(A, mid+1, r);
13         merge(A, l, mid, r);
14     }
15     
16     void merge(int* A, int left, int mid, int right){
17         int* tmp = new int[right-left+1];
18         int l=left, r=mid+1;
19         int i=0;
20         while(l<=mid && r<=right)
21             tmp[i++] = A[l]<=A[r] ? A[l++] : A[r++];
22         
23         while(l <= mid)
24             tmp[i++] = A[l++];
25         while(r <= right)
26             tmp[i++] = A[r++];
27         
28         for(int i=0; i<right-left+1; ++i)
29             A[left + i] = tmp[i];
30     }
31 };

3. 堆排序

https://www.cnblogs.com/chengxiao/p/6129630.html

 1 class HeapSort {
 2 public:
 3     int* heapSort(int* arr, int n) {
 4         // 1.构建大顶堆
 5         for(int i=n/2-1; i>=0; --i){
 6             adjustHeap(arr, i, n);  //从第一个非叶子结点从下至上,从右至左调整结构
 7         }
 8         //2.调整堆结构+交换堆顶元素与末尾元素
 9         for(int i=n-1; i>0; --i){
10             swap(arr[0], arr[i]);
11             adjustHeap(arr, 0, i);
12         }
13         return arr;
14     }
15     
16     void adjustHeap(int* arr, int pos, int len){
17         int parent=arr[pos];  // 取出当前元素
18         for(int i=2*pos+1; i<len; i=2*i+1){ // 从k结点的左子结点开始,也就是2k+1处开始
19             if(i+1<len && arr[i]<arr[i+1])   // 如果左子结点小于右子结点,i指向右子结点
20                 ++i;
21             if(arr[i] > parent){   //如果子节点大于父节点,将子节点值赋给父节点(不用进行交换)
22                 arr[pos] = arr[i];
23                 pos = i;
24             }else
25                 break;
26         }
27         arr[pos] = parent;
28     }
29 };

4. 希尔排序

 1 class ShellSort {
 2 public:
 3     int* shellSort(int* A, int n) {
 4         for(int gap=n/2; gap>=1; gap/=2){
 5             for(int i=gap; i<n; ++i){
 6                 int j=i;
 7                 while(j-gap>=0 && A[j-gap]>A[j]){
 8                     swap(A[j-gap], A[j]);
 9                     j-=gap;
10                 }
11             }
12         }
13         return A;
14     }
15 };

5. 计数排序

 1 class CountingSort {
 2 public:
 3     int* countingSort(int* A, int n) {
 4         int max_v = *max_element(A, A+n);
 5         int min_v = *min_element(A, A+n);
 6         int size = max_v - min_v + 1;
 7         int* count = new int[size]();  // 有括号才有初始值0
 8         for(int i=0; i<n; ++i){
 9             ++count[A[i] - min_v];
10         }
11         
12         int j=0;
13         for(int i=0; i<size; ++i){
14             while(count[i]--)
15                 A[j++] = min_v + i;
16         }
17         return A;
18     }
19 };

 

posted @ 2019-06-04 11:54  Longice  阅读(212)  评论(0编辑  收藏  举报