Last active
June 24, 2020 04:34
-
-
Save educartoons/09c09ffd8adf8372bab212a42d378a20 to your computer and use it in GitHub Desktop.
Implementation of Quick Sort in C++
This file contains 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
// We will call the function with quicksort(numbers, 1, numbers.length - 1) | |
int partition(int *numbers, int p, int r){ | |
int x = numbers[r]; | |
int i = p - 1; | |
for (int j = p; j<r; j++) { | |
if(numbers[j]<=x){ | |
i = i + 1; | |
swap(numbers[i], numbers[j]); | |
} | |
} | |
swap(numbers[i+1], numbers[r]); | |
return i+1; | |
} | |
void quickSort(int *numbers,int p,int r){ | |
if(p<r){ | |
int q = partition(numbers, p, r); | |
quickSort(numbers, p, q-1); | |
quickSort(numbers, q+1, r); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment