Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- bool IsSorted(int *firstElement, int arraySize);
- void Bogosort(int *firstElement, int arraySize);
- void RandomSorted(int *firstElement, int arraySize);
- void DisplayArray(int *firstElement, int arraySize, int index);
- int main(int argc, const char * argv[])
- {
- int const ARRAY_SIZE = 7;
- int array[ARRAY_SIZE] = {21, 1, 423, 5, 22, 124, 2};
- srand(ARRAY_SIZE);
- Bogosort(array, ARRAY_SIZE);
- DisplayArray(array, ARRAY_SIZE, 0);
- cout << endl;
- return 0;
- }
- void DisplayArray(int *firstElement, int arraySize, int index)
- {
- if(index >= arraySize) return;
- cout << firstElement[index] << " ";
- DisplayArray(firstElement, arraySize, ++index);
- }
- bool IsSorted(int *firstElement, int arraySize)
- {
- for(auto i = 0; i < arraySize - 1; ++i)
- {
- if(firstElement[i] > firstElement[i + 1])
- {
- return false;
- }
- }
- return true;
- }
- void Bogosort(int *firstElement, int arraySize)
- {
- while(!IsSorted(firstElement, arraySize))
- {
- RandomSorted(firstElement, arraySize);
- }
- }
- void RandomSorted(int *firstElement, int arraySize)
- {
- auto n = arraySize;
- while(n > 1)
- {
- n--;
- auto i = rand()%arraySize;
- auto temp = firstElement[i];
- firstElement[i] = firstElement[n];
- firstElement[n] = temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement