Last active
August 29, 2015 13:57
-
-
Save rozag/9625066 to your computer and use it in GitHub Desktop.
Глупейшая сортировка. Случайным образом свапает два элемента и проверят пока не посортится. Уже на 13 работает долго :)
This file contains hidden or 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
#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