The Artima Developer Community
Sponsored Link

Java Buzz Forum
Quicksort algorithm in java with example program

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Quicksort algorithm in java with example program Posted: Aug 16, 2016 12:44 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Quicksort algorithm in java with example program
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • 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.

  • After than these two lists will be sorted in same manner until became one.
  • 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.png



Quicksort implementation java:
  • Quicksort example step by step in java

32 1 23 14 43 7 6 65
lowerIndex= 0 higherIndex=7
i=0 j=7
pivote= 14
array[j] > pivot  (6 > 14 )  so decrementing j
i=0 j=6
i<=j (0 <= 6 )  so swap  array[i] and array[j] (32 > 6 )
6 1 23 14 43 7 32 65 i=1 j=5
array[i] < pivot (23 < 14 ) so incrementing i
i=2 j=5
i<=j (2 <= 5 )  so swap  array[i] and array[j] (23 > 7 )
6 1 7 14 43 23 32 65 i=3 j=4
array[j] > pivot  (14 > 14 )  so decrementing j
i=3 j=3
i<=j (3 <= 3 )  so swap  array[i] and array[j] (14 > 14 )
6 1 7 14 43 23 32 65 i=4 j=2
6 1 7 14 43 23 32 65
lowerIndex < j  (0 < 2 )   call quickSort(lowerIndex, j)  quickSort (0 , 2 )

6 1 7 14 43 23 32 65 

lowerIndex= 0 higherIndex=2
i=0 j=2
pivote= 1
array[j] > pivot  (7 > 1 )  so decrementing j
i=0 j=1
i<=j (0 <= 1 )  so swap  array[i] and array[j] (6 > 1 )
1 6 7 14 43 23 32 65 i=1 j=0
1 6 7 14 43 23 32 65
i < higherIndex  (1 < 2 )   call quickSort(i, higherIndex);  quickSort (1 , 2 )

1 6 7 14 43 23 32 65 

lowerIndex= 1 higherIndex=2
i=1 j=2
pivote= 6
array[j] > pivot  (7 > 6 )  so decrementing j
i=1 j=1
i<=j (1 <= 1 )  so swap  array[i] and array[j] (6 > 6 )
1 6 7 14 43 23 32 65 i=2 j=0
1 6 7 14 43 23 32 65
i < higherIndex  (4 < 7 )   call quickSort(i, higherIndex);  quickSort (4 , 7 )
1 6 7 14 43 23 32 65
lowerIndex= 4 higherIndex=7
i=4 j=7
pivote= 23
array[j] > pivot  (65 > 23 )  so decrementing j
i=4 j=6
array[j] > pivot  (65 > 23 )  so decrementing j
i=4 j=5
i<=j (4 <= 5 )  so swap  array[i] and array[j] (43 > 23 )
1 6 7 14 23 43 32 65 i=5 j=4
1 6 7 14 23 43 32 65
i < higherIndex  (5 < 7 )   call quickSort(i, higherIndex);  quickSort (5 , 7 )

1 6 7 14 23 43 32 65 

lowerIndex= 5 higherIndex=7
i=5 j=7
pivote= 32
array[j] > pivot  (65 > 32 )  so decrementing j
i=5 j=6
i<=j (5 <= 6 )  so swap  array[i] and array[j] (43 > 32 )
1 6 7 14 23 32 43 65 i=6 j=5
1 6 7 14 23 32 43 65
i < higherIndex  (6 < 7 )   call quickSort(i, higherIndex);  quickSort (6 , 7 )
1 6 7 14 23 32 43 65
lowerIndex= 6 higherIndex=7
i=6 j=7
pivote= 43
array[j] > pivot  (65 > 43 )  so decrementing j
i=6 j=6
i<=j (6 <= 6 )  so swap  array[i] and array[j] (43 > 43 )
1 6 7 14 23 32 43 65 i=7 j=5

1 6 7 14 23 32 43 65

Read: Quicksort algorithm in java with example program

Topic: Java Tutorial : Java IO (InputStreamReader) Previous Topic   Next Topic Topic: 36% off KDLINKS X1 Full-HD 1920x1080 Wide Angle Car Dashboard Cam with GPS - Deal Alert

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use