Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <stdlib.h>
- unsigned long long sortTime, aOperations, cOperations;
- using namespace std;
- bool isSorted(int arr[], int n) {
- for (int i = 0; i < n - 1; i++) {
- if (arr[i] > arr[i + 1]) return false;
- }
- return true;
- }
- void insertionSort(int arr[], int n) {
- for (int i = 1, j = i; i < n; i++, j = i) {
- while (j > 0 && arr[j] < arr[j - 1]) {
- swap(arr[j], arr[j - 1]);
- j--;
- }
- }
- }
- void insertionSortwithOperations(int arr[], int n) {
- for (int i = 1, j = i; i < n; i++, j = i) {
- while (j > 0 && arr[j] < arr[j - 1]) {
- cOperations++;
- swap(arr[j], arr[j - 1]);
- aOperations += 3;
- j--;
- }
- cOperations++;
- }
- }
- void quickSort(int arr[], int a, int b) {
- int l = a, r = b;
- int m = arr[(a + b) / 2];
- while (l <= r) {
- while (arr[l] < m) l++;
- while (arr[r] > m) r--;
- if (l <= r) {
- if (l < r)
- swap(arr[l], arr[r]);
- ++l; --r;
- }
- }
- if (a < r) quickSort(arr, a, r);
- if (l < b) quickSort(arr, l, b);
- }
- void quickSortwithOperations(int arr[], int a, int b) {
- int l = a, r = b;
- int m = arr[(a + b) / 2];
- while (l <= r) {
- while (arr[l] < m) {
- l++;
- cOperations++;
- }
- cOperations++;
- while (arr[r] > m) {
- r--;
- cOperations++;
- }
- cOperations++;
- if (l <= r) {
- if (l < r) {
- aOperations += 3;
- swap(arr[l], arr[r]);
- }
- ++l; --r;
- }
- }
- if (a < r) quickSort(arr, a, r);
- if (l < b) quickSort(arr, l, b);
- }
- void insertionSortBenchmark(int arr[], int n) {
- int bArr[n];
- for (int i = 0; i < n; i++)
- bArr[i] = arr[i];
- sortTime = clock();
- insertionSort(bArr, n);
- sortTime = clock() - sortTime;
- for (int i = 0; i < n; i++)
- bArr[i] = arr[i];
- aOperations = cOperations = 0;
- insertionSortwithOperations(bArr, n);
- cout << "===Insertion=Sort=Benchmark===" << endl;
- cout << "n = " << n << endl;
- cout << "time = " << sortTime << " milliseconds" << endl;
- cout << "assignment operations = " << aOperations << endl;
- cout << "comparison operations = " << cOperations << endl;
- cout << "is sorted = " << isSorted(bArr, n) << endl;
- cout << "==============================" << endl << endl;
- }
- void quickSortBenchmark(int arr[], int n) {
- int bArr[n];
- for (int i = 0; i < n; i++)
- bArr[i] = arr[i];
- sortTime = clock();
- quickSort(bArr, 0, n);
- sortTime = clock() - sortTime;
- for (int i = 0; i < n; i++)
- bArr[i] = arr[i];
- aOperations = cOperations = 0;
- quickSortwithOperations(bArr, 0, n);
- cout << "=====Quick=Sort=Benchmark=====" << endl;
- cout << "n = " << n << endl;
- cout << "time = " << sortTime << " milliseconds" << endl;
- cout << "assignment operations = " << aOperations << endl;
- cout << "comparison operations = " << cOperations << endl;
- cout << "is sorted = " << isSorted(bArr, n) << endl;
- cout << "==============================" << endl << endl;
- }
- int getRandom(int max) {
- return rand() % max + 1;
- }
- void generateRandom(int arr[], int n) {
- for (int i = 0; i < n; i++) {
- arr[i] = getRandom(n);
- }
- }
- void generateGrowth(int arr[], int n) {
- for (int i = 0; i < n; i++) {
- arr[i] = i;
- }
- }
- void generateDesc(int arr[], int n) {
- for (int i = 0; i < n; i++) {
- arr[i] = n - i - 1;
- }
- }
- int main() {
- srand(time(NULL));
- const int tN[] = {10, 100, 1000, 10000};
- for (int n : tN) {
- int arr[n];
- cout << "by random: " << endl;
- generateRandom(arr, n);
- quickSortBenchmark(arr, n);
- insertionSortBenchmark(arr, n);
- cout << "by growth: " << endl;
- generateGrowth(arr, n);
- quickSortBenchmark(arr, n);
- insertionSortBenchmark(arr, n);
- cout << "by descending: " << endl;
- generateDesc(arr, n);
- quickSortBenchmark(arr, n);
- insertionSortBenchmark(arr, n);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement