Sorting algorithms/Stooge sort
You are encouraged to solve this task according to the task description, using any language you may know.
Sorting Algorithm
This is a sorting algorithm. It may be applied to a set of data in order to sort it.
For comparing various sorts, see compare sorts.
For other sorting algorithms, see sorting algorithms, or:
Heap sort | Merge sort | Patience sort | Quick sort
O(n log2n) sorts
Shell Sort
O(n2) sorts
Bubble sort |
Cocktail sort |
Cocktail sort with shifting bounds |
Comb sort |
Cycle sort |
Gnome sort |
Insertion sort |
Selection sort |
Strand sort
other sorts
Bead sort |
Bogo sort |
Common sorted list |
Composite structures sort |
Custom comparator sort |
Counting sort |
Disjoint sublist sort |
External sort |
Jort sort |
Lexicographical sort |
Natural sorting |
Order by pair comparisons |
Order disjoint list items |
Order two numerical lists |
Object identifier (OID) sort |
Pancake sort |
Quickselect |
Permutation sort |
Radix sort |
Ranking methods |
Remove duplicate elements |
Sleep sort |
Stooge sort |
[Sort letters of a string] |
Three variable sort |
Topological sort |
Tree sort
This page uses content from Wikipedia. The original article was at Sorting algorithms/Stooge sort. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) |
Show the Stooge Sort for an array of integers. The Stooge Sort algorithm is as follows:
algorithm stoogesort(array L, i = 0, j = length(L)-1) if L[j] < L[i] then L[i] ↔ L[j] if j - i > 1 then t = (j - i + 1)/3 stoogesort(L, i , j-t) stoogesort(L, i+t, j ) stoogesort(L, i , j-t) return L
D
<lang d>import std.stdio ;
T[] stooge(T)(ref T[] l) {
if(l[$-1] < l[0]) { T t = l[0] ; l[0] = l[$-1] ; l[$-1] = t ; } if(l.length > 2) { T[] r ; int m = l.length / 3 ; r = l[0..$ - m] ; stooge(r) ; r = l[m..$] ; stooge(r) ; r = l[0..$ - m] ; stooge(r) ; } return l ;
}
void main() {
auto num = [1, 4, 5, 3, -6, 3, 7, 10, -2, -5] ;
writefln("%s", num.stooge) ;
}</lang>
Java
<lang java>import java.util.Arrays;
public class Stooge {
public static void main(String[] args) { int[] nums = {1, 4, 5, 3, -6, 3, 7, 10, -2, -5}; stoogeSort(nums); System.out.println(Arrays.toString(nums)); }
public static void stoogeSort(int[] L) { stoogeSort(L, 0, L.length - 1); }
public static void stoogeSort(int[] L, int i, int j) { if (L[j] < L[i]) { int tmp = L[i]; L[i] = L[j]; L[j] = tmp; } if (j - i > 1) { int t = (j - i + 1) / 3; stoogeSort(L, i, j - t); stoogeSort(L, i + t, j); stoogeSort(L, i, j - t); } }
}</lang> Output:
[-6, -5, -2, 1, 3, 3, 4, 5, 7, 10]
PureBasic
<lang PureBasic>Procedure Stooge_Sort(Array L.i(1), i=0 , j=0)
If j=0 j=ArraySize(L()) EndIf If L(i)>L(j) Swap L(i), L(j) EndIf If j-i>1 Protected t=(j-i+1)/3 Stooge_Sort(L(), i, j-t) Stooge_Sort(L(), i+t, j ) Stooge_Sort(L(), i, j-t) EndIf
EndProcedure</lang> Implementation may be as<lang PureBasic>Define AmountOfPosts=(?Stop_Data-?Start_data)/SizeOf(Integer) Dim Xyz.i(AmountOfPosts) CopyMemory(?Start_data, @Xyz(), ?Stop_Data-?Start_data)
Stooge_Sort(Xyz())
For i=0 To ArraySize(Xyz())
Debug Xyz(i)
Next i
DataSection
Start_data: Data.i 1, 4, 5, 3, -6, 3, 7, 10, -2, -5, 7, 5, 9, -3, 7 Stop_Data:
EndDataSection</lang>
Tcl
<lang tcl>package require Tcl 8.5
proc stoogesort { L {i 0} {j ""} } {
if {$j==""} { set j [expr [llength $L]-1] } set Li [lindex $L $i] set Lj [lindex $L $j] if {$Lj < $Li } { set L [lreplace $L $i $i $Lj] set L [lreplace $L $j $j $Li] } if {($j-$i) > 1} { set t [expr ($j-$i+1)/3] set L [stoogesort $L $i [expr $j-$t]] set L [stoogesort $L [expr $i+$t] $j] set L [stoogesort $L $i [expr $j-$t]] } return $L
}
stoogesort {1 4 5 3 -6 3 7 10 -2 -5}
</lang> Output:
-6 -5 -2 1 3 3 4 5 7 10