Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<time.h>
- using namespace std;
- void swap(int* a, int* b) {
- int temp;
- temp = *a;
- *a = *b;
- *b = temp;
- }
- void ShakerSort(int a[], int m) {
- int i, j, k;
- clock_t start = 0, end = 0;
- start = clock();
- for (int r = 0; r < 1000; r++) {
- for (i = 0; i < m;) {
- for (j = i + 1; j < m; j++) {
- if (a[j] < a[j - 1])
- swap(&a[j], &a[j - 1]);
- }
- m--;
- for (k = m - 1; k > i; k--) {
- if (a[k] < a[k - 1])
- swap(&a[k], &a[k - 1]);
- }
- i++;
- }
- }
- end = clock();
- cout << endl << "time = " << ("The above code block was executed in %.4f second(s)\n", ((double)end - start) / ((double)CLOCKS_PER_SEC)) << endl;
- }
- /*------------------------------------------------------------*/
- int binarySearch(int arr[], int item, int low, int high) {
- if (high <= low)
- return (item > arr[low]) ? (low + 1) : low;
- int mid = (low + high) / 2;
- if (item == arr[mid])
- return mid + 1;
- if (item > arr[mid])
- return binarySearch(arr, item, mid + 1, high);
- return binarySearch(arr, item, low, mid - 1);
- }
- void BinaryInsertionSort(int arr[], int n) {
- int i, loc, j, k, selected;
- clock_t start = 0, end = 0;
- start = clock();
- for (int r = 0; r < 10087; r++) {
- for (i = 1; i < n; ++i) {
- j = i - 1;
- selected = arr[i];
- loc = binarySearch(arr, selected, 0, j);
- while (j >= loc) {
- arr[j + 1] = arr[j];
- j--;
- }
- arr[j + 1] = selected;
- }
- }
- end = clock();
- cout << endl << "time = " << ("The above code block was executed in %.4f second(s)\n", ((double)end - start) / ((double)CLOCKS_PER_SEC)) << endl;
- }
- /*---------------------------------------------------------------*/
- int i, j, key = 0, temp = 0;
- void InsertSort(int* mas, int n)
- {
- clock_t start = 0, end = 0;
- start = clock();
- for (int r = 0; r < 1000000; r++) {
- for (i = 0; i < n - 1; i++)
- {
- key = i + 1;
- temp = mas[key];
- for (j = i + 1; j > 0; j--)
- {
- if (temp < mas[j - 1])
- {
- mas[j] = mas[j - 1];
- key = j - 1;
- }
- }
- mas[key] = temp;
- }
- }
- end = clock();
- cout << endl << "sıralama: ";
- for (i = 0; i < n; i++)
- cout << mas[i] << " ";
- cout << endl << "time = " << ("The above code block was executed in %.4f second(s)\n", ((double)end - start) / ((double)CLOCKS_PER_SEC)) << endl;
- }
- void main()
- {
- int n;//Sorting by simple inserts
- cout << "n:"; cin >> n;
- int* mas = new int[n];
- for (i = 0; i < n; i++)
- {
- cout << "Enter element " << i + 1 << ": ";
- cin >> mas[i];
- }
- InsertSort(mas, n);
- cout << "----------------------------------------------------------" << endl;
- int mas2[] = { 37, 23, 0, 17, 12, 72, 31,46, 100, 88, 54 };//Sorting using binary inserts.
- int g = sizeof(mas2) / sizeof(mas2[0]), v;
- BinaryInsertionSort(mas2, g);
- for (i = 0; i < 11; i++)
- {
- cout << mas2[i] << " ";
- }
- cout << endl;
- cout << "----------------------------------------------------------------" << endl;
- srand(time(0));
- int q;//shaker sorting
- cout << "n:"; cin >> q;
- int* mas3 = new int[q];
- for (i = 0; i < q; i++)
- {
- mas3[i] = rand() % 120;
- }
- for (i = 0; i < q; i++)
- {
- cout << "Enter element " << i + 1 << ": " << mas3[i] << endl;
- }
- ShakerSort(mas3, q);
- for (i = 0; i < q; i++)
- cout << mas3[i] << " ";
- cout << endl;
- delete[] mas;
- delete[] mas3;
- system("pause");
- }
Add Comment
Please, Sign In to add comment