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.
//This is a java program to sort the numbers using Shaker Sort import java.util.Random; public class Shaker_Sort { public static void printSequence(int[] sorted_sequence) { for (int i = 0; i < sorted_sequence.length; i++) System.out.print(sorted_sequence[i] + " "); } public static int[] shakerSort(int[] array) { for (int i = 0; i < array.length/2; i++) { boolean swapped = false; for (int j = i; j < array.length - i - 1; j++) { if (array[j] < array[j+1]) { int tmp = array[j]; array[j] = array[j+1]; array[j+1] = tmp; } } for (int j = array.length - 2 - i; j > i; j--) { if (array[j] > array[j-1]) { int tmp = array[j]; array[j] = array[j-1]; array[j-1] = tmp; swapped = true; } } if(!swapped) break; } return array; } public static void main(String args[]) { System.out .println("Sorting of randomly generated numbers using Shaker SORT"); Random random = new Random(); int N = 20; int[] sequence = new int[N]; for (int i = 0; i < N; i++) sequence[i] = Math.abs(random.nextInt(100)); System.out.println("\nOriginal Sequence: "); printSequence(sequence); System.out.println("\nSorted Sequence: "); printSequence(shakerSort(sequence)); } }
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
Related posts:
Quick Guide on Loading Initial Data with Spring Boot
Java Program to Implement Quick Sort with Given Complexity Constraint
Adding Shutdown Hooks for JVM Applications
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Spring Security Remember Me
Checking for Empty or Blank Strings in Java
Daemon Threads in Java
Java Program to Implement Double Ended Queue
Logout in an OAuth Secured Application
New Features in Java 14
Java Stream Filter with Lambda Expression
Lập trình đa luồng với Callable và Future trong Java
Spring Boot - Scheduling
Java Program to Implement Expression Tree
Java Program to Implement Stack using Two Queues
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Spring Web Annotations
Lớp LinkedHashMap trong Java
Spring Security – security none, filters none, access permitAll
A Guide to EnumMap
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Java TreeMap vs HashMap
Java Program to Implement Johnson’s Algorithm
Entity To DTO Conversion for a Spring REST API
Getting Started with Stream Processing with Spring Cloud Data Flow
How to Get the Last Element of a Stream in Java?
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Lập trình hướng đối tượng (OOPs) trong java
HashSet trong java
Guide to Escaping Characters in Java RegExps