3 Replies - 1817 Views - Last Post: 24 January 2008 - 02:25 PM Rate Topic: -----

#1 Insan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • 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! :D
Was This Post Helpful? 0
  • +
  • -

#3 Insan  Icon User is offline

  • New D.I.C Head

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

Re: Bozo Sort is not working

Posted 24 January 2008 - 01:59 PM

View PostMartyr2, 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! :D



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;
	}
}

Was This Post Helpful? 0
  • +
  • -

#4 Insan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1