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

Commit

Permalink
added permitation or bogo_sort
Browse files Browse the repository at this point in the history
  • Loading branch information
MERWINDASARI committed Oct 9, 2018
1 parent a04381c commit d33d2c3
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 d33d2c3

Please sign in to comment.