Skip to content

Instantly share code, notes, and snippets.

@cristaloleg
Created December 6, 2012 12:20
Show Gist options
  • Save cristaloleg/4224064 to your computer and use it in GitHub Desktop.
Save cristaloleg/4224064 to your computer and use it in GitHub Desktop.
maybe the fastest bogosort
#include <cstdio>
#include <cstdlib>
#include <windows.h>
#define N 10
int arr[N];
__forceinline bool correct()
{
int size = N;
while (--size > 0)
if (arr[size - 1] > arr[size])
return false;
return true;
}
void main()
{
for ( int i = 0; i < N; ++i )
arr[i] = rand() % 100;
for ( int i = 0; i < N; ++i )
printf("%d ", arr[i]);
printf("\n");
long long time1 = GetTickCount();
while (!correct())
{
for (int j = 0; j < N; j++)
{
int r = rand() % N;
int t = arr[j];
arr[j] = arr[r];
arr[r] = t;
}
}
long long time2 = GetTickCount();
printf("%lld\n", time2 - time1);
for ( int i = 0; i < N; ++i )
printf("%d ", arr[i]);
printf("\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment