Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- void printArray(int *arr, int n)
- {
- for (int i = 0; i < n; i++)
- cout << arr[i] << " ";
- cout << endl;
- }
- void fillRandArray(int *arr, int n)
- {
- for (int i = 0; i < n; i++)
- arr[i] = rand();
- }
- long long exchangeSort(int *arr, int n)
- {
- long long compare = 0, move = 0;
- for (int i = 0; i < n - 1; i++)
- for (int j = 0; j < n - i - 1; j++) {
- compare++;
- if (arr[j] > arr[j + 1]) {
- move++;
- int temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- return compare + move;
- }
- int main()
- {
- srand(time(0));
- const long long n = 100; //100, 1000, 10000, 100000
- int *arr = new int[n];
- fillRandArray(arr, n);
- long long start = clock();
- cout << exchangeSort(arr, n) << endl;
- long long end = clock();
- cout << end - start << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement