Val_Kir

2lab_123

Feb 22nd, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. /*  Написать программу упорядочения целочисленного массива с
  2.     помощью одного из алгоритмов: обмена, вставок или выбора. Рекомендуемые значения
  3.     длины массива n: 10, 100, 1000, 10000, 100000. Провести проверку упорядоченности.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <time.h>
  8. #include <stdlib.h>
  9.  
  10. void test (int n, int a[]) //Проверка упорядоченности
  11. {
  12.     int i, b[10000];
  13.  
  14.     for (i=0; i<n; i++) b[i]=a[i];
  15.  
  16.     for (i=1; i<n; i++)
  17.     {
  18.         if (b[i-1]>b[i])
  19.         {
  20.             printf("Array is not ordered.");
  21.             system("pause");
  22.             exit(0);
  23.         }  
  24.     }
  25.     printf("The array is ordered. ");
  26. }
  27.  
  28. void choice (int n, int a[]) //сортировка выбором
  29. {
  30.     int c, i, j, b[10000], p, min;
  31.  
  32.     for (i=0; i<n; i++) b[i]=a[i]; //присваиваем значения одного массива другому
  33.  
  34.     for (i=0; i<n; i++)
  35.     {
  36.         min=b[i];
  37.         c=i;
  38.         for (j=i+1; j<n; j++)
  39.         {
  40.             if (b[j]<min)
  41.             {
  42.                 min=b[j];
  43.                 c=j;
  44.             }
  45.         }
  46.         p=b[i];
  47.         b[i]=min;
  48.         b[c]=p;
  49.     }
  50.  
  51.     printf ("\n Sorting by choice: \n");
  52.     for (i=0; i<n; i++) printf ("%d ",b[i]);
  53.     putchar('\n');
  54.  
  55.     test(n,b); //проверка
  56. }
  57.  
  58. void main()
  59. {
  60.     const int n=10;
  61.     int a[n];
  62.  
  63.     srand(time(0));
  64.     for (int i=0; i<n; i++)
  65.     {
  66.         a[i]=rand()%100;
  67.         printf("%d ",a[i]);
  68.     }
  69.     putchar('\n');
  70.  
  71.     choice(n,a);
  72.  
  73.     system("pause");
  74. }
Advertisement
Add Comment
Please, Sign In to add comment