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 8 Predicate Chain
Runnable vs. Callable in Java
Java InputStream to Byte Array and ByteBuffer
Java – Byte Array to Reader
Lập trình đa luồng với CompletableFuture trong Java 8
Spring 5 Testing with @EnabledIf Annotation
Java Program to Implement Heap Sort Using Library Functions
Constructor Dependency Injection in Spring
Java String to InputStream
Mockito and JUnit 5 – Using ExtendWith
Check If Two Lists are Equal in Java
Java Program to Implement Tarjan Algorithm
Java Program to Implement Sparse Matrix
Java Program to Perform LU Decomposition of any Matrix
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Spring MVC Setup with Kotlin
Spring RequestMapping
Spring WebFlux Filters
Java Program to Implement Leftist Heap
Java Program to Implement Sorted List
Java Program to Implement Circular Singly Linked List
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
REST Pagination in Spring
Handling URL Encoded Form Data in Spring REST
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to Implement the RSA Algorithm
Converting Between Byte Arrays and Hexadecimal Strings in Java
Quick Guide to Spring Bean Scopes
Introduction to the Functional Web Framework in Spring 5
Service Registration with Eureka
Immutable ArrayList in Java