Skip to content

Instantly share code, notes, and snippets.

@christophewang
Last active April 10, 2024 22:59
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save christophewang/ad056af4b3ab0ceebacf to your computer and use it in GitHub Desktop.
Save christophewang/ad056af4b3ab0ceebacf to your computer and use it in GitHub Desktop.
Quick Sort in C++
#include <iostream>
void printArray(int *array, int n)
{
for (int i = 0; i < n; ++i)
std::cout << array[i] << std::endl;
}
void quickSort(int *array, int low, int high)
{
int i = low;
int j = high;
int pivot = array[(i + j) / 2];
int temp;
while (i <= j)
{
while (array[i] < pivot)
i++;
while (array[j] > pivot)
j--;
if (i <= j)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}
if (j > low)
quickSort(array, low, j);
if (i < high)
quickSort(array, i, high);
}
int main()
{
int array[] = {95, 45, 48, 98, 1, 485, 65, 478, 1, 2325};
int n = sizeof(array)/sizeof(array[0]);
std::cout << "Before Quick Sort :" << std::endl;
printArray(array, n);
quickSort(array, 0, n);
std::cout << "After Quick Sort :" << std::endl;
printArray(array, n);
return (0);
}
@acartu16
Copy link

how can i make it full quick sort ?

@megiajip
Copy link

thank you very much

@seunggabi
Copy link

Thanks

@Sialor
Copy link

Sialor commented Nov 27, 2018

Nice code, bro 👍

@m-uzakkir
Copy link

Thanks a lot....

@TimurKuashev
Copy link

Thx, u are the best

@Mr-Slippery
Copy link

int array[] = {0, 0};

@sdasdsda35435dfdfs
Copy link

👍👍

@zqp2013
Copy link

zqp2013 commented Jun 10, 2020

quickSort(array, 0, n-1); high must be n-1.

@Llewellynvdm
Copy link

quickSort(array, 0, n-1); high must be n-1.

👍

@Silidrone
Copy link

Nice one!

@ManuelWiki
Copy link

Good job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment