/***************************************************************************** 002 * sort-impl.h 003 * 004 * Implementation for sort algorithms. 005 * 006 * Zhang Ming, 2010-07, Xi'an Jiaotong University. 007 *****************************************************************************/ 008 #include 009 010 typedef int bool; 011 #define true 1 012 #define false 0 013 014 void swap(int *a, int *b) 015 { 016 int t = *a; 017 *a = *b; 018 *b = t; 019 } 020 /** 021 * Bubble sort algorithm. 022 * "a" ----> array of Comparable items. 023 * "left" ----> the left-most index of the subarray. 024 * "right" ----> the right-most index of the subarray. 025 */ 026 027 void bubbleSort(int *a, int left, int right) 028 { 029 bool cond = true; 030 int i; 031 for (i = left; i < right; ++i) 032 { 033 cond = false; 034 int j; 035 for (j = right; j > i; --j) 036 if (a[j] < a[j - 1]) 037 { 038 swap(&a[j], &a[j - 1]); 039 cond = true; 040 } 041 042 if (!cond) 043 return; 044 } 045 } 046 047 /** 048 * Selection sort algorithm. 049 * "a" ----> array of Comparable items. 050 * "left" ----> the left-most index of the subarray. 051 * "right" ----> the right-most index of the subarray. 052 */ 053 054 void selectSort(int *a, int left, int right) 055 { 056 int minPos; 057 int i; 058 for (i = left; i < right; ++i) 059 { 060 minPos = i; 061 int j; 062 for (j = i + 1; j <= right; ++j) 063 if (a[j] < a[minPos]) 064 minPos = j; 065 066 if (i != minPos) 067 swap(&a[i], &a[minPos]); 068 } 069 } 070 071 /** 072 * Insertion sort algorithm. 073 * "a" ----> array of Comparable items. 074 * "left" ----> the left-most index of the subarray. 075 * "right" ----> the right-most index of the subarray. 076 */ 077 078 void insertSort(int *a, int left, int right) 079 { 080 int p; 081 for (p = left + 1; p <= right; p++) 082 { 083 int tmp = a[p]; 084 int j; 085 086 for (j = p; j > left && tmp < a[j - 1]; --j) 087 a[j] = a[j - 1]; 088 a[j] = tmp; 089 } 090 } 091 092 /** 093 * Internal quicksort method that makes recursive calls. 094 * Uses median-of-three partitioning and a cutoff of 20. 095 * "a" ----> array of Comparable items. 096 * "left" ----> the left-most index of the subarray. 097 * "right" ----> the right-most index of the subarray. 098 */ 099 int median3(int *a, int left, int right); 100 void quickSort(int *a, int left, int right) 101 { 102 if (left + 20 <= right) 103 { 104 int pivot = median3(a, left, right); 105 106 // begin partitioning 107 int i = left, j = right - 1; 108 for (;;) 109 { 110 while (a[++i] < pivot) 111 { 112 } 113 while (pivot < a[--j]) 114 { 115 } 116 117 if (i < j) 118 swap(&a[i], &a[j]); 119 else 120 break; 121 } 122 123 // Restore pivot 124 swap(&a[i], &a[right - 1]); 125 126 // Sort small elements 127 quickSort(a, left, i - 1); 128 129 // Sort large elements 130 quickSort(a, i + 1, right); 131 } 132 else 133 insertSort(a, left, right); 134 } 135 /** 136 * Return median of left, center, and right. 137 * Order these and hide the pivot. 138 */ 139 int median3(int *a, int left, int right) 140 { 141 int center = (left + right) / 2; 142 143 if (a[center] < a[left]) 144 swap(&a[left], &a[center]); 145 146 if (a[right] < a[left]) 147 swap(&a[left], &a[right]); 148 149 if (a[right] < a[center]) 150 swap(&a[center], &a[right]); 151 152 swap(&a[center], &a[right - 1]); 153 154 return a[right - 1]; 155 } 156 157 /** 158 * Merg sort algorithm (nonrecursion). 159 * "a" ----> array of Comparable items. 160 * "left" ----> the left-most index of the subarray. 161 * "right" ----> the right-most index of the subarray. 162 */ 163 164 void merg(int *a, int left1, int right1, int left2, int right2); 165 void mergSort(int *a, int left, int right) 166 { 167 int left1, right1, left2, right2, n = right - left + 1, size = 1; 168 169 while (size < n) 170 { 171 left1 = left; 172 173 while (left1 + size < n) 174 { 175 left2 = left1 + size; 176 right1 = left2 - 1; 177 if (left2 + size > n) 178 right2 = right; 179 else 180 right2 = left2 + size - 1; 181 182 merg(a, left1, right1, left2, right2); 183 184 left1 = right2 + 1; 185 } 186 187 size *= 2; 188 } 189 } 190 /** 191 * Merg two subsequence to a bigger one. 192 * The first subsequence is a[left1] ... a[right1], and 193 * The second subsqeuence is a[left2] ... a[right2]. 194 */ 195 void merg(int *a, int left1, int right1, int left2, int right2) 196 { 197 int k = 0, i = left1, j = left2, n1 = right1 - left1 + 1, n2 = right2 198 - left2 + 1; 199 200 int tmp[n1 + n2]; 201 202 while (i <= right1 && j <= right2) 203 if (a[i] < a[j]) 204 tmp[k++] = a[i++]; 205 else 206 tmp[k++] = a[j++]; 207 208 while (i <= right1) 209 tmp[k++] = a[i++]; 210 while (j <= right2) 211 tmp[k++] = a[j++]; 212 213 for (i = 0; i < n1; ++i) 214 a[left1++] = tmp[i]; 215 for (i = 0; i < n2; ++i) 216 a[left2++] = tmp[n1 + i]; 217 218 //delete []tmp; 219 } 220 221 /** 222 * Heap sort algorthm. 223 * "a" ----> array of Comparable items. 224 * "left" ----> the left-most index of the subarray. 225 * "right" ----> the right-most index of the subarray. 226 */ 227 228 void filterDown(int *a, int i, int n); 229 void heapSort(int *a, int left, int right) 230 { 231 int n = right - left + 1; 232 int tmp[n]; 233 int i; 234 for (i = 0; i < n; ++i) 235 tmp[i] = a[left + i]; 236 237 for (i = n / 2; i >= 0; --i) 238 filterDown(tmp, i, n); 239 int j; 240 for (j = n - 1; j > 0; --j) 241 { 242 swap(&tmp[0], &tmp[j]); 243 filterDown(tmp, 0, j); 244 } 245 246 for (i = 0; i < n; ++i) 247 a[left + i] = tmp[i]; 248 } 249 /** 250 * Percolate down the heap. 251 * "i" ----> the position from which to percolate down. 252 * "n" ----> the logical size of the binary heap. 253 */ 254 void filterDown(int *a, int i, int n) 255 { 256 int child; 257 int tmp; 258 259 for (tmp = a[i]; 2 * i + 1 < n; i = child) 260 { 261 child = 2 * i + 1; 262 if (child != n - 1 && a[child] < a[child + 1]) 263 child++; 264 265 if (tmp < a[child]) 266 a[i] = a[child]; 267 else 268 break; 269 } 270 a[i] = tmp; 271 } 272 273 int main() 274 { 275 int d[] = 276 { 7, 5, 6, 4, 2, 3, 1, 9, 8 }; 277 int n; 278 for (n = 0; n < sizeof(d) / sizeof(int); n++) 279 printf("%d", d[n]); 280 printf("/n"); 281 282 int opt = 0; 283 printf("1 bubbleSort/n"); 284 printf("2 selectSort/n"); 285 printf("3 insertSort/n"); 286 printf("4 quickSort/n"); 287 printf("5 mergSort/n"); 288 printf("6 heapSort/n"); 289 printf("option:"); 290 scanf("%d",&opt); 291 printf("/n"); 292 293 switch (opt) 294 { 295 case 1: 296 bubbleSort(d, 0, 9); 297 break; 298 case 2: 299 selectSort(d, 0, 9); 300 break; 301 case 3: 302 insertSort(d, 0, 9); 303 break; 304 case 4: 305 quickSort(d, 0, 9); 306 break; 307 case 5: 308 mergSort(d, 0, 9); 309 break; 310 case 6: 311 heapSort(d, 0, 9); 312 break; 313 default: 314 printf("input error!/n"); 315 exit(1); 316 } 317 318 for (n = 0; n < sizeof(d) / sizeof(int); n++) 319 printf("%d", d[n]); 320 }