Skip to content

Instantly share code, notes, and snippets.

@rozag
Last active August 29, 2015 13:57
Show Gist options
  • Save rozag/9625066 to your computer and use it in GitHub Desktop.
Save rozag/9625066 to your computer and use it in GitHub Desktop.
Глупейшая сортировка. Случайным образом свапает два элемента и проверят пока не посортится. Уже на 13 работает долго :)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <vector>
#include <set>
#include <ctime>
using namespace std;
bool sorted (vector <int>& arr) {
for (int i = 1; i < arr.size(); i++) {
if (arr[i - 1] > arr[i]) return false;
}
return true;
}
void printArr (vector <int>& arr) {
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main (int argc, char *argv[]) {
srand(time(NULL));
int count;
cin >> count;
vector <int> arr;
for (int i = 0; i < count; i++) {
arr.push_back(rand() % count);
}
printArr(arr);
cout << endl;
clock_t start = clock();
while (!sorted(arr)) {
int i = rand() % count;
int j = rand() % count;
swap(arr[i], arr[j]);
// printArr(arr);
}
printArr(arr);
cout << (double) (clock() - start) / CLOCKS_PER_SEC << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment