2019-08-10 13:05:10 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
void sort(int *numbers, int size)
|
|
|
|
{
|
|
|
|
int pos = 0;
|
|
|
|
while (pos < size)
|
|
|
|
{
|
2020-07-08 15:54:58 +03:00
|
|
|
if (numbers[pos] >= numbers[pos - 1])
|
2019-08-10 13:05:10 +03:00
|
|
|
pos++;
|
2020-02-02 11:15:59 +03:00
|
|
|
else
|
2019-08-10 13:05:10 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
int tmp = numbers[pos - 1];
|
|
|
|
numbers[pos - 1] = numbers[pos];
|
2019-08-10 13:05:10 +03:00
|
|
|
numbers[pos] = tmp;
|
|
|
|
pos--;
|
2020-07-08 15:54:58 +03:00
|
|
|
|
|
|
|
if (pos == 0)
|
|
|
|
pos = 1;
|
2019-08-10 13:05:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void display(int *array, int n)
|
|
|
|
{
|
2019-08-10 15:17:27 +03:00
|
|
|
int i;
|
2020-06-28 18:25:37 +03:00
|
|
|
for (i = 0; i < n; ++i) printf("%d ", array[i]);
|
2019-08-10 13:05:10 +03:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int size = 6;
|
2019-08-10 14:19:05 +03:00
|
|
|
int i;
|
2020-05-29 23:23:24 +03:00
|
|
|
int *numbers = malloc(size * sizeof(int));
|
2019-08-10 13:05:10 +03:00
|
|
|
printf("Insert %d unsorted numbers: \n", size);
|
2020-06-28 18:25:37 +03:00
|
|
|
for (i = 0; i < size; ++i) scanf("%d", &numbers[i]);
|
2019-08-10 13:05:10 +03:00
|
|
|
printf("Initial array: ");
|
|
|
|
display(numbers, size);
|
|
|
|
sort(numbers, size);
|
|
|
|
printf("Sorted array: ");
|
|
|
|
display(numbers, size);
|
|
|
|
free(numbers);
|
|
|
|
return 0;
|
|
|
|
}
|