Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "pch.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <conio.h>
  6. #include <Windows.h>
  7. #include <cmath>
  8. #include <string>
  9. #include <locale.h>
  10. #include <iostream>
  11. #include <time.h>
  12.  
  13.  
  14.  
  15.  
  16. void show(int a[], int n, int p, int el) {
  17.     system("cls");
  18.     for (int i = 0; i < n; ++i)
  19.         if (i == p)
  20.             printf("%5i\n", el);
  21.         else printf("%i\n", a[i]);
  22.     Sleep(250);
  23. }
  24.  
  25.  
  26. int main()
  27. {
  28.     setlocale(LC_ALL, "");
  29.  
  30.     printf("Не сортированный массив\n");
  31.     system("color 4");
  32.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  33.  
  34.  
  35.  
  36. const int N = 15;
  37. int a[N];
  38. srand((unsigned)time(0));
  39.    for (int i = 0; i < N; i++)
  40.    {
  41.     a[i] = 1 + rand() % 51;
  42.     SetConsoleTextAttribute(hConsole, 14);
  43.     printf("%d ", a[i]);
  44.     Sleep(150);
  45.    }
  46.  
  47. printf("\n");
  48.  
  49.  
  50.  
  51.         show(a, N, -1, 0);
  52.         for (int i = 1; i < N; ++i)
  53.         {
  54.             int t = a[i];
  55.             show(a, N, i, t);
  56.             int j = i - 1;
  57.  
  58.             while (j >= 0 && a[j] > t)
  59.             {
  60.                 a[j + 1] = a[j];
  61.                 show(a, N, j, t);
  62.                 --j;
  63.             }
  64.             a[j + 1] = t;
  65.             show(a, N, -1, 0);
  66.         }
  67.  
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement