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:
Java – File to Reader
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Spring Boot Annotations
Partition a List in Java
Spring Cloud – Bootstrapping
Java – Random Long, Float, Integer and Double
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Java Program to Implement AA Tree
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Introduction to Spring Cloud CLI
Serialization và Deserialization trong java
Java Program to Find Path Between Two Nodes in a Graph
Tính trừu tượng (Abstraction) trong Java
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Java Program to Implement Triply Linked List
Java Program to Perform Partial Key Search in a K-D Tree
OAuth2.0 and Dynamic Client Registration
Spring Security Form Login
Java Program to Implement ArrayDeque API
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
Java Program to Perform LU Decomposition of any Matrix
Java Program to Check whether Undirected Graph is Connected using DFS
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Lớp LinkedHashMap trong Java
Java Program to Implement Suffix Array
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Consuming RESTful Web Services
Java – Convert File to InputStream
Introduction to Thread Pools in Java
Spring Security 5 – OAuth2 Login
Spring Security Authentication Provider
Java IO vs NIO