八大基于比较的排序算法(回顾和模板代码)

  1 #include <map>
  2 #include <set>
  3 #include <cmath>
  4 #include <ctime>
  5 #include <queue>
  6 #include <stack>
  7 #include <vector>
  8 #include <bitset>
  9 #include <cstdio>
 10 #include <cctype>
 11 #include <string>
 12 #include <cstring>
 13 #include <cassert>
 14 #include <climits>
 15 #include <cstdlib>
 16 #include <iostream>
 17 #include <algorithm>
 18 #include <functional>
 19 using namespace std ;
 20 
 21 #define rep(i, a, b) for (int (i)=(a);(i)<=(b);(i)++)
 22 #define Rep(i, a, b) for (int (i)=(a)-1;(i)<(b);(i)++)
 23 #define REP(i, a, b) for (int (i)=(a);(i)>=(b);(i)--)
 24 #define reg(i, x) for (int (i)=head[x];(i);i=e[i].next)
 25 #define clr(a) memset(a,0,sizeof(a))
 26 #define Sort(a, len) sort(a + 1, a + len + 1)
 27 #define Sort2(a, len, cmp) sort(a + 1, a + len + 1, cmp)
 28 #define ass(a, sum) memset(a, sum, sizeof(a))
 29 
 30 #define ull unsigned long long
 31 #define ll long long
 32 #define ls ((rt) << 1)
 33 #define rs ((rt) << 1 | 1)
 34 #define mp make_pair
 35 #define pb push_back
 36 #define fi first
 37 #define se second
 38 #define endl '\n'
 39 #define Pii pair<int, int>
 40 
 41 const int N = 100010 ;
 42 const int iinf = INT_MAX/2 ;
 43 const ll linf = LLONG_MAX/2 ;
 44 const int MOD = 1e9+7 ;
 45 
 46 inline int read(){
 47     int X = 0,w = 0 ;
 48     char ch = 0;
 49     while(!isdigit(ch)) {w |= ch == '-';ch = getchar();}
 50     while(isdigit(ch)) X = (X<<3) + (X<<1) + (ch ^ 48),ch = getchar();
 51     return w ? -X : X;
 52 }
 53 
 54 void write(int x){
 55      if(x < 0) putchar('-'),x = -x;
 56      if(x > 9) write(x / 10);
 57      putchar(x%10 + '0');
 58 }
 59 
 60 int n ;
 61 int a[N], b[N] ; 
 62 
 63 /*
 64 排序算法名称     平均         最优      最差      辅助空间      稳定性
 65 Bubblesort       O(n^2)       O(n)      O(n^2)      O(1)         稳定 
 66 CocktailSort     O(n^2)       O(n)      O(n^2)      O(1)         稳定 
 67 SelectionSort    O(n^2)       O(n^2)    O(n^2)      O(1)         不稳定 
 68 InsertionSort    O(n^2)       O(n)      O(n^2)      O(1)         稳定 
 69 efInsertionSort  O(n^2)       O(nlogn)  O(n^2)      O(1)         稳定 
 70 Shellsort        O(n^(1.3~2}) O(n)      O(n^2)      O(1)         不稳定 
 71 MergeSort        O(nlogn)     O(nlogn)  O(nlogn)    O(n)         稳定 
 72 HeapSort         O(nlogn)     O(nlogn)  O(nlogn)    O(1)         不稳定  
 73 QuickSort        O(nlogn)     O(nlogn)  O(n^2)      O(1)         不稳定 
 74 */ 
 75 
 76 void BubbleSort() { // 冒泡排序 
 77     for (int i = 1; i <= n; i++){
 78          bool flag = false ; 
 79         for (int j = 1; j <= n - i; j++)
 80         if (a[j] > a[j + 1]) swap(a[j], a[j + 1]), flag = true ;
 81         if (!flag) return ;
 82     } 
 83 }
 84 
 85 void CocktailSort() { //鸡尾酒排序 
 86     int l = 1, r = n ;
 87     while (l < r) {
 88         for (int i = l; i < r; i++) if (a[i] > a[i + 1]) swap(a[i], a[i + 1]) ;
 89         r-- ;
 90         for (int i = r; i > l; i--) if (a[i - 1] > a[i]) swap(a[i - 1], a[i]) ;
 91         l++ ; 
 92     }
 93 }
 94 
 95 void SelectionSort() { //选择排序 
 96     for (int i = 1; i <= n; i++){
 97         int MIN = i ;
 98         for (int j = i + 1; j <= n; j++) if (a[j] < a[MIN]) MIN = j ;
 99         swap(a[MIN], a[i]) ;
100     }
101 } 
102 
103 void InsertionSort() { //插入排序 
104     for (int i = 2; i <= n; i++) {
105         int NOW = a[i] ;
106         int j = i - 1 ;
107         while (j >= 1 && a[j] > NOW) a[j + 1] = a[j], j-- ;
108         a[j + 1] = NOW ; 
109     } 
110 }
111 
112 void efInsertionSort() { //二分插入排序 
113     for (int i = 2; i <= n; i++) {
114         int NOW = a[i] ;
115         int l = 1, r = i ;
116         while (l < r) {
117             int mid = (l + r) >> 1 ;
118             if (a[mid] > NOW) r = mid - 1 ;
119             else l = mid + 1 ;
120         }
121         for (int j = i - 1; j >= l; j--) a[j + 1] = a[j] ;
122         a[l] = NOW ;
123     }
124 } 
125 
126 void ShellSort() { //希尔排序 
127     int h = 1 ; // 增量 
128     while (h <= n) h = h * 3 + 1 ;
129     while (h) {
130         for (int i = h; i <= n; i++) {
131             int j = i - h ;
132             int NOW = a[i] ;
133             while (j > 0 && a[j] > NOW) {
134                 a[j + h] = a[j] ;
135                 j = j - h ;
136             }
137             a[j + h] = NOW ;
138         }
139         h = (h - 1) / 3 ;
140     }
141 } 
142 
143 void MergeSort(int l, int r) { // 归并排序 
144     if (l == r) return ;
145     int mid  = (l + r) >> 1 ;
146     MergeSort(l, mid) ;
147     MergeSort(mid + 1, r) ;
148     int L = l, R = mid + 1, tot = l ;
149     while (L <= mid && R <= r) {
150         if (a[L] <= a[R]) b[tot++] = a[L++] ;
151         else b[tot++] = a[R++] ;
152     }
153     while (L <= mid) b[tot++] = a[L++] ;
154     while (R <= r) b[tot++] = a[R++] ;
155     for (int i = l; i <= r; i++) a[i] = b[i] ;
156 }
157 
158 void HeapSort() { //堆排序 
159     priority_queue <int, vector<int>, greater<int> > q ; //相当于小根堆 
160     for (int i = 1; i <= n; i++) q.push(a[i]) ;
161     int p = 1 ;
162     while (!q.empty()) a[p++] = q.top(), q.pop() ;
163 }
164 
165 void QuickSort(int l, int r) { // 快速排序 
166     if (l >= r) return ;
167     int L = l, R = r ;
168     int tmp = a[L] ;
169     while (L < R) {
170         while (L < R && a[R] > tmp) R-- ;
171         if (L < R) a[L++] = a[R] ;
172         while (L < R && a[L] < tmp) L++ ;
173         if (L < R) a[R--] = a[L] ; 
174     } 
175     a[L] = tmp ;
176     QuickSort(l, L - 1) ;
177     QuickSort(L + 1, r) ;
178 }
179 
180 int main(){
181     scanf("%d", &n) ;
182     for (int i = 1; i <= n; i++) scanf("%d", &a[i]) ;
183 //     BubbleSort() ;
184 //    CocktailSort() ;
185 //    SelectionSort() ;
186 //    InsertionSort() ;
187 //    efInsertionSort() ;
188 //    ShellSort() ; 
189         MergeSort(1, n) ;
190 //    HeapSort() ;
191 //     QuickSort(1, n) ;
192     for (int i = 1; i <= n; i++) cout << a[i] << " " ;
193     cout << endl ;
194 }
Sort

这是一篇很好的入门博客

posted @ 2018-10-08 17:26  harryhqg  阅读(426)  评论(0编辑  收藏  举报