Bogo Sort

Bogo sort, also known as permutation sort, stupid sort, slow sort, shotgun sort or monkey sort, is a particularly ineffective sorting algorithm based on the generate and test paradigm. The algorithm successively generates permutations of its input until it finds one that is sorted. It is not useful for sorting, but may be used for educational purposes, to contrast it with other more realistic algorithms.



									/*****Please include following header files*****/
// stdlib.h
/***********************************************/

static bool IsSorted(int* data, int count)
{
	while (--count >= 1)
		if (data[count] < data[count - 1]) return false;

	return true;
}

static void Shuffle(int* data, int count)
{
	int temp, rnd;

	for (int i = 0; i < count; ++i)
	{
		rnd = rand() % count;
		temp = data[i];
		data[i] = data[rnd];
		data[rnd] = temp;
	}
}

static void BogoSort(int* data, int count)
{
	while (!IsSorted(data, count))
		Shuffle(data, count);
}
								


Example

									int data[] = { -1, 25, -58964, 8547, -119, 0, 78596 };
BogoSort(data, 7);
								


Output

									-58964
-119
-1
0
25
8547
78596