Quick Sort


  • According to me, it is the easiest and one of the most important and efficient sorting algorithm.
  • Java 7 also uses quicksort for their .sort() method
  • Time Complexity - O(nlogn)
  • Space Complexity - O(n) -> As we are using recursion, it uses the internal stack
  • In quicksort, we chose a pivot element first (in this program, I chose the last element as pivot)
  • All the elements in any array, which are less than the pivot remains to the left of pivot and all the elements which are greater than the pivot element remains to the right of pivot.
  • To do the above, we first chose last element as pivot element, i as (start-1) and j as start
  • In quicksort, we only have to take care of pivot, i and j
  • Now, we compare if a[i]<= pivot element. If yes, we increment i and swap arr[i] and arr[j]
  • After completing one iteration from start to end, we get a pivot element fixed at its right position.
  • Now, we recursively do this for the left subarray and right subarray to the pivot element.
  • QuickSort is also a divide and conquer algorithm
  • The disadvantage of QuickSort is that it is an unstable sort, which means if two elements have the same value then their ordering is not guaranteed in the sorted array. 
    • For example - Consider unsorted array {1,4,4,5,0,21,3}
    • Let's name the 4 at index 1 - Max and 4 at index 2 - Ghost
    • So, in the above array, as we can see Max is before Ghost
    • Now, if we sort the above array using quicksort, we get - {0,1,3,4,4,5,21,3}
    • It is possible in quicksort, that the ordering in the sorted array is not maintained. So, it might be possible that Ghost comes before Max in the sorted array. 
__________________________________________________________________________________________________________________________________________________________________



public class Main {
   
    public static void quickSort(int[] arr, int start, int end){
        if(start<end){
            int pivot = partition(arr,start,end);
            quickSort(arr,start,pivot-1);
            quickSort(arr,pivot+1,end);
        }
    }
   
    public static int partition(int[] arr, int start, int end){
        int pivot = end;
        // int i = start;
        int i = start-1;
       
        for(int j=start;j<arr.length;j++){
            if(arr[j]<=arr[pivot]){
                // we swap i and j but after incrementing i
                i++;
               
                // now swap arr[i] and arr[j]
                int temp = arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }
        return i;
    }
    public static void main(String[] args) throws Exception {
     
        int[] arr={1,4,4,5,6,2,3,90,120,43,0};
        System.out.println("UnSorted Array :");
        for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
        System.out.println();
       
     
        quickSort(arr,0,arr.length-1);
     
       
        System.out.println();
        System.out.println("Sorted Array in increasing order:");
        for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
    }
}

Output:

UnSorted Array :
1 4 4 5 6 2 3 90 120 43 0 

Sorted Array in increasing order:
0 1 2 3 4 4 5 6 43 90 120

Comments

Popular posts from this blog

Binary Heap - Introduction

Divide and Conquer - Introduction

Bubble Sort