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:
Check if there is mail waiting
New Stream Collectors in Java 9
Merging Two Maps with Java 8
Introduction to Spring Data JDBC
Spring Boot - Application Properties
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Zipping Collections in Java
HashSet trong java
Using JWT with Spring Security OAuth
Java Program to Implement Hopcroft Algorithm
A Guide to TreeMap in Java
Lập trình đa luồng trong Java (Java Multi-threading)
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Java Program to Implement Unrolled Linked List
Tính kế thừa (Inheritance) trong java
How to Manually Authenticate User with Spring Security
Java Program to Use rand and srand Functions
LIKE Queries in Spring JPA Repositories
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Java Program to implement Priority Queue
Java Program to Implement Horner Algorithm
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Control Structures in Java
Spring Security Remember Me
Java Program to Perform Searching Using Self-Organizing Lists
Java Program to Find Minimum Element in an Array using Linear Search
Java Program to Check whether Graph is a Bipartite using DFS
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Partition a List in Java
A Quick Guide to Spring Cloud Consul
Java Program to Implement Gale Shapley Algorithm
Java Program to Implement Red Black Tree