Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void printData(double *Data, int L, int R) {
- for (int i = L; i <= R ; i++) {
- printf("%g ", Data[i]);
- }
- printf("\n");
- }
- void swap(double *x, double *y) {
- double swap = *x;
- *x = *y;
- *y = swap;
- }
- void quickSortBase(double *Data, int L, int R) {
- int pivot_index = (L + R) / 2;
- double pivot = Data[pivot_index];
- printf("sorting [%d] ... [%d], pivot=[%d]:%g\n", L, R, pivot_index, pivot);
- int i = L;
- int j = R;
- while (1) {
- while (Data[i] < pivot) i++;
- while (pivot < Data[j]) j--;
- if (i >= j) break;
- printf("swapped [%d]:%g <=> [%d]:%g\n", i, Data[i], j, Data[j]);
- swap(&Data[i], &Data[j]);
- i++;
- j--;
- }
- quickSortBase(Data, L, i - 1);
- quickSortBase(Data, j + 1, R);
- }
- void quickSort(double *Data, int N) {
- if (N >= 2) {
- quickSortBase(Data, 0, N);
- }
- }
- int main(void) {
- double Data[] = { 1., 3., 4., 3., 5., 5., 2., 4. };
- int N = sizeof(Data) / sizeof(Data[0]);
- quickSort(Data, N);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement