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:
Registration with Spring Security – Password Encoding
Java Program to Optimize Wire Length in Electrical Circuit
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Template Engines for Spring
Java Program to Represent Graph Using Adjacency List
Java Program to Implement Iterative Deepening
Java Program to Perform the Sorting Using Counting Sort
Java Program to Check whether Undirected Graph is Connected using DFS
Hướng dẫn Java Design Pattern – Dependency Injection
JUnit 5 for Kotlin Developers
Java Program to Implement Park-Miller Random Number Generation Algorithm
Java Program to Search for an Element in a Binary Search Tree
Mệnh đề Switch-case trong java
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists
Collect a Java Stream to an Immutable Collection
Sắp xếp trong Java 8
Read an Outlook MSG file
Concatenating Strings In Java
Spring NoSuchBeanDefinitionException
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Java Program to Implement Extended Euclid Algorithm
Java Program to Implement Unrolled Linked List
Spring Security Login Page with React
Java Program to Implement Queue using Two Stacks
Java Program to Implement Regular Falsi Algorithm
Marker Interface trong Java
Java Program to Find Number of Articulation points in a Graph
Java Program to Implement LinkedHashMap API
How to Replace Many if Statements in Java
Lập trình hướng đối tượng (OOPs) trong java
Spring Boot - Web Socket