Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <iostream>
- #include <time.h>
- using namespace std;
- void ekran(int w[], int q){
- cout << "Введите размер массива\n" << q << endl;
- cout << "Исходный массив\n";
- for (int i = 0; i < q; i++) cout << " " << w[i];
- }
- int main() {
- setlocale(LC_ALL, "Rus");
- srand(time(NULL));
- int *x;
- int a = -100;
- int b = 100;
- int y, n;
- int s = 0;
- cout << "Введите размер массива\n";
- cin >> n;
- if (n < 1) {
- x = new int[10];
- cout << "Исходный массив\n";
- for (int i = 0; i < 10; i++) {
- x[i] = rand() % (b - a + 1) + a;
- cout << " " << x[i];
- }
- for (int i = 1; i < 10; ++i)
- {
- for (int j = 0; j < 10 - i; j++)
- {
- if (x[j] > x[j + 1])
- {
- s++;
- y = x[j];
- x[j] = x[j + 1];
- x[j + 1] = y;
- }
- }
- }
- cout << "\nОтсортированный массив\n";
- for (int i = 0; i < 10; i++) {
- cout << " " << x[i];
- }
- cout << "\nКоличество сравнений\n" << s;
- }
- else {
- x = new int[n];
- cout << "Исходный массив\n";
- for (int i = 0; i < n; i++) {
- cin >> x[i];
- cout << " " << x[i];
- }
- system("cls");
- ekran(x, n);
- for (int i = 1; i < n; ++i)
- {
- for (int j = 0; j < n - i; j++)
- {
- if (x[j] > x[j + 1])
- {
- s++;
- y = x[j];
- x[j] = x[j + 1];
- x[j + 1] = y;
- }
- }
- }
- cout << "\nОтсортированный массив\n";
- for (int i = 0; i < n; i++) {
- cout << " " << x[i];
- }
- cout << "\nКоличество сравнений\n" << s;
- }
- cout << endl;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment