Java Program for Iterative Quick Sort

JavaObject Oriented ProgrammingProgramming

Following is the Java program for Iterative Quick Sort −

Example

 Live Demo

public class Demo{
   void swap_vals(int arr[], int i, int j){
      int temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
   }
   int partition(int arr[], int l, int h){
      int x = arr[h];
      int i = (l - 1);
      for (int j = l; j <= h - 1; j++){
         if (arr[j] <= x){
            i++;
            swap_vals(arr, i, j);
         }
      }
      swap_vals(arr, i + 1, h);
      return (i + 1);
   }
   void quick_sort(int arr[], int l, int h){
      int my_list[] = new int[h - l + 1];
      int top = -1;
      my_list[++top] = l;
      my_list[++top] = h;
      while (top >= 0){
         h = my_list[top--];
         l = my_list[top--];
         int p = partition(arr, l, h);
         if (p - 1 > l){
            my_list[++top] = l;
            my_list[++top] = p - 1;
         }  
         if (p + 1 < h){
            my_list[++top] = p + 1;
            my_list[++top] = h;
         }
      }
   }
   public static void main(String args[]){
      Demo my_ob = new Demo();
      int my_arr[] = { 34, 76, 41, 32, 11, 0 , 91, 102, -11};
      my_ob.quick_sort(my_arr, 0, my_arr.length - 1);
      int i;
      System.out.println("After iteratively performing quick sort, the array is ");
      for (i = 0; i < my_arr.length; ++i)
      System.out.print(my_arr[i] + " ");
   }
}

Output

After iteratively performing quick sort, the array is
-11 0 11 32 34 41 76 91 102

A class named Demo contains 3 functions, ‘swap_vals’ that is used to swap the values using a temporary variable, a ‘partition’ function that divides the array into two halves based on a pivot value, and the ‘quick_sort’ function that uses a pivot value and based on this value, sorts the values in the array.

In the main function, an instance of the Demo class is created along with an array. The ‘quick_sort’ function is called on this array and the output is displayed on the console.

raja
Published on 07-Jul-2020 11:42:23
Advertisements