• Quick sort uses divide and conquer algorithm.
  • First will be divided in to two parts based on some pivot element. All elements which are less than pivot element will be placed left and and all elements which are greater than pivot will be in right part.

  • So now pivot element is exactly in middle. if we sort left and right side elements all elements will be sorted. Here recursive quick sort will take place in order to sort all elements.
  • Quick sort will be sorting these elements without using extra space that is the reason it is called in place sorting algorithm.
  • Using the first or middle elements as an pivot element. it splits the arrays by re arranging the elements like every thing that is less than pivot will come left side and all elements greater than or equal to pivot will come right side.
  • Now pivot is in middle and correct place it left and right arrays sorted then entire array will be sorted.
  • Quick sort exactly will do the same it will sort left and right recursively.
  • If size of input is very  less merge sort will be time consuming.
  • For smaller inputs quick sort is faster than merge sort.
  • Time complexity of Quicksort  default case is O(n log n).
  • Worst case Time complexity of  Quicksort  is O(n2).
Steps to implement quick sort algorithm: 

  • Create a array with some elements. Choose pivot element as middle element. we can choose first or last also.
  • After choosing pivot element arrange the elements like all elements which are less than pivot value comes left side and elements greater than equal to pivot come right side of pivot.
  • And then apply same to both sides. until it becomes one. then all elements in array will be sorted.

 Program #1: Java Example program to sort elements of array using quick sort algorithm:

  1. package quicksortjava;

  2. public class QuickSort {
  3.  
  4.     private int array[];
  5.     private int length;
  6.  
  7. public void sortElements(int[] arrayvalues) {
  8.          
  9.         if (arrayvalues == null || arrayvalues.length == 0) {
  10.             return;
  11.         }
  12.         this.array = arrayvalues;
  13.         length = arrayvalues.length;
  14.         doQuickSort(0, length - 1);
  15. }
  16.  
  17.   private void doQuickSort(int lowIndex, int highIndex) {
  18.          
  19.         int i = lowIndex;
  20.         int j = highIndex;
  21.         
  22.         int pivot = array[lowIndex+(highIndex-lowIndex)/2];
  23.         
  24.         // now Divide the array into two arrays(actually we are maintaining single array only)
  25.         while (i <= j) {
  26.        
  27.             while (array[i] < pivot) {
  28.                 i++;
  29.                 
  30.             }
  31.             while (array[j] > pivot) {
  32.                 j--;
  33.             }
  34.             if (i <= j) {
  35.                 swapElements(i, j);
  36.                
  37.                 //move index to next position on both sides
  38.                
  39.                 i++;
  40.                 j--;
  41.                 
  42.                 
  43.             }
  44.         }
  45.         
  46.         // call quickSort() method recursively
  47.         if (lowIndex < j){
  48.        
  49.         doQuickSort(lowIndex, j);
  50.         }
  51.         if (i < highIndex){
  52.        
  53.         doQuickSort(i, highIndex);
  54.             
  55.         }
  56.     }
  57.  
  58.     
  59.      
  60. private void swapElements(int i, int j) {

  61.         int temp = array[i];
  62.         array[i] = array[j];
  63.         array[j] = temp;

  64.  }
  65.      
  66.  public static void main(String a[]){
  67.          
  68.     QuickSort quicksort = new QuickSort();
  69.         int[] inputarray = {32,1,23,14,43,7,6,65};
  70.         
  71.         System.out.println("Before sorting");
  72.         for(int i:inputarray){
  73.             System.out.print(i);
  74.             System.out.print(" ");
  75.         }
  76.        
  77.  quicksort.sortElements(inputarray);

  78.         System.out.println("After sorting");
  79.         for(int i:inputarray){
  80.             System.out.print(i);
  81.             System.out.print(" ");
  82.         }
  83.     }

  84. }
 Output:

  1. Before sorting
  2. 32 1 23 14 43 7 6 65 
  3. After sorting
  4. 1 6 7 14 23 32 43 65 

Execution flow explanation of quick sort algorithm

quick sort program in java using recursion program

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu