Advertisement
dmkozyrev

bubble_sort

Jan 13th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. void bubble_sort(int mas[], int N); // Сортировка пузырьком
  6. void print(int mas[], int N); // Вывод массива на экран
  7. void fill(int mas[], int N, int a, int b); // Заполнение массива случайными числами
  8.  
  9. int main(){
  10.     srand(time(0)); // Для генерации разных случайных чисел при каждом запуске программы
  11.  
  12.     unsigned const int SIZE = 20; // Размер массива
  13.     int a[SIZE]; // Массив
  14.  
  15.     // Заполнение массива случайными числами
  16.     fill(a, SIZE, -10, 10);
  17.  
  18.     // Печать изначального массива
  19.     print(a, SIZE);
  20.  
  21.     // Сортировка массива
  22.     bubble_sort(a, SIZE);
  23.  
  24.     // Печать измененного массива
  25.     print(a, SIZE);
  26.  
  27.     return 0;
  28. }
  29.  
  30. void bubble_sort(int mas[], int N){
  31.     int i, j;
  32.     for(i = 0; i < N-1; i++)
  33.         for(j = i+1; j < N; j++)
  34.             if (mas[i] < mas[j]){
  35.                 int temp = mas[i];
  36.                 mas[i] = mas[j];
  37.                 mas[j] = temp;
  38.             }
  39. }
  40.  
  41. void print(int mas[], int N){
  42.     int i;
  43.     for(i = 0; i < N; i++)
  44.         printf("%d ", mas[i]);
  45.     printf("\n");
  46. }
  47.  
  48. void fill(int mas[], int N, int a, int b){
  49.     int i;
  50.     for(i = 0; i < N; i++)
  51.         mas[i] = rand()%(b-a+1)+a;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement