Java Program to Implement Quick Sort Using Randomization

«
»
This is a java program to perform sorting using Randomized Quick Sort. Randomized Quick Sort randomly selects a pivot element, after selecting pivot standard procedure is to be followed as quick sort.

Here is the source code of the Java Program to Implement Quick Sort Using Randomization. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to sort numbers using randomized quick sort
  2. import java.util.Random;
  3.  
  4. public class Randomized_Quick_Sort 
  5. {
  6.     public static int N = 20;
  7.     public static int[] sequence = new int[N];
  8.  
  9.     public static void QuickSort(int left, int right) 
  10.     {
  11.         if (right - left <= 0)
  12.             return;
  13.         else 
  14.         {
  15.             Random rand = new Random();
  16.             int pivotIndex = left + rand.nextInt(right - left + 1);
  17.             swap(pivotIndex, right);
  18.  
  19.             int pivot = sequence[right];
  20.             int partition = partitionIt(left, right, pivot);
  21.             QuickSort(left, partition - 1);
  22.             QuickSort(partition + 1, right);
  23.         }
  24.     }
  25.  
  26.     public static int partitionIt(int left, int right, long pivot) 
  27.     {
  28.         int leftPtr = left - 1;
  29.         int rightPtr = right;
  30.         while (true) 
  31.         {
  32.             while (sequence[++leftPtr] < pivot)
  33.                 ;
  34.             while (rightPtr > 0 && sequence[--rightPtr] > pivot)
  35.                 ;
  36.  
  37.             if (leftPtr >= rightPtr)
  38.                 break;
  39.             else
  40.                 swap(leftPtr, rightPtr);
  41.         }
  42.         swap(leftPtr, right);
  43.         return leftPtr;
  44.     }
  45.  
  46.     public static void swap(int dex1, int dex2) 
  47.     {
  48.         int temp = sequence[dex1];
  49.         sequence[dex1] = sequence[dex2];
  50.         sequence[dex2] = temp;
  51.     }
  52.  
  53.     static void printSequence(int[] sorted_sequence) 
  54.     {
  55.         for (int i = 0; i < sorted_sequence.length; i++)
  56.             System.out.print(sorted_sequence[i] + " ");
  57.     }
  58.  
  59.     public static void main(String args[]) 
  60.     {
  61.         System.out
  62.                 .println("Sorting of randomly generated numbers using RANDOMIZED QUICK SORT");
  63.         Random random = new Random();
  64.  
  65.         for (int i = 0; i < N; i++)
  66.             sequence[i] = Math.abs(random.nextInt(100));
  67.  
  68.         System.out.println("\nOriginal Sequence: ");
  69.         printSequence(sequence);
  70.         System.out.println("\nSorted Sequence: ");
  71.         QuickSort(0, N - 1);
  72.         printSequence(sequence);
  73.     }
  74. }

Output:

advertisement
$ javac Randomized_Quick_Sort.java
$ java Randomized_Quick_Sort
 
Sorting of randomly generated numbers using RANDOMIZED QUICK SORT
 
Original Sequence: 
98 95 22 64 77 49 11 98 56 63 84 18 9 68 4 69 2 20 68 4 
Sorted Sequence: 
2 4 4 9 11 18 20 22 49 56 63 64 68 68 69 77 84 95 98 98

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.