Java Program to Implement Shaker Sort

This is a java program to implement Shaker Sort algorithm.

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

Output:

$ javac Shaker_Sort.java
$ java Shaker_Sort
 
Sorting of randomly generated numbers using SHAKER SORT
 
Original Sequence: 
195 853 655 915 364 689 539 684 956 197 67 871 509 662 825 336 540 815 403 876 
Sorted Sequence: 
956 915 876 871 853 825 815 689 684 662 655 540 539 509 403 364 336 197 195 67

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

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

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.