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:

Spring Security Remember Me
Getting Started with GraphQL and Spring Boot
Java Program to Implement the Monoalphabetic Cypher
Java Program to Perform Postorder Recursive Traversal of a Given Binary Tree
Java Program to Implement WeakHashMap API
Spring Security OAuth2 – Simple Token Revocation
Introduction to Spring Cloud Netflix – Eureka
Tiêu chuẩn coding trong Java (Coding Standards)
Java NIO2 Path API
Java Program to Find a Good Feedback Edge Set in a Graph
Java Program to Implement Nth Root Algorithm
Spring Boot Gradle Plugin
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Java Program to Construct an Expression Tree for an Infix Expression
Guide to ThreadLocalRandom in Java
Java Program to Implement Fibonacci Heap
How to Round a Number to N Decimal Places in Java
Cachable Static Assets with Spring MVC
Java Program to Perform the Sorting Using Counting Sort
Spring WebFlux Filters
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Query Entities by Dates and Times with Spring Data JPA
Number Formatting in Java
Spring’s RequestBody and ResponseBody Annotations
Hướng dẫn sử dụng Printing Service trong Java
Java Program to Implement LinkedBlockingDeque API
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Java Program to Implement Suffix Array
Runnable vs. Callable in Java
Java Program to Implement the RSA Algorithm
Serialize Only Fields that meet a Custom Criteria with Jackson