快排
void quickSort(int* a,int right,int left=0) { int low = left; int high = right; while(low < high) { while (low < high && a[low] < a[high]) high--; if (low != high) { int temp = a[low]; a[low] = a[high]; a[high] = temp; } while(low < high && a[low] < a[high]) low++; if (low != high) { int temp = a[low]; a[low] = a[high]; a[high] = temp; } } if (left < low-1) quickSort(a,low-1,left); if (low+1 < right) quickSort(a,right,low+1); }
例如int a[8] = {4,6,3,5,8,2,1,7};
第一轮:
1 6 3 5 8 2 4 7
1 4 3 5 8 2 6 7
1 2 3 5 8 4 6 7
1 2 3 4 8 5 6 7
浙公网安备 33010602011771号