排序算法
Posted 任我主宰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了排序算法相关的知识,希望对你有一定的参考价值。
参考链接写的:http://www.cnblogs.com/eniac12/p/5329396.html
1 #include<iostream> 2 3 using namespace std; 4 5 void swap(int A[], int i, int j){ 6 7 int temp = A[i]; 8 A[i] = A[j]; 9 A[j] = temp; 10 11 } 12 13 14 void Bubble_Sort(int A[],int size,bool signal){ 15 16 for(int i = 0; i < size; i++){ 17 bool flag = true; //判断是否有交换过 18 for(int j = 0; j < size-i-1; j++){ 19 20 if(signal == true){ // true --> least to lagest 21 if(A[j] > A[j+1] ){ 22 swap(A,j,j+1); 23 flag = false; 24 } 25 } 26 else{ 27 if(A[j] < A[j+1] ){ // false --> lagest to least 28 swap(A,j,j+1); 29 flag = false; 30 } 31 } 32 33 34 } 35 if(flag == true) break; 36 } 37 38 } 39 40 41 void CocktailSort(int A[],int size){ 42 43 int left = 0; 44 int right = size-1; 45 while(left < right){ 46 bool flag = false; 47 for(int i = left ; i < right; i++){ 48 if(A[i] > A[i+1]){ 49 swap(A,i,i+1); 50 flag = true; 51 } 52 } 53 right--; 54 for(int j = right; j > left; j--){ 55 if(A[j] < A[j-1]){ 56 swap(A,j,j-1); 57 flag = true; 58 } 59 } 60 left++; 61 if(flag = false) break; 62 } 63 64 } 65 66 void SelectionSort(int A[],int size){ 67 68 /* int left = 0; // SelectionSort + CocktailSort 69 int right = size-1; 70 for(int i=left;i<=right;i++){ 71 int min = i; 72 int max = i; 73 for(int j=i;j<=right;j++){ 74 if(A[j]<A[i]){ 75 min = j; 76 } 77 if(A[j]>A[i]){ 78 max = j; 79 } 80 } 81 swap(A,left,min); 82 left++; 83 swap(A,right,max); 84 right--; 85 } */ 86 87 for(int i=0;i<size;i++){ 88 int min = i; 89 for(int j=i;j<size;j++){ 90 if(A[j]<A[min]){ 91 min = j; 92 } 93 } 94 95 if(min != i){ 96 swap(A,i,min); 97 } 98 99 } 100 101 } 102 103 104 void InsertSort(int A[],int size){ 105 106 107 for(int i=1;i<size;i++){ 108 int get = A[i]; 109 int j = i-1; 110 111 //no swap 112 while(j>=0 && get<A[j]){ 113 A[j+1] = A[j]; 114 j--; 115 } 116 A[j+1] = get; 117 /* 118 //swap 119 while(j>=0 && get< A[j]){ 120 swap(A,j,i); 121 j--; 122 } 123 */ 124 } 125 126 } 127 128 void BinaryInsertionSort(int A[],int size){ 129 130 for(int i=1;i<size;i++){ 131 int left = 0; 132 int right = i-1; 133 int get = A[i]; 134 while(left <= right){ 135 int mid = (left+right)/2; 136 if(A[mid]<get){ 137 left = mid+1; 138 } 139 else if(A[mid]>get){ 140 right = mid-1; 141 } 142 else break; 143 } 144 for(int j=i-1;j>=left;j--){ 145 A[j+1] = A[j]; 146 } 147 A[left] = get; 148 } 149 } 150 151 void Merge(int A[], int left, int mid, int right){ 152 153 int len = right-left+1; 154 int *tmp = new int[len]; 155 int index = 0; 156 int i = left; 157 int j = mid+1; 158 while(i<=mid&&j<=right){ 159 tmp[index++] = A[i]<A[j] ? A[i++] : A[j++]; 160 } 161 while(i<=mid){ 162 tmp[index++] = A[i++]; 163 } 164 while(j<=right){ 165 tmp[index++] = A[j++]; 166 } 167 for(int k=0;k<len;k++){ 168 A[left++] = tmp[k]; 169 } 170 171 172 } 173 void MergeSortRecursion(int A[], int left, int right){ 174 175 if(right == left) return; 176 int mid = (left + right) / 2; 177 MergeSortRecursion(A,left,mid); //排左边 178 MergeSortRecursion(A,mid+1,right); //排右边 179 Merge(A,left,mid,right); //合并 180 181 } 182 183 void MergeSortIteration(int A[], int size){ 184 185 int left,mid,right; 186 187 for(int i=1;i<size;i=i*2){ 188 189 left = 0; 190 191 while(i+left<size){ 192 mid = left+i-1; 193 right = mid+i < size ? mid+i : size-1; 194 Merge(A,left,mid,right); 195 left = right+1; 196 } 197 198 } 199 200 } 201 202 203 void Heapify(int A[],int i,int size){ 204 205 int left_child = 2*i+1; 206 int right_child = 2*i+2; 207 int max = i; 208 while(left_child < size && A[left_child] > A[max]){ 209 max = left_child; 210 } 211 while(right_child < size && A[right_child] > A[max]){ 212 max = right_child; 213 } 214 215 if(max !=i){ 216 swap(A,i,max); 217 Heapify(A,max,size); 218 } 219 220 } 221 222 223 void HeapSort(int A[],int size){ 224 225 for(int i=size/2-1;i>=0;i--){ 226 Heapify(A,i,size); 227 } 228 int heap_size = size; 229 while(heap_size>1){ 230 swap(A,0,--heap_size); 231 Heapify(A,0,heap_size); 232 } 233 234 } 235 236 237 int QuickSortDetile(int A[], int left, int right){ 238 int pivot = A[right]; 239 int index = left-1; 240 for(int i=left;i<right;i++){ 241 if(A[i] <= pivot){ 242 if((++index)!=i) swap(A,++index,i); 243 } 244 } 245 swap(A,index+1,right); 246 return index; 247 } 248 249 void QuickSort(int A[],int left,int right){ 250 251 if(left >= right){ 252 return; 253 } 254 int t = QuickSortDetile(A,left,right); 255 QuickSort(A,left,t-1); 256 QuickSort(A,t+1,right); 257 258 } 259 260 261 void Display(int A[], int size){ 262 for(int i=0;i<size;i++){ 263 cout<<A[i]<<" "; 264 } 265 cout<<endl; 266 } 267 268 int main(){ 269 int A[] = {1,3,3,11,7,5,8,6,2,4,9,10}; 270 int size = sizeof(A)/sizeof(int); 271 //BUbble sort 272 Bubble_Sort(A,size,false); 273 cout<<"The result of the Bubble Sort :"<<endl; 274 Display(A,size); 275 276 cout<<endl; 277 278 //Cocktail Sort 279 cout<<"The result of the Cocktail Sort :"<<endl; 280 CocktailSort(A,size); 281 Display(A,size); 282 283 cout<<endl; 284 285 //Selection Sort 286 SelectionSort(A,size); 287 cout<<"The result of the Selection Sort :"<<endl; 288 Display(A,size); 289 290 cout<<endl; 291 292 //Insertion Sort 293 InsertSort(A,size); 294 cout<<"The result of the Insertion Sort :"<<endl; 295 Display(A,size); 296 297 cout<<endl; 298 299 //Binary Insertion Sort 300 BinaryInsertionSort(A,size); 301 cout<<"The result of the BinaryInsertionSort Sort :"<<endl; 302 Display(A,size); 303 304 cout<<endl; 305 306 //MergeSort 递归实现 307 MergeSortRecursion(A,0,size-1); 308 cout<<"The result of the MergeSortRecursion (递归实现) Sort :"<<endl; 309 Display(A,size); 310 311 cout<<endl; 312 313 //MergeSort 迭代实现 314 MergeSortIteration(A,size); 315 cout<<"The result of the MergeSortIteration(迭代实现) Sort :"<<endl; 316 Display(A,size); 317 318 cout<<endl; 319 320 //HeapSort 321 HeapSort(A,size); 322 cout<<"The result of the HeapSort :"<<endl; 323 Display(A,size); 324 325 cout<<endl; 326 327 //QuickSort 328 QuickSort(A,0,size-1); 329 cout<<"The result of the QuickSort :"<<endl; 330 Display(A,size); 331 332 333 334 return 0; 335 }
以上是关于排序算法的主要内容,如果未能解决你的问题,请参考以下文章