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:
A Quick Guide to Using Keycloak with Spring Boot
Collect a Java Stream to an Immutable Collection
Java Program to Find Median of Elements where Elements are Stored in 2 Different Arrays
HttpClient Connection Management
@Before vs @BeforeClass vs @BeforeEach vs @BeforeAll
Jackson Date
DynamoDB in a Spring Boot Application Using Spring Data
Spring Security Registration – Resend Verification Email
Hướng dẫn Java Design Pattern – Command
Spring Boot - Cloud Configuration Server
Allow user:password in URL
Optional trong Java 8
Java Program to Check the Connectivity of Graph Using DFS
Spring Boot - Bootstrapping
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Một số ký tự đặc biệt trong Java
Display Auto-Configuration Report in Spring Boot
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Using Spring ResponseEntity to Manipulate the HTTP Response
Java – Write to File
Disable Spring Data Auto Configuration
Java Program to Generate All Possible Combinations of a Given List of Numbers
Java Program to Implement the Monoalphabetic Cypher
Guide to ThreadLocalRandom in Java
Quick Guide to @RestClientTest in Spring Boot
List Interface trong Java
Java Program to Perform Deletion in a BST
Java Program to Implement Stack using Two Queues
ExecutorService – Waiting for Threads to Finish
Java CyclicBarrier vs CountDownLatch
Reactive WebSockets with Spring 5
Java Program to Implement LinkedList API