# Java Program for Stooge Sort

Following is the Java program for Stooge sort −

## Example

Live Demo

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.