diff options
| author | mithe24 <mithe24@student.sdu.dk> | 2025-10-29 15:25:13 +0100 |
|---|---|---|
| committer | Andreas Kapp Lindquist <andkaplin05@gmail.com> | 2025-10-29 18:33:28 +0100 |
| commit | 48139d1c1d8f4da22a4ea474abe6f1f6aeffcaad (patch) | |
| tree | 88f1bd86f00bb3922c33ec7a7c195aecc7f6bf9e /report/qsort.c | |
| parent | 8db3bea514257cf68da8fe4768c5cd84e8f20b78 (diff) | |
| download | sorter-48139d1c1d8f4da22a4ea474abe6f1f6aeffcaad.tar.gz sorter-48139d1c1d8f4da22a4ea474abe6f1f6aeffcaad.zip | |
report(qsort): add part about qsort
Diffstat (limited to 'report/qsort.c')
| -rw-r--r-- | report/qsort.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/report/qsort.c b/report/qsort.c new file mode 100644 index 0000000..6877369 --- /dev/null +++ b/report/qsort.c @@ -0,0 +1,21 @@ +void quicksort(int arr[], int lo, int hi) { + if (lo >= 0 && hi >= 0 && lo < hi) { + int p = partition(A, lo, hi) + quicksort(A, lo, p) + quicksort(A, p + 1, hi) + } +} + +int partition(int arr[], int lo, int hi) { + int pivot = arr[lo]; + int i = lo - 1, j = hi + 1; + while (true) { + do { i++; } while (arr[i] < pivot); + do { j--; } while (arr[j] > pivot); + if (i >= j) break; + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + return j; +} |