mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 05:21:49 +03:00
cdf8453db8
* Revert "Merge pull request #513 from tania-cmyk/master" This reverts commit5eebdedefc
, reversing changes made to53e92ebc05
. * formatting source-code for16110cd079
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
46 lines
911 B
C
46 lines
911 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
void sort(int *numbers, int size)
|
|
{
|
|
int pos = 0;
|
|
while (pos < size)
|
|
{
|
|
if (numbers[pos] >= numbers[pos - 1])
|
|
pos++;
|
|
else
|
|
{
|
|
int tmp = numbers[pos - 1];
|
|
numbers[pos - 1] = numbers[pos];
|
|
numbers[pos] = tmp;
|
|
pos--;
|
|
|
|
if (pos == 0)
|
|
pos = 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
void display(int *array, int n)
|
|
{
|
|
int i;
|
|
for (i = 0; i < n; ++i) printf("%d ", array[i]);
|
|
printf("\n");
|
|
}
|
|
|
|
int main()
|
|
{
|
|
int size = 6;
|
|
int i;
|
|
int *numbers = malloc(size * sizeof(int));
|
|
printf("Insert %d unsorted numbers: \n", size);
|
|
for (i = 0; i < size; ++i) scanf("%d", &numbers[i]);
|
|
printf("Initial array: ");
|
|
display(numbers, size);
|
|
sort(numbers, size);
|
|
printf("Sorted array: ");
|
|
display(numbers, size);
|
|
free(numbers);
|
|
return 0;
|
|
}
|