I searched heavily for the code but couldn't find any of them written in C++, and clean enough.
So I decided to blog it down, for my own future references and above all, for the whole coding community.
#include "iostream"
#define max 9
using namespace std;
void display(int* a)
{
for(int i=0; i< max; i++)
printf("%d",a[i]);
printf("\n");
}
void swap(int& x,int& y)
{
int temp;
temp = x;
x = y;
y = temp;
}
int partition(int* a,int beg,int end)
{
int left = beg, right=end, piv = *(a+beg);
while(left
while((a[left]<=piv)&&(left
while((a[right]>=piv)&&(right>beg))
right--;
if(left
}
a[beg] = a[right];
a[right] = piv;
return(right);
}
void quick(int* a,int beg,int end)
{
if (beg>=end) return;
int pos = partition(a,beg,end);
cout<
quick(a,pos+1,end);
}
int main()
{
int arr[] = {1,5,2,7,3,4,6,0,9};
display(arr);
quick(arr,0,max-1);
printf("After Quick Sorting:");
display(arr);
system ("pause");
return 0;
}
The program was written in Dev C++ version 4.9.9.2
Compiler used- g++
No comments:
Post a Comment