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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | //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:
1 2 3 4 5 6 7 8 9 | $ 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 Implement Rope
Lớp Collectors trong Java 8
Quick Guide on Loading Initial Data with Spring Boot
Generating Random Numbers in a Range in Java
Guide to DelayQueue
Weak References in Java
Java Program to Permute All Letters of an Input String
Java Program to Implement ArrayDeque API
Java – String to Reader
Java Program to Find Transitive Closure of a Graph
Exploring the New Spring Cloud Gateway
Tạo chương trình Java đầu tiên sử dụng Eclipse
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Service Registration with Eureka
Registration – Activate a New Account by Email
Filtering a Stream of Optionals in Java
Java Program to Implement Nth Root Algorithm
Spring Security Authentication Provider
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Convert a Decimal Number to Binary Number using Stacks
A Custom Data Binder in Spring MVC
A Guide to the Java ExecutorService
Primitive Type Streams in Java 8
Java Program to Implement Shunting Yard Algorithm
Java 8 Stream findFirst() vs. findAny()
Java Program to Implement Kosaraju Algorithm
LinkedHashSet trong Java hoạt động như thế nào?
Java Program to Implement Singly Linked List
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Refactoring Design Pattern với tính năng mới trong Java 8
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
A Guide to JUnit 5