标签:quick i++ int arch sel shel oid pre span
1 //冒泡排序 2 void bubbleSorte(int* arr, int len) 3 { 4 for (int i = 0; i < len - 1; i++) 5 { 6 for (int j = 0; j < len - 1 - i; j++) 7 { 8 if (arr[j] > arr[j + 1]) 9 { 10 arr[j] ^= arr[j + 1]; 11 arr[j + 1] ^= arr[j]; 12 arr[j] ^= arr[j + 1]; 13 } 14 } 15 } 16 }
1 //选择排序 2 void selectSorte(int* arr, int len) 3 { 4 for (int i = 0; i < len - 1; i++) 5 { 6 for (int j = i + 1; j < len; j++) 7 { 8 if (arr[i] > arr[j]) 9 { 10 arr[i] ^= arr[j]; 11 arr[j] ^= arr[i]; 12 arr[i] ^= arr[j]; 13 } 14 } 15 } 16 }
1 //插入排序 2 void insertSorte(int* arr, int len) 3 { 4 int i, j, t; 5 for (i = 1; i < len; i++) 6 { 7 t = arr[i]; 8 for (j = i; j - 1 >= 0 && t < arr[j - 1]; j--) 9 { 10 arr[j] = arr[j - 1]; 11 } 12 arr[j] = t; 13 } 14 }
1 //希尔排序 2 void shellSorte(int* arr, int len) 3 { 4 int idx = len / 2; 5 while (idx >= 1) 6 { 7 int i, j, t; 8 for (i = idx; i < len; i++) 9 { 10 t = arr[i]; 11 for (j = i; j - idx >= 0 && t < arr[j - idx]; j -= idx) 12 { 13 arr[j] = arr[j - idx]; 14 } 15 arr[j] = t; 16 } 17 idx /= 2; 18 } 19 }
1 //快速排序 2 void quickSorte(int* arr, int left, int right) 3 { 4 if (left < right) 5 { 6 int low = left; int high = right; 7 int sh = arr[left]; 8 while (low < high) 9 { 10 while (arr[high] > sh&&low < high) 11 { 12 high--; 13 } 14 arr[low] = arr[high]; 15 while (arr[low] < sh&&low < high) 16 { 17 low++; 18 } 19 arr[high] = arr[low]; 20 } 21 arr[low] = sh; 22 quickSorte(arr, left, high - 1); 23 quickSorte(arr, low + 1, right); 24 } 25 }
1 //二分查找 2 int Two_points_search(int* arr, int low, int high, int find) 3 { 4 while (low<=high) 5 { 6 int idx = (low + high) / 2; 7 if (arr[idx] == find) 8 { 9 return idx; 10 } 11 else if (find > arr[idx]) 12 { 13 low = idx + 1; 14 } 15 else 16 { 17 high = idx - 1; 18 } 19 } 20 return -1; 21 }
标签:quick i++ int arch sel shel oid pre span
原文地址:https://www.cnblogs.com/mtn007/p/12228434.html