Created
April 18, 2020 03:16
-
-
Save hoholee12/2df638b5b8539b8f88678a05e0075493 to your computer and use it in GitHub Desktop.
quicksort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define swap(x,y) { x = x + y; y = x - y; x = x - y; } | |
#define arrsize 12 | |
#define arrcontent { 8, 3, 11, 9, 12, 2, 6, 15, 18, 10, 7, 14} | |
#include<stdio.h> | |
void quicksort(int* A, int left, int right){ | |
for (int i = 0; i < arrsize; i++) printf("%d ", A[i]); | |
printf("\n"); | |
int pivot = left; | |
int low = left + 1; | |
int high = right; | |
int i = low, j = high; | |
//low pointer loop | |
while(i <= right){ | |
//A[low] > A[pivot] | |
if (A[i] > A[pivot]){ | |
//high pointer loop | |
while (A[j] > A[pivot]){ //A[high] < A[pivot] | |
j--; | |
} | |
high = j; | |
j--; | |
low = i; | |
//end if low and high pointers cross | |
if (low > high){ | |
break; | |
} | |
//swap if no OOB | |
if (low <= right && high >= left + 1) | |
swap(A[low], A[high]); | |
} | |
i++; | |
} | |
//swap if high isnt OOB | |
if (high >= left + 1){ | |
swap(A[high], A[pivot]); | |
pivot = high; | |
} | |
//divide if not end | |
if (left < pivot - 1) quicksort(A, left, pivot - 1); | |
if (pivot + 1 < right) quicksort(A, pivot + 1, right); | |
} | |
int main(){ | |
int A[arrsize] = arrcontent; | |
printf("before sort: "); | |
for (int i = 0; i < arrsize; i++) printf("%d ", A[i]); | |
printf("\nduring sort: \n"); | |
quicksort(A, 0, arrsize - 1); | |
printf("after sort: "); | |
for (int i = 0; i < arrsize; i++) printf("%d ", A[i]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment