Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Oct 31, 2017

how can i make it full quick sort ?

@megiajip

This comment has been minimized.

Copy link

commented May 21, 2018

thank you very much

@seunggabi

This comment has been minimized.

Copy link

commented Sep 11, 2018

Thanks

@Sialor

This comment has been minimized.

Copy link

commented Nov 27, 2018

Nice code, bro 👍

@m-uzakkir

This comment has been minimized.

Copy link

commented Mar 21, 2019

Thanks a lot....

@TimurKuashev

This comment has been minimized.

Copy link

commented Jul 22, 2019

Thx, u are the best

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.