Java Program to Perform the Shaker Sort

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 Check if a Directed Graph is a Tree or Not Using DFS
The Dining Philosophers Problem in Java
Consuming RESTful Web Services
Guide to ThreadLocalRandom in Java
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Guide To CompletableFuture
Giới thiệu SOAP UI và thực hiện test Web Service
Spring Boot - Flyway Database
Convert Hex to ASCII in Java
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Spring Security – Reset Your Password
Java Program to Implement Threaded Binary Tree
Abstract class và Interface trong Java
Java Program to Perform Finite State Automaton based Search
Java Program to Implement LinkedHashMap API
Introduction to Spring Data JDBC
Hướng dẫn Java Design Pattern – Decorator
An Intro to Spring Cloud Vault
Sắp xếp trong Java 8
Primitive Type Streams in Java 8
Spring Security – security none, filters none, access permitAll
Spring Security OAuth2 – Simple Token Revocation
Hamcrest Collections Cookbook
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Spring Boot - Admin Server
Java Program to Implement LinkedHashSet API
CyclicBarrier in Java
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Spring Security Remember Me