# Bozo Sort is not working

Page 1 of 1

## 3 Replies - 2698 Views - Last Post: 24 January 2008 - 02:25 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=41631&amp;s=c01af7d5baa086eeda34d0e7d3076c1f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Insan

• New D.I.C Head

Reputation: 0
• Posts: 7
• Joined: 24-January 08

# Bozo Sort is not working

Posted 24 January 2008 - 06:09 AM

```void SortFunctions::Shuffle(int a[], int sa)
{
int temp,randN, last;
for (last = sa; last > 1; last--)
{
randN = rand() % last;
temp = a[randN];
a[randN] = a[last-1];
a[last-1] = temp;
}

}
bool SortFunctions::Sorted(int a[], int sa)
{
for(int i = 1; i < sa; i++)
{
if (a[i] < a[i-1])
{
return false;
}
}
return true;
}
int SortFunctions::BozoSort(int a[], int sa)
{

while( !Sorted(a,sa))
{
Shuffle(a,sa);
}
return 0;
}

```

For some reason its not working. can someone tell me what I am doing wrong?

Is This A Good Question/Topic? 0

## Replies To: Bozo Sort is not working

### #2 Martyr2

• Programming Theoretician

Reputation: 5178
• Posts: 13,898
• Joined: 18-April 07

## Re: Bozo Sort is not working

Posted 24 January 2008 - 01:32 PM

hahaha Bozo Sort rules!

Ok now for your question... How are you calling BozoSort because the structure of those three functions are just fine and I have tested them with no modification and it works. So all that is left is to see how you are using it. What is your error you are getting or describe what is happening to make you think it is not working.

This is actually more of a BogoSort than a BozoSort. Bozo would require you to pick two random values, swap them and then test for sorted rather than shuffling everything up. Bogo would require the shuffle of the whole array.

I do hope you know that this is not a true or good sort and you are using it for entertainment purposes.

Gave me a nice laugh though. Thanks!

### #3 Insan

• New D.I.C Head

Reputation: 0
• Posts: 7
• Joined: 24-January 08

## Re: Bozo Sort is not working

Posted 24 January 2008 - 01:59 PM

Martyr2, on 24 Jan, 2008 - 01:32 PM, said:

hahaha Bozo Sort rules!

Ok now for your question... How are you calling BozoSort because the structure of those three functions are just fine and I have tested them with no modification and it works. So all that is left is to see how you are using it. What is your error you are getting or describe what is happening to make you think it is not working.

This is actually more of a BogoSort than a BozoSort. Bozo would require you to pick two random values, swap them and then test for sorted rather than shuffling everything up. Bogo would require the shuffle of the whole array.

I do hope you know that this is not a true or good sort and you are using it for entertainment purposes.

Gave me a nice laugh though. Thanks!

Oh.. I just realized I dont even need bogosort I need bozo. But the concept is pretty much the same, right? swap the elements of the array and to check if they are sorted. And also can u tell me if there are any logical errors in this one because I think this is where its not working.

``` void SortFunctions::InitARSorted(int *a, int sa)
/* Input: Array[], size of the array
Output: None
Return: None */
{
// Reverses the array
for (int i=0; i < sa; i++)
{
a[i] = sa-i;
}
}
```

### #4 Insan

• New D.I.C Head

Reputation: 0
• Posts: 7
• Joined: 24-January 08

## Re: Bozo Sort is not working

Posted 24 January 2008 - 02:25 PM

Wow thanks man! u know what was wrong I was trying to call bozo sort inside another function and it wasnt executing it at all but when I tried it in the main() it works. Anyways can u tell if this is correct bozo sort:
```int SortFunctions::BozoSort(int a[], int sa)
{
int c=0;
bool sorted = false;
while(c++ && sorted == false)
{
int index1 = rando(sa);		c++;
int index2 = rando(sa);		c++;
int temp =  a[index2];		c++;
a[index1] = temp;			c++;
sorted = true;				c++;
for(int i =1; c++ && i<sa; i++)
{
if(c++ && a[i-1] > a[i])
{
sorted = false;		c++;
break;
}
}
}
return c;
}

int SortFunctions::rando(int sa)
{
int result;
result = int(rand() % sa);
return result;
}
```