
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program for Stooge Sort
Following is the Java program for Stooge sort −
Example
import java.io.*; public class Demo { static void stooge_sort(int my_arr[], int l_val, int h_val){ if (l_val >= h_val) return; if (my_arr[l_val] > my_arr[h_val]){ int temp = my_arr[l_val]; my_arr[l_val] = my_arr[h_val]; my_arr[h_val] = temp; } if (h_val-l_val+1 > 2){ int temp = (h_val-l_val+1) / 3; stooge_sort(my_arr, l_val, h_val-temp); stooge_sort(my_arr, l_val+temp, h_val); stooge_sort(my_arr, l_val, h_val-temp); } } public static void main(String args[]){ int my_arr[] = {12, 34, 67, 91, 11, 0, 89, 102, 39}; int n = my_arr.length; stooge_sort(my_arr, 0, n-1); System.out.println("The array after performing stooge sort is "); for (int i=0; i < n; i++) System.out.print(my_arr[i] + " "); } }
Output
The array after performing stooge sort is 0 11 12 34 39 67 89 91 102
A class named Demo contains a function named ‘stooge_sort’ that takes the array, left, height and right values as parameters. If the left value is greater than right value, nothing is returned. If the left value in the array is greater than right value of the array, a simple swapping is performed.
Based on the values of height, and left values, the ‘stooge_sort’ function is called by passing the left value, and height value. In the main function, the array is defined, and its length is stored in a value. The function is called by passing these values and the output is displayed on the console.
- Related Questions & Answers
- Python Program for Stooge Sort
- C++ Program to Perform Stooge Sort
- Java Program for Gnome Sort
- Java Program for Cocktail Sort
- Java Program for Counting Sort
- Java Program for Comb Sort
- Java Program for Bitonic Sort
- Java Program for Pigeonhole Sort
- Java Program for Radix Sort
- Java Program for Recursive Bubble Sort
- Java Program for Recursive Insertion Sort
- Java Program for Iterative Merge Sort
- Java Program for Iterative Quick Sort
- Java Program for Binary Insertion Sort
- 8085 program for bubble sort
Advertisements