Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define MAX 999
- int input_array(int array[], int len);
- void bubble_sort(int array[], int len);
- void print_array(int array[], int len);
- int main()
- {
- int arr[MAX]; // Исходный массив
- int n; // Число элементов в массиве
- printf("Input size of array ");
- if ((scanf("%i", &n) != 1) || (n <= 0))
- {
- printf("Wrong input data");
- return -1;
- }
- if (input_array(arr, n) == -1)
- {
- printf("Wrong input data");
- return -1;
- }
- bubble_sort(arr, n);
- printf("Sorted array:\n");
- print_array(arr, n);
- return 0;
- }
- int input_array(int array[], int len)
- {
- int i; // Счётчик
- int chr; // Число из потока ввода
- for (i = 0; i < len; i++)
- {
- if (scanf("%i", &chr) == 1)
- {
- array[i] = chr;
- }
- else
- {
- return -1;
- }
- }
- return 0;
- }
- void bubble_sort(int array[], int len)
- {
- int i, j; // Счётчики
- int temporary; // Временная переменная
- for (i = 0; i < len; i++)
- {
- for (j = 0; j < len - 1; j++)
- {
- if (array[j] > array[j + 1])
- {
- temporary = array[j];
- array[j] = array[j + 1];
- array[j + 1] = temporary;
- }
- }
- }
- return;
- }
- void print_array(int array[], int len)
- {
- int i;
- for (i = 0; i < len; i++)
- {
- printf("%d ", array[i]);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement