Java Program to Perform Stooge Sort

«
»
This is a java program to implement Stooge sort algorithm.

Here is the source code of the Java Program to Perform Stooge Sort. 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 Stooge Sort
  2. import java.util.Random;
  3.  
  4. public class Stooge_Sort 
  5. {
  6.  
  7.     public static int N = 20;
  8.     public static int[] sequence = new int[N];
  9.  
  10.     public static int[] stoogeSort(int[] L, int i, int j) 
  11.     {
  12.         if (L[j] < L[i]) 
  13.         {
  14.             int swap = L[i];
  15.             L[i] = L[j];
  16.             L[j] = swap;
  17.         }
  18.         if ((j - i + 1) >= 3) 
  19.         {
  20.             int t = (j - i + 1) / 3;
  21.             stoogeSort(L, i, j - t);
  22.             stoogeSort(L, i + t, j);
  23.             stoogeSort(L, i, j - t);
  24.         }
  25.         return L;
  26.     }
  27.  
  28.     public static void printSequence(int[] sorted_sequence) 
  29.     {
  30.         for (int i = 0; i < sorted_sequence.length; i++)
  31.             System.out.print(sorted_sequence[i] + " ");
  32.     }
  33.  
  34.     public static void main(String[] args) 
  35.     {
  36.         Random random = new Random();
  37.         System.out
  38.                 .println("Sorting of randomly generated numbers using STOOGE SORT");
  39.  
  40.         for (int i = 0; i < N; i++)
  41.             sequence[i] = Math.abs(random.nextInt(1000));
  42.  
  43.         System.out.println("\nOriginal Sequence: ");
  44.         printSequence(sequence);
  45.  
  46.         System.out.println("\nSorted Sequence: ");
  47.         printSequence(stoogeSort(sequence, 0, sequence.length - 1));
  48.     }
  49. }

Output:

advertisement
$ javac Stooge_Sort.java
$ java Stooge_Sort
 
Sorting of randomly generated numbers using STOOGE SORT
 
Original Sequence: 
213 931 260 34 184 706 346 849 279 918 781 242 995 2 187 378 634 965 138 843 
Sorted Sequence: 
2 34 138 184 187 213 242 260 279 346 378 634 706 781 843 849 918 931 965 995

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

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

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter