Advertisement
Guest User

ANDREY_CHALY

a guest
Apr 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.59 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define MAX 999
  4.  
  5. int input_array(int array[], int len);
  6. void bubble_sort(int array[], int len);
  7. void print_array(int array[], int len);
  8.  
  9. int main()
  10. {
  11.     int arr[MAX]; // Исходный массив
  12.     int n; // Число элементов в массиве
  13.  
  14.     printf("Input size of array ");
  15.     if ((scanf("%i", &n) != 1) || (n <= 0))
  16.     {
  17.         printf("Wrong input data");
  18.         return -1;
  19.     }
  20.  
  21.     if (input_array(arr, n) == -1)
  22.     {
  23.         printf("Wrong input data");
  24.         return -1;
  25.     }
  26.  
  27.     bubble_sort(arr, n);
  28.     printf("Sorted array:\n");
  29.     print_array(arr, n);
  30.     return 0;
  31. }
  32.  
  33. int input_array(int array[], int len)
  34. {
  35.     int i; // Счётчик
  36.     int chr; // Число из потока ввода
  37.  
  38.     for (i = 0; i < len; i++)
  39.     {
  40.         if (scanf("%i", &chr) == 1)
  41.         {
  42.             array[i] = chr;
  43.         }
  44.         else
  45.         {
  46.             return -1;
  47.         }
  48.     }
  49.  
  50.     return 0;
  51. }
  52.  
  53. void bubble_sort(int array[], int len)
  54. {
  55.     int i, j; // Счётчики
  56.     int temporary; // Временная переменная
  57.  
  58.     for (i = 0; i < len; i++)
  59.     {
  60.         for (j = 0; j < len - 1; j++)
  61.         {
  62.             if (array[j] > array[j + 1])
  63.             {
  64.                 temporary = array[j];
  65.                 array[j] = array[j + 1];
  66.                 array[j + 1] = temporary;
  67.             }
  68.         }
  69.     }
  70.  
  71.     return;
  72. }
  73. void print_array(int array[], int len)
  74. {
  75.     int i;
  76.  
  77.     for (i = 0; i < len; i++)
  78.     {
  79.         printf("%d ", array[i]);
  80.     }
  81.  
  82.     return;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement