Write a program on Quick Sort in Java Programming ?

Introduction :

Hii all, here we are going to code one of the most popular and interesting code for sorting i.e. Quick Sort.Here we are going to arrange our data in ascending order.The strategy to fix the code is, we are taking initial position as “pos” and the integer place at the 0th position of an array pass by us is known as “pivot” . we consider pivot as the minimum number of an array and then we started comparing the integers with there upcoming integer and as we find the minimum integer in the rest array we comapre it to the pivot, if the integer at a[pos] is lesser than the pivot then we interchange the position of a[pos] and pivot.

Description Of Program :

First of all, I would like to tell you we are constructing our product using functions, initially we will check whether the left integer is greater than the right integer if yes, then we will return from thet position only.Moving forward initially in the introduction part we have already discuss the strategies.Again we are calling two functions and that is, if the entered data is greater that the a[pos] then the left position will move to pos+1 position and if less than the right position will move pos-1.Then at the main fuction we pass an array again 0th position is consider as the 0 and last integer’s position is a.lenght-1.Hope you find it helpful.

Thank You!

CODE :

package arrays;

public class QuickSort {
static void quickSort(int a[],int left,int right){
if(left>=right)
return;
int pivot=a[left];
int pos=left;
int t;
for(int i=left+1;i<=right;i++)
{
if(a[i]>=pivot)
continue;
pos++;
t=a[i];
a[i]=a[pos];
a[pos]=t;
}
t=a[left];
a[left]=a[pos];
a[pos]=t;
quickSort(a, left, pos-1);
quickSort(a, pos+1, right);
System.out.printf(“left=%s, pos=%s, right=%s\n”,left,pos,right);
}
public static void main(String[] args) {
int a[]={10,15,5,66,5,44};
quickSort(a, 0, a.length-1);
for(int i=0;i<=a.length-1;i++)
System.out.print(a[i] + “,”);
System.out.println();
}
}

OUTPUT :

Leave a Reply