常见的排序
常见的排序
直接插入排序、二分插入排序、希尔排序、冒泡排序、快排、简单选择排序、归并排序
1 #include <iostream> 2 using namespace std; 3 4 /** 5 *插入排序 6 */ 7 //直接插入排序 8 void insertSort(char sourse[], int n) { 9 for(int i=2; i<=n; ++i) { 10 if(sourse[i]<sourse[i-1]) { 11 sourse[0] = sourse[i]; 12 int j; 13 for(j=i-1; sourse[j]>sourse[0]; --j) { 14 sourse[j+1] = sourse[j]; 15 } 16 sourse[j+1] = sourse[0]; 17 } 18 } 19 } 20 21 //二分查找 22 int binarySearch(char sourse[], int low, int high, char aim) { 23 int mid = (low + high)/2; 24 while(low <= high) { 25 if(sourse[mid] == aim) { 26 low = mid; 27 break; 28 } 29 if(sourse[mid] > aim) { 30 high = mid - 1; 31 } else { 32 low = mid + 1; 33 } 34 mid = (low + high)/2; 35 } 36 37 return low; 38 } 39 40 //折半插入排序 41 void insertHalfSort(char sourse[], int n) { 42 for(int i=2; i<=n; ++i) { 43 if(sourse[i-1] > sourse[i]) { 44 sourse[0] = sourse[i]; 45 int pos = binarySearch(sourse, 1, i-1, sourse[0]); 46 for(int j=i-1; j>=pos; --j) { 47 sourse[j+1] = sourse[j]; 48 } 49 sourse[pos] = sourse[0]; 50 } 51 } 52 } 53 54 //希尔排序 55 void shellSort(char sourse[], int n) { 56 for(int step=n/2; step>=1; step=step/2) { 57 for(int i=step+1; i<=n; ++i) { 58 if(sourse[i] < sourse[i-step]) { 59 sourse[0] = sourse[i]; 60 int j; 61 for(j = i-step; sourse[0] < sourse[j] && j>0; j=j-step) { 62 sourse[j+step] = sourse[j]; 63 } 64 sourse[j+step] = sourse[0]; 65 } 66 } 67 } 68 } 69 70 71 72 /************************************************************************************************************/ 73 74 75 76 /** 77 *交换排序 78 */ 79 //冒泡排序 80 void bubbleSort(char sourse[], int n) { 81 for(int i=1; i<n; ++i) { 82 bool isChanged = false; 83 for(int j=1; j<n-i+1; ++j) { 84 if(sourse[j]>sourse[j+1]) { 85 swap(sourse[j], sourse[j+1]); 86 isChanged = true; 87 } 88 } 89 if(!isChanged) { 90 break; 91 } 92 } 93 } 94 95 //快排 96 int partition(char sourse[], int low, int high) { 97 sourse[0] = sourse[low]; 98 while(low<high) { 99 while(low<high && sourse[high] >= sourse[0]) 100 --high; 101 sourse[low] = sourse[high]; 102 while(low<high && sourse[low] <= sourse[0]) 103 ++low; 104 sourse[high] = sourse[low]; 105 } 106 sourse[low]=sourse[0]; 107 return low; 108 } 109 void quickSort(char sourse[], int low, int high) { 110 if(low >=high) { 111 return; 112 } 113 int pos = partition(sourse,low,high); 114 quickSort(sourse, low, pos-1); 115 quickSort(sourse, pos+1, high); 116 } 117 118 119 /************************************************************************************************************/ 120 121 /** 122 *选择排序 123 */ 124 //简单选择排序 125 void selectSort(char sourse[], int n) { 126 for(int i=1; i<=n; ++i) { 127 int minp = i; 128 for(int j=i; j<=n; ++j) { 129 if(sourse[j]<sourse[minp]) { 130 minp = j; 131 } 132 } 133 swap(sourse[i], sourse[minp]); 134 } 135 } 136 137 138 /** 139 *二路归并排序 140 */ 141 char medim[14]; 142 void merge(char sourse[], int low, int mid, int high) { 143 144 for(int i=low; i<=high; i++) { 145 medim[i] = sourse[i]; 146 } 147 int i=low, j=mid+1; 148 int k=low; 149 while(i<=mid && j<=high) { 150 if(medim[i]<=medim[j]) { 151 sourse[k]=medim[i]; 152 i++; 153 } else { 154 sourse[k]=medim[j]; 155 j++; 156 } 157 k++; 158 } 159 while(i<=mid) { 160 sourse[k]=medim[i]; 161 i++; 162 k++; 163 } 164 while(j<=high) { 165 sourse[k]=medim[j]; 166 k++; 167 j++; 168 } 169 170 } 171 void mergeSort(char sourse[], int low, int high) { 172 if(low >= high) { 173 return; 174 } 175 int mid = (low + high)/2; 176 mergeSort(sourse, low, mid); 177 mergeSort(sourse, mid+1, high); 178 merge(sourse, low, mid, high); 179 } 180 181 182 183 void show(char sourse[], int n) { 184 for(int i=1; i<=n; i++) { 185 cout<<sourse[i]; 186 } 187 cout<<endl; 188 } 189 190 int main() { 191 char sourse[]= {' ','j', 'k','d','g','e', 'l', 'm','h','a','n','f','c', 'i','b'}; 192 193 // insertSort(sourse, 14); 194 // insertSort(sourse, 14); 195 // shellSort(sourse, 14); 196 // bubbleSort(sourse, 14); 197 // quickSort(sourse, 1, 14); 198 // selectSort(sourse, 14); 199 mergeSort(sourse, 1, 14); 200 show(sourse, 14); 201 202 return 0; 203 }