Java Program to Perform Quick Sort on Large Number of Elements

«
»
This is a java program to sort the large number of elements using Quick Sort Technique. Quick sort uses a pivot element, where all the elements less that pivot are kept in one list and all the elements greater than pivot are kept in another list, and so on.

Here is the source code of the Java Program to Perform Quick Sort on Large Number of Elements. 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 large number of element using Quick Sort
  2. import java.util.Random;
  3.  
  4. public class Quick_Sort 
  5. {
  6.     public static int N = 25;
  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.             int pivot = sequence[right];
  16.             int partition = partitionIt(left, right, pivot);
  17.             QuickSort(left, partition - 1);
  18.             QuickSort(partition + 1, right);
  19.         }
  20.     }
  21.  
  22.     public static int partitionIt(int left, int right, long pivot) 
  23.     {
  24.         int leftPtr = left - 1;
  25.         int rightPtr = right;
  26.         while (true) 
  27.         {
  28.             while (sequence[++leftPtr] < pivot)
  29.                 ;
  30.             while (rightPtr > 0 && sequence[--rightPtr] > pivot)
  31.                 ;
  32.  
  33.             if (leftPtr >= rightPtr)
  34.                 break;
  35.             else
  36.                 swap(leftPtr, rightPtr);
  37.         }
  38.         swap(leftPtr, right);
  39.         return leftPtr;
  40.     }
  41.  
  42.     public static void swap(int dex1, int dex2) 
  43.     {
  44.         int temp = sequence[dex1];
  45.         sequence[dex1] = sequence[dex2];
  46.         sequence[dex2] = temp;
  47.     }
  48.  
  49.     static void printSequence(int[] sorted_sequence) 
  50.     {
  51.         for (int i = 0; i < sorted_sequence.length; i++)
  52.             System.out.print(sorted_sequence[i] + " ");
  53.     }
  54.  
  55.     public static void main(String args[]) 
  56.     {
  57.         System.out
  58.                 .println("Sorting of randomly generated numbers using QUICK SORT");
  59.         Random random = new Random();
  60.  
  61.         for (int i = 0; i < N; i++)
  62.             sequence[i] = Math.abs(random.nextInt(100));
  63.  
  64.         System.out.println("\nOriginal Sequence: ");
  65.         printSequence(sequence);
  66.         System.out.println("\nSorted Sequence: ");
  67.         QuickSort(0, N - 1);
  68.         printSequence(sequence);
  69.  
  70.     }
  71.  
  72. }

Output:

advertisement
$ javac Quick_Sort.java
$ java Quick_Sort
 
Sorting of randomly generated numbers using QUICK SORT
 
Original Sequence: 
54 22 88 52 43 84 61 75 54 72 7 42 47 15 40 16 46 28 9 48 78 10 89 95 8 
Sorted Sequence: 
7 8 9 10 15 16 22 28 40 42 43 46 47 48 52 54 54 61 72 75 78 84 88 89 95

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
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.