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 Program to Generate Random Numbers Using Probability Distribution Function
Java Program to Implement LinkedBlockingQueue API
Guide to Spring @Autowired
Java Program to Implement AttributeList API
A Guide to Java 9 Modularity
Custom HTTP Header with the HttpClient
Zipping Collections in Java
Assert an Exception is Thrown in JUnit 4 and 5
Java 9 Stream API Improvements
Creating a Web Application with Spring 5
Java Program to implement Array Deque
Comparing Dates in Java
Java InputStream to String
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Java Program to Implement Find all Cross Edges in a Graph
Java Program to Implement Pagoda
OAuth2 for a Spring REST API – Handle the Refresh Token in AngularJS
Java Program to Perform Stooge Sort
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Running Spring Boot Applications With Minikube
Filtering a Stream of Optionals in Java
Java Program to add two large numbers using Linked List
Guide to the Fork/Join Framework in Java
Retrieve User Information in Spring Security
Java Program to Perform integer Partition for a Specific Case
Spring Boot Security Auto-Configuration
Java – Write an InputStream to a File
Guide to DelayQueue
Spring 5 and Servlet 4 – The PushBuilder
Java Program to Implement Suffix Tree
New Features in Java 13
Java Program to Implement Pairing Heap