This repository has been archived by the owner on Oct 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #184 from MERWINDASARI/master
added permutation or Bogo_sort
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
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
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; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// C++ implementation of Bogo Sort |