Related Articles

# Java Program for Stooge Sort

• Last Updated : 03 Apr, 2018

The Stooge sort is a recursive sorting algorithm. It is defined as below (for ascending order sorting).

```Step 1 : If value at index 0 is greater than
value at last index, swap them.
Step 2:  Recursively,
a) Stooge sort the initial 2/3rd of the array.
b) Stooge sort the last 2/3rd of the array.
c) Stooge sort the initial 2/3rd again to confirm.
```
 `// Java program to implement stooge sort``import` `java.io.*;`` ` `public` `class` `stooge``{``    ``// Function to implement stooge sort``    ``static` `void` `stoogesort(``int` `arr[], ``int` `l, ``int` `h)``    ``{``        ``if` `(l >= h)``           ``return``;`` ` `        ``// If first element is smaller``        ``// than last,swap them``        ``if` `(arr[l] > arr[h])``        ``{``            ``int` `t = arr[l];``            ``arr[l] = arr[h];``            ``arr[h] = t;``        ``}`` ` `        ``// If there are more than 2 elements in``        ``// the array``        ``if` `(h-l+``1` `> ``2``)``        ``{``            ``int` `t = (h-l+``1``) / ``3``;`` ` `            ``// Recursively sort first 2/3 elements``            ``stoogesort(arr, l, h-t);`` ` `            ``// Recursively sort last 2/3 elements``            ``stoogesort(arr, l+t, h);`` ` `            ``// Recursively sort first 2/3 elements``            ``// again to confirm``            ``stoogesort(arr, l, h-t);``        ``}``    ``}`` ` `    ``// Driver Code``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `arr[] = {``2``, ``4``, ``5``, ``3``, ``1``};``        ``int` `n = arr.length;`` ` `        ``stoogesort(arr, ``0``, n-``1``);`` ` `        ``for` `(``int` `i=``0``; i < n; i++)``             ``System.out.print(arr[i] + ``" "``);``    ``}``}``// Code Contributed by Mohit Gupta_OMG <(0_o)>`

Output:

```1 2 3 4 5
```

Please refer complete article on Stooge Sort for more details!

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up