//快速排序 void quickSort(int *a, int start, int end) { if (start>=end) { return; } int pivot = a[start]; //選取第一個元素爲基準 int left_index = start; int right_index = end; bool flag_left_or_right = true; //0-表示計算座標的,1-表示計算右邊的 while (right_index>left_index) { if (flag_left_or_right==1) { if (a[right_index] >= pivot) { right_index--; continue; } else { int temp = a[right_index]; a[right_index] = a[left_index]; a[left_index] = temp; left_index++; flag_left_or_right = false; } } else { if (a[left_index]<=pivot) { left_index++; continue; } else { int temp = a[left_index]; a[left_index] = a[right_index]; a[right_index] = temp; right_index--; flag_left_or_right = true; } } } quickSort(a, start, right_index); quickSort(a, right_index + 1, end); }