Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- using namespace std;
- // Выводит массив
- void printArr(int* arr, unsigned int size) {
- for (int i = 0; size > i; i++) {
- cout << arr[i] << ' ';
- }
- cout << endl;
- }
- // Меняет значения у аргументов(!) местами
- void swap(int &x, int &y) {
- int tempInt = x;
- x = y;
- y = tempInt;
- }
- // Проверяет на корректность массив
- bool correct(int *arr, int size) {
- // Цикл, сравнивающий элементы
- for (int i = 1; size > i; i++) {
- if (arr[i] < arr[i - 1]) {
- return 0;
- }
- }
- return 1;
- }
- // Меняет первый элемента массива с случайным size раз (Перемешивание)
- void shuffle(int *arr, int size) {
- for (int i = 0; i < size; ++i) {
- swap(arr[i], arr[(rand() % size)]);
- }
- }
- // Сортирует, пока массив не будет отсортирован)
- void bogoSort(int *arr, int size) {
- while (!correct(arr, size)) {
- shuffle(arr, size);
- printArr(arr, size);
- }
- }
- int main() {
- srand(time(NULL));
- int arr[5] = { 0, 5, 2, 3, 1 };
- printArr(arr, 5);
- bogoSort(arr, 5);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement