Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int main()
- {
- enum Methods { bubleSort, minSort, Count };
- const int randBegin{ 10 };
- const int randEnd{ 99 };
- const int testsCount{ 10 };
- const int arrSize{ 1000 };
- int arr[arrSize];
- int premutation[Methods::Count]{};
- int copy{};
- int noPremutation{true};
- int min{ 0 };
- srand(time(0));
- for (int testNum{}; testNum < testsCount; ++testNum)
- {
- // buble Sort
- for (int i{ 0 }; i < arrSize; ++i) { arr[i] = randBegin + (rand() % (randEnd - randBegin)); }
- for (int head{ 0 }; head < arrSize; ++head)
- {
- noPremutation = true;
- for (int tail{ arrSize - 1 }; tail > head; --tail)
- {
- if (arr[tail] < arr[tail - 1])
- {
- copy = arr[tail - 1];
- arr[tail - 1] = arr[tail];
- arr[tail] = copy;
- ++premutation[Methods::bubleSort];
- noPremutation = false;
- }
- }
- if (noPremutation) { break; }
- }
- // min Sort
- for (int i{ 0 }; i < arrSize; ++i) { arr[i] = randBegin + (rand() % (randEnd - randBegin)); }
- for (int head{ 0 }; head < arrSize; ++head)
- {
- min = head;
- for (int minIdx{ head }; minIdx < arrSize; ++minIdx)
- {
- if (arr[min] > arr[minIdx]) { min = minIdx; }
- }
- if (min != head)
- {
- copy = arr[head];
- arr[head] = arr[min];
- arr[min] = copy;
- ++premutation[Methods::minSort];
- }
- }
- }
- std::cout << "Premutations average BubleSort: " << premutation[Methods::bubleSort] / (double)testsCount << " MinSort: " << premutation[Methods::minSort] / (double)testsCount << '\n';
- return 0;
- }
Add Comment
Please, Sign In to add comment