Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions bogoSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// implementation of Bogo Sort
#include <bits/stdc++.h>

using namespace std;

bool isSorted(int a[], int n){
while (--n > 1)
if (a[n] < a[n - 1])
return false;
return true;
}

void shuffle(int a[], int n){
for (int i = 0; i < n; i++)
swap(a[i], a[rand() % n]);
}

void bogosort(int a[], int n){
while (!isSorted(a, n))
shuffle(a, n);
}


void printArray(int a[], int n){
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}


int main(){
int a[] = { 3, 2, 5, 1, 0, 4 };
int n = sizeof a / sizeof a[0];
bogosort(a, n);
printf("Sorted array :\n");
printArray(a, n);
return 0;
}