Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <time.h>
- using namespace std;
- int cheng = 0, simile = 0;
- void quickSort(int arr[], int left, int right) {
- int i = left, j = right;
- int tmp;
- int pivot = arr[(left + right) / 2];
- /* partition */
- while (i <= j) {
- while (arr[i] < pivot)
- i++;
- while (arr[j] > pivot)
- j--;
- cheng++;
- if (i <= j) {
- simile++;
- tmp = arr[i];
- arr[i] = arr[j];
- arr[j] = tmp;
- i++;
- j--;
- }
- };
- /* recursion */
- if (left < j)
- quickSort(arr, left, j);
- if (i < right)
- quickSort(arr, i, right);
- }
- int main() {
- setlocale(LC_CTYPE, "Russian");
- int num;
- cout << "Колво элементов: ";
- cin >> num;
- int* mass = new int[num];
- for (int i = 0; i < num; i++) {
- mass[i] = rand() % 20 + 100;
- cout << mass[i] << " ";
- }
- cout << endl;
- cout << "Quicksorted array:" << endl;
- quickSort(mass, 0, num - 1);//функция квиксорта.
- for (int i = 0; i < num; i++) {
- cout << mass[i] << "\t";
- }
- cout << endl;
- cout << cheng << endl;
- cout << simile << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment