Skip to content

Instantly share code, notes, and snippets.

@educartoons
Last active June 24, 2020 04:34
Show Gist options
  • Save educartoons/09c09ffd8adf8372bab212a42d378a20 to your computer and use it in GitHub Desktop.
Save educartoons/09c09ffd8adf8372bab212a42d378a20 to your computer and use it in GitHub Desktop.
Implementation of Quick Sort in C++
// 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