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:
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Lập trình hướng đối tượng (OOPs) trong java
Java Program to Implement Shoelace Algorithm
Introduction to Spring Boot CLI
OAuth2 Remember Me with Refresh Token
Spring MVC and the @ModelAttribute Annotation
Java – Write a Reader to File
Toán tử instanceof trong java
JPA/Hibernate Persistence Context
Using Optional with Jackson
Stack Memory and Heap Space in Java
Introduction to Using Thymeleaf in Spring
A Guide to Java HashMap
REST Web service: Upload và Download file với Jersey 2.x
Properties with Spring and Spring Boot
Auditing with JPA, Hibernate, and Spring Data JPA
Configure a RestTemplate with RestTemplateBuilder
So sánh ArrayList và LinkedList trong Java
Spring Boot - Actuator
Serve Static Resources with Spring
Intro to the Jackson ObjectMapper
Giới thiệu JDBC Connection Pool
Guide to Guava Table
Java Program to find the number of occurrences of a given number using Binary Search approach
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Debug a HttpURLConnection problem
Java Program to Solve any Linear Equation in One Variable
Java Program to Find Number of Articulation points in a Graph
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Java Program to Implement Bresenham Line Algorithm
Spring Boot Security Auto-Configuration
Java – Convert File to InputStream