Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Windows.h>
- #include <ctime>
- using namespace std;
- //Быстрая сортировка
- int partition(int list[], int start, int pivot)
- {
- int i = start;
- while (i < pivot)
- {
- if (list[i] > list[pivot] && i == pivot - 1)
- {
- swap(list[i], list[pivot]);
- pivot--;
- }
- else if (list[i] > list[pivot])
- {
- swap(list[pivot - 1], list[pivot]);
- swap(list[i], list[pivot]);
- pivot--;
- }
- else i++;
- }
- return pivot;
- }
- void quickSort(int list[], int start, int end)
- {
- if (start < end)
- {
- int pivot = partition(list, start, end);
- quickSort(list, start, pivot - 1);
- quickSort(list, pivot + 1, end);
- }
- }
- int main1()
- {
- int list[6] = { 2, 12, 5, 48, 0, 4 };
- cout << "Input array ...\n";
- for (int i = 0; i < 6; i++)
- {
- cout << list[i] << "\t";
- }
- quickSort(list, 0, 6);
- cout << "\n\nSorted array ... \n";
- for (int i = 0; i < 6; i++)
- {
- cout << list[i] << "\t";
- }
- return 0;
- }
- //СОРТИРОВКА СЛИЯНИЕМ
- void merge(int list[], int start, int end, int mid);
- void mergeSort(int list[], int start, int end)
- {
- int mid;
- if (start < end) {
- mid = (start + end) / 2;
- mergeSort(list, start, mid);
- mergeSort(list, mid + 1, end);
- merge(list, start, end, mid);
- }
- }
- void merge(int list[], int start, int end, int mid)
- {
- int mergedList[8];
- int i, j, k;
- i = start;
- k = start;
- j = mid + 1;
- while (i <= mid && j <= end) {
- if (list[i] < list[j]) {
- mergedList[k] = list[i];
- k++;
- i++;
- }
- else {
- mergedList[k] = list[j];
- k++;
- j++;
- }
- }
- while (i <= mid) {
- mergedList[k] = list[i];
- k++;
- i++;
- }
- while (j <= end) {
- mergedList[k] = list[j];
- k++;
- j++;
- }
- for (i = start; i < k; i++) {
- list[i] = mergedList[i];
- }
- }
- int main2()
- {
- int list[8] = { 3,19,8,0,48,4,5,12 };
- cout << "Input array ...\n";
- for (int i = 0; i < 8; i++)
- {
- cout << list[i] << "\t";
- }
- mergeSort(list, 0, 7);
- cout << "\n\nSorted array ... \n";
- for (int i = 0; i < 8; i++)
- {
- cout << list[i] << "\t";
- }
- return 0;
- }
- //ПУЗЫРЬКОВАЯ СОРТИРОВКА
- void bubbleSort(int list[], int listLength)
- {
- while (listLength--)
- {
- bool swapped = false;
- for (int i = 0; i < listLength; i++)
- {
- if (list[i] > list[i + 1])
- {
- swap(list[i], list[i + 1]);
- swapped = true;
- }
- }
- if (swapped == false)
- break;
- }
- }
- int main3()
- {
- int list[5] = { 3,19,8,0,48 };
- cout << "Input array ..." << endl;
- for (int i = 0; i < 5; i++)
- cout << list[i] << '\t';
- cout << endl;
- bubbleSort(list, 5);
- cout << "Sorted array ..." << endl;
- for (int i = 0; i < 5; i++)
- cout << list[i] << '\t';
- cout << endl;
- return 0;
- }
- //Сортировка выбором
- int findSmallestPosition(int list[], int startPosition, int listLength)
- {
- int smallestPosition = startPosition;
- for (int i = startPosition; i < listLength; i++)
- {
- if (list[i] < list[smallestPosition])
- smallestPosition = i;
- }
- return smallestPosition;
- }
- void selectionSort(int list[], int listLength)
- {
- for (int i = 0; i < listLength; i++)
- {
- int smallestPosition = findSmallestPosition(list, i, listLength);
- swap(list[i], list[smallestPosition]);
- }
- return;
- }
- int main4()
- {
- int list[5] = { 12, 5, 48, 0, 4 };
- cout << "Input array ..." << endl;
- for (int i = 0; i < 5; i++)
- cout << list[i] << '\t';
- cout << endl;
- selectionSort(list, 5);
- cout << "Sorted array ..." << endl;
- for (int i = 0; i < 5; i++)
- cout << list[i] << '\t';
- cout << endl;
- return 0;
- }
- int main()
- {
- srand(time(0));
- cout << "Choose sorting 1 - 4" << endl;
- int n;
- cin >> n;
- switch (n)
- {
- case 1:
- cout << "Quick sorting" << endl;
- main1();
- cout << "\n runtime = " << clock() / 1000.0 << endl;
- break;
- case 2:
- cout << "MERGE SORTING" << endl;
- main2();
- cout << " \n runtime = " << clock() / 1000.0 << endl;
- break;
- case 3:
- cout << " \n BUBBLE SORTING" << endl;
- main3();
- cout << " \n runtime = " << clock() / 1000.0 << endl;
- break;
- case 4:
- cout << "Sorting by choice" << endl;
- cout << " \n runtime =" << clock() / 1000.0 << endl;
- main4();
- break;
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement