This is the quick sort program in java language.
We sort the array
A = (38 81 22 48 13 69 93 14 45 58 79 72)
with quicksort, always choosing the pivot element to be the element in position (left+right)/2. The partitioning during the top-level call to quicksort() is illustrated on the next page. During the partitioning process,
i) Elements strictly to the left of position lo are less than or equivalent to the pivot element (69).
ii) Elements strictly to the right of position hi are greater than the pivot element.
When lo and hi cross, we are done. The final value of hi is the position in which the partitioning element ends up. An asterisk indicates an element compared to the pivot element at this step.
A = (38 81 22 48 13 69 93 14 45 58 79 72)
with quicksort, always choosing the pivot element to be the element in position (left+right)/2. The partitioning during the top-level call to quicksort() is illustrated on the next page. During the partitioning process,
i) Elements strictly to the left of position lo are less than or equivalent to the pivot element (69).
ii) Elements strictly to the right of position hi are greater than the pivot element.
When lo and hi cross, we are done. The final value of hi is the position in which the partitioning element ends up. An asterisk indicates an element compared to the pivot element at this step.
Quicksort Program:
/* write quick sort program in java language */
public class QuickSort
{
public static void main(String a[])
{
int i;
int elements[] = {14,7,8,98,43,1,3,75,13};
System.out.println("Values Before the sorting:\n");
for(i = 0; i < elements.length; i++)
System.out.print( elements[i]+" ");
System.out.println();
quick_srt(elements,0,elements.length-1);
System.out.print("Values after the sorting:\n");
for(i = 0; i <elements.length; i++)
System.out.print(elements[i]+" ");
System.out.println();
}
public static void quick_srt(int array[],int low, int n)
{
int lo = low;
int hi = n;
if (lo >= n)
{
return;
}
int mid = array[(lo + hi) / 2];
while (lo < hi)
{
while (lo<hi && array[lo] < mid)
{
lo++;
}
while (lo<hi && array[hi] > mid)
{
hi--;
}
if (lo < hi)
{
int T = array[lo];
array[lo] = array[hi];
array[hi] = T;
}
}
if (hi < lo)
{
int T = hi;
hi = lo;
lo = T;
}
quick_srt(array, low, lo);
quick_srt(array, lo == low ? lo+1 : lo, n);
}
}
Output: