Quick Sort Program Code in C++
Read Similar Posts:1. Quick Sort In C
2. Quick Sort Time Complexity
using namespace std;
int partition(int a[], int left, int right, int n);
void QuickSort(int a[], int left, int right, int n);
{
int i, n, numbers[100];
cin>>n;
for(i=0;i<n;i++)
cin>>numbers[i];
}
for(i=0;i<n;i++)
cout<<numbers[i]<<" ";
}
return 0;
}
int partition(int a[], int left, int right, int n)
{
int piv, i, j, temp;
piv = left;
i = piv + 1;
j = right;
while(i<=j)
{
while(a[piv] < a[j] && piv < j)
{
j--;
}
if(a[piv] > a[j] && piv < j)
{
temp = a[piv];
a[piv] = a[j];
a[j] = temp;
piv = j;
j--;
}
while(a[i] < a[piv] && i < piv)
{
i++;
}
if(a[i] > a[piv] && i < piv)
{
temp = a[piv];
a[piv] = a[i];
a[i] = temp;
piv = i;
i++;
}
}
return piv;
}
void QuickSort(int a[], int left, int right, int n)
{
if(left < right)
{
int ind = partition(a, left, right, n);
QuickSort(a, left, ind-1, n);
QuickSort(a, ind+1, right, n);
}
}
//Code Body End