Skip to content
This repository has been archived by the owner on Oct 7, 2019. It is now read-only.

Commit

Permalink
Merge pull request #184 from MERWINDASARI/master
Browse files Browse the repository at this point in the history
added permutation or Bogo_sort
  • Loading branch information
ankitjena authored Oct 12, 2018
2 parents 195f01d + d33d2c3 commit 2f5436b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
51 changes: 51 additions & 0 deletions sort/Bogo_sort/permutation_bogosort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// C++ implementation of Bogo Sort
#include<bits/stdc++.h>
using namespace std;
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);
}

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

int main()
{
int n;
cout<<"enter size of array"<<endl;
cin>>n;
int* a = new int[n];
cout<<"elements of array"<<endl;
for(int i=0;i<n;i++)cin>>a[i];
BogoSort(a, n);
printf("Sorted array :\n");
printArray(a,n);
return 0;
}
1 change: 1 addition & 0 deletions sort/Bogo_sort/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// C++ implementation of Bogo Sort

0 comments on commit 2f5436b

Please sign in to comment.