Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <locale.h>
- #include <stdlib.h>
- #include <iostream>
- #include <ctime>
- const int size = 100000;
- void sort1(int* &A);
- void sort2(int* &A);
- void sort3(int* &A);
- void fill(int* &A);
- int main()
- {
- char method = '1';
- std::clock_t start;
- int* A = new int[size];
- int* B = new int[size];
- int* C = new int[size];
- fill(A);
- fill(B);
- fill(C);
- while (method != '0') {
- printf("Write the nubmer of method (0 - exit):\n");
- std::cin >> method;
- switch (method) {
- case '1':
- start = std::clock();
- sort1(A);
- printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
- start = std::clock();
- sort1(B);
- printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
- start = std::clock();
- sort1(C);
- printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
- break;
- case '2':
- start = std::clock();
- sort2(A);
- printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
- start = std::clock();
- sort2(B);
- printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
- start = std::clock();
- sort2(C);
- printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
- break;
- case '3':
- start = std::clock();
- sort3(A);
- printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
- start = std::clock();
- sort3(B);
- printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
- start = std::clock();
- sort3(C);
- printf("Runtime is %.3f seconds\n", ((std::clock() - start) / (double)(CLOCKS_PER_SEC)));
- break;
- }
- }
- system("PAUSE");
- return 0;
- }
- void fill(int* &A) {
- for (int i = 0; i < size; i++) {
- A[i] = rand() % 10;
- }
- }
- void sort1(int* &A) //вставками
- {
- for (int i = 0, j, x; i < size; i++)
- {
- j = i;
- x = A[i];
- while (j>0 && x < A[j - 1])
- {
- A[j] = A[j - 1];
- j--;
- }
- A[j] = x;
- };
- }
- void sort2(int* &A) //Пузырек
- {
- for (int i = 0, x; i < size; i++)
- for (int j = size; j >= i; j--)
- if (A[j - 1]>A[j])
- {
- x = A[j - 1];
- A[j - 1] = A[j];
- A[j] = x;
- }
- }
- void sort3(int* &A) //Выборка
- {
- for (int i = 0, k, x; i < size; i++)
- {
- k = i;
- x = A[i];
- for (int j = i + 1; j <= size; j++)
- if (A[j]>x)
- {
- k = j;
- x = A[k];
- }
- A[k] = A[i];
- A[i] = x;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement