Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include"windows.h"
- #include"locale.h"
- #include "dos.h"
- #include <ctime>
- using namespace std;
- void sort1();//пузырьковая
- void sort2();//обменная
- void sort3();//вставки
- void show(int a[], int n, int pos, int elem);
- int main()
- {
- setlocale(LC_ALL, "");
- int input;
- printf("Методы сортировки:\n ");
- printf("1.Пузырьковая сортировка\n ");
- printf("2.Сортировка выбором\n ");
- printf("3.Сортировка вставки\n ");
- scanf_s("%d", &input);
- switch (input) {
- case 1:
- sort1();
- break;
- case 2:
- sort2();
- break;
- case 3:
- sort3();
- break;
- }
- return 0;
- }
- void show(int a[], int n, int pos, int elem)
- {
- system("cls");
- printf("\n");
- HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
- for (int i = 0; i < n; i++)
- if (i == elem)
- {
- SetConsoleTextAttribute(hConsole, (WORD)((13 << 4) | 15));
- printf("\t%i", a[elem]);
- SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | 14));
- }
- else if (i == pos)
- {
- SetConsoleTextAttribute(hConsole, (WORD)((13 << 4) | 15));
- printf("\t%i", a[pos]);
- SetConsoleTextAttribute(hConsole, (WORD)((0 << 4) | 14));
- }
- else
- printf("\t%i", a[i]);
- printf("\n\n");
- Sleep(300);
- }
- void sort1()
- {
- printf("Введите количество элементов: ");
- int n;
- scanf_s("%i", &n);
- int* a = new int[n];
- srand(time(0));
- for (int i = 0; i < n; i++)
- a[i] = rand() % 101 - 50;
- for (int i = 1; i < n; i++)
- {
- int flag = 1;
- for (int j = 0; j < n - i; j++)
- {
- show(a, n, j, j + 1);
- if (a[j] > a[j + 1])
- {
- swap(a[j], a[j + 1]);
- show(a, n, j, j + 1);
- flag = 0;
- }
- }
- if (flag == 1)
- break;
- }
- show(a, n, -1, -1);
- delete[] a;
- }
- void sort2()
- {
- printf("Введите количество элементов: ");
- int n;
- scanf_s("%i", &n);
- int* a = new int[n];
- srand(time(0));
- for (int i = 0; i < n; i++)
- a[i] = rand() % 101 - 50;
- for (int i = 0; i < n - 1; i++)
- {
- int m = i;
- for (int j = i + 1; j < n; j++)
- {
- show(a, n, j, i);
- if (a[j] < a[m])
- m = j;
- }
- show(a, n, m, i);
- swap(a[i], a[m]);
- show(a, n, m, i);
- }
- delete[] a;
- }
- void sort3()
- {
- printf("Введите количество элементов: ");
- int n;
- scanf_s("%i", &n);
- int* a = new int[n];
- srand(time(0));
- for (int i = 0; i < n; i++)
- a[i] = (rand() % 101) - 50;
- for (int i = 1; i < n; ++i)
- {
- int t = a[i];
- int j = i - 1;
- show(a, n, i, j);
- while (j >= 0 && a[j] > t)
- {
- a[j + 1] = a[j];
- show(a, n, i, j);
- j--;
- }
- a[j + 1] = t;
- show(a, n, -1, -1);
- }
- delete[] a;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement