Created
December 6, 2012 12:20
-
-
Save cristaloleg/4224064 to your computer and use it in GitHub Desktop.
maybe the fastest bogosort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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