/*--------------------------------------------------------*/ void quick_sort_1(float *list, int *index, int left_end, int right_end) { int i, j, temp; float chosen; chosen = list[index[(left_end + right_end)/2]]; i = left_end-1; j = right_end+1; for (;;) { while (list[index[++i]] < chosen); { while (list[index[--j]] > chosen); if (i < j) { temp=index [j]; index [j] = index [i]; index [i] = temp; } else if (i == j) { ++i; break; } else break; } } if (left_end < j) quick_sort_1(list, index, left_end, j); if (i < right_end) quick_sort_1(list, index, i, right_end); return; }
void quick_sort_1(int a[], int l, int r) { //整列する要素が一つなら終わり int v; if (l >= r) { return; } v = partition(a, l, r); quick_sort_1(a, l, v-1); quick_sort_1(a, v+1, r); }
/*--------------------------------------------------------*/ void quick_sort(float *list, int *index, int n) { int i; for (i=0; i<n; i++) index[i]=i; quick_sort_1(list, index, 0, n-1); return; }
static int quick_sort_1(int *arr, int l, int r, int size) { if (l >= r) return -1; int pivot = (l+r)/2; //swap the pivot to end swap_pivot(&arr[pivot], &arr[r]); int left = l; int right = r-1; //because the pivot swap to the end, so the right index need to decrease 1 while (/*left < right*/1) { while (less_than(arr[left], arr[r], &comp)) ++ left; while(less_than(arr[r], arr[right], &comp)) -- right; if (left < right) { int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; ++ left; -- right; ++ swap_times; } else break; } swap_pivot(&arr[r], &arr[left]); for(int n = 0; n < size; ++n) cout << arr[n] << " "; cout<<endl; quick_sort_1(arr, l, left-1, size); quick_sort_1(arr, left+1, r, size); return 0; }
int quickSort_1(int *arr, int size) { comp = 0; swap_times = 0; rdirect out("D:\\output.txt"); cout<<"**************** Quick sort 1 **************************"<<endl; quick_sort_1(arr, 0, size-1, size); cout << "compare :" << comp << " times\n"; cout << "swap :" << swap_times << " times\n"; return 0; }
void quick_sort(int a[], int n) { quick_sort_1(a, 0, n-1); }