DSA: Quick Sort
class qsort
{
int j,n=0,a[],key,temp;
public qsort(int max)
{
a=new int[max];
}
public void insert(int item)
{
a[n]=item;
n++;
}
public void display()
{int i;
for(i=0;i
{
System.out.print(a[i]+"\t");
}
}
int partition( int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = a[(left + right) / 2];
while (i <= j) {
while (a[i] < pivot)
i++;
while (a[j] > pivot)
j--;
if (i <= j) {
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
i++;
j--;
}
};
return i;
}
void quickSort( int left, int right) {
int index = partition( left, right);
if (left < index - 1)
quickSort( left, index - 1);
if (index < right)
quickSort( index, right);
}
}
No comments:
Post a Comment