# Stooge Sort

Page 1 of 1

## 1 Replies - 784 Views - Last Post: 20 April 2010 - 12:30 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=364152&amp;s=c450bc1a8dc280de84d0ca54bbb6ee4d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 born2c0de

• printf("I'm a %XR",195936478);

Reputation: 187
• Posts: 4,673
• Joined: 26-November 04

# Stooge Sort

Posted 29 October 2005 - 08:34 PM

Description: Shows how an array can be sorted recursively using the Stooge Sort Algorithm.
```/*

STOOGE SORT
Written by Sanchit Karve AKA born2c0de
born2c0de AT hotmail DOT com
*/

#include <stdio>

#define ELEMENTS 6

void stoogesort(int A[],int i,int j)
{
int tmp,k;
if(A[i]>A[j])
{
tmp=A[i];
A[i]=A[j];
A[j]=tmp;
}
if((i+1)>=j)
return;

k=(int)((j-i+1)/3);   // Round down
stoogesort(A,i,j-k);  // First two-thirds
stoogesort(A,i+k,j);  // Last two-thirds
stoogesort(A,i,j-k);  // First two-thirds again
}

int main()
{
int i;
int X[ELEMENTS]={5,2,4,6,1,3};
printf("Unsorted Array:n");
for(i=0;i<ELEMENTS;i++)
printf("%d ",X[i]);

stoogesort(X,0,ELEMENTS);
printf("nSORTED ARRAYn");
for(i=0;i<ELEMENTS;i++)
printf("%d ",X[i]);

}

```

Is This A Good Question/Topic? 0