Java Program to Perform Stooge Sort

This is a java program to implement Stooge sort algorithm.

Here is the source code of the Java Program to Perform Stooge 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 numbers using Stooge Sort
import java.util.Random;
 
public class Stooge_Sort 
{
 
    public static int N = 20;
    public static int[] sequence = new int[N];
 
    public static int[] stoogeSort(int[] L, int i, int j) 
    {
        if (L[j] < L[i]) 
        {
            int swap = L[i];
            L[i] = L[j];
            L[j] = swap;
        }
        if ((j - i + 1) >= 3) 
        {
            int t = (j - i + 1) / 3;
            stoogeSort(L, i, j - t);
            stoogeSort(L, i + t, j);
            stoogeSort(L, i, j - t);
        }
        return L;
    }
 
    public static void printSequence(int[] sorted_sequence) 
    {
        for (int i = 0; i < sorted_sequence.length; i++)
            System.out.print(sorted_sequence[i] + " ");
    }
 
    public static void main(String[] args) 
    {
        Random random = new Random();
        System.out
                .println("Sorting of randomly generated numbers using STOOGE SORT");
 
        for (int i = 0; i < N; i++)
            sequence[i] = Math.abs(random.nextInt(1000));
 
        System.out.println("\nOriginal Sequence: ");
        printSequence(sequence);
 
        System.out.println("\nSorted Sequence: ");
        printSequence(stoogeSort(sequence, 0, sequence.length - 1));
    }
}

Output:

$ javac Stooge_Sort.java
$ java Stooge_Sort
 
Sorting of randomly generated numbers using STOOGE SORT
 
Original Sequence: 
213 931 260 34 184 706 346 849 279 918 781 242 995 2 187 378 634 965 138 843 
Sorted Sequence: 
2 34 138 184 187 213 242 260 279 346 378 634 706 781 843 849 918 931 965 995

Related posts:

Guide to CopyOnWriteArrayList
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Create a Custom Exception in Java
Java Program to Find kth Largest Element in a Sequence
Java Program to Describe the Representation of Graph using Incidence List
New Features in Java 15
String Initialization in Java
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Thao tác với tập tin và thư mục trong Java
Spring @RequestMapping New Shortcut Annotations
Đồng bộ hóa các luồng trong Java
Composition, Aggregation, and Association in Java
Java Program to do a Depth First Search/Traversal on a graph non-recursively
Join and Split Arrays and Collections in Java
ClassNotFoundException vs NoClassDefFoundError
OAuth2.0 and Dynamic Client Registration
Java Program to implement Bit Matrix
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Java Program to Implement Adjacency Matrix
Predicate trong Java 8
Spring Boot - Twilio
Java Program to Implement Segment Tree
New Features in Java 14
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Hướng dẫn sử dụng Java Annotation
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Perform Insertion in a BST
Spring Boot - Logging
Java Program to Perform Searching Based on Locality of Reference
Guide to the Java TransferQueue
Java Program to Find Number of Articulation points in a Graph