Sorting Algorithms in Data Structure:
Quick Sort Algorithm Java code:
Operations:
1.
Swap()
2.
Partition()
3.
Quicksort()
4.
printArray()
Program Code:
public class quicksort
{
static void swap( int []arr ,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
static int partition (int []arr, int start, int end){
int pivot=arr[end];
int i=(start -1);
for (int j=start ;j<=end-1;j++){
if (arr[j]<pivot){
i++;
swap(arr , i, j);
}
}
swap(arr ,i+1,end) ;
return( i+1);
}
static void quicksort ( int [] arr ,int start,int end)
{
if (start < end){
int pi=partition(arr,start,end);
quicksort(arr,start , pi -1);
quicksort(arr, pi+1,end);
}
}
static void printArray(int[] arr, int size)
{
for (int i = 0; i < size; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
public static void main(String[] args)
{
System.out.println("\t\t....Created by
Jhangir Mughal....");
System.out.println("\n\t\t\t***Quick
Sort***");
int[] arr = { 3,4,6,1,9,7};
int n = arr.length;
System.out.println("UnSorted array:");
printArray(arr, n);
quicksort(arr, 0, n - 1);
System.out.println("\nSorted array: ");
printArray(arr, n);
}
}