-
Notifications
You must be signed in to change notification settings - Fork 0
/
bogosort.c
66 lines (57 loc) · 1.08 KB
/
bogosort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// implementation of bogosort greatest algorithm yes yes for learning purposes
int is_sorted(int arr[], int n);
int main(int argc, char* argv[])
{
if (argc == 1)
{
printf("run ./bogosort N\n");
return EXIT_FAILURE;
}
const int N = atoi(argv[1]);
printf("LET'S GO GAMBLING\n");
srand(time(NULL));
int arr[N];
for (int i = 0; i < N; i++)
{
arr[i] = rand() % 1000;
}
unsigned long long iterations = 0;
while (1)
{
// shuffle
iterations++;
if (is_sorted(arr, N))
{
break;
}
for (int i = 0; i < N-1; i++)
{
int j = rand() % (N - i) + i;
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
for (int i = 0; i < N; i++)
{
printf("%3d ", arr[i]);
}
printf("AWW DANG IT\n");
}
printf("LETS GO!!!!!\n");
printf("Completed in %llu iterations\n", iterations);
return EXIT_SUCCESS;
}
int is_sorted(int arr[], int n)
{
for (int i = 1; i < n; i++)
{
if (arr[i] < arr[i - 1])
{
return EXIT_SUCCESS;
}
}
return EXIT_FAILURE;
}