2019-08-08 12:56:39 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-06-28 18:25:37 +03:00
|
|
|
#define SHRINK 1.3 // suggested shrink factor value
|
2019-08-08 12:56:39 +03:00
|
|
|
|
2020-05-29 23:23:24 +03:00
|
|
|
void sort(int *numbers, int size)
|
2019-08-08 12:56:39 +03:00
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
int gap = size;
|
2020-06-28 18:25:37 +03:00
|
|
|
while (gap > 1) // gap = 1 means that the array is sorted
|
2020-05-29 23:23:24 +03:00
|
|
|
{
|
|
|
|
gap = gap / SHRINK;
|
|
|
|
int i = 0;
|
|
|
|
while ((i + gap) < size)
|
2020-06-28 18:25:37 +03:00
|
|
|
{ // similiar to the Shell Sort
|
2020-05-29 23:23:24 +03:00
|
|
|
if (numbers[i] > numbers[i + gap])
|
|
|
|
{
|
|
|
|
int tmp = numbers[i];
|
|
|
|
numbers[i] = numbers[i + gap];
|
|
|
|
numbers[i + gap] = tmp;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
2019-08-08 12:56:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void display(int *array, int n)
|
|
|
|
{
|
2019-08-25 14:15:06 +03:00
|
|
|
int i;
|
2020-06-28 18:25:37 +03:00
|
|
|
for (i = 0; i < n; ++i) printf("%d ", array[i]);
|
2019-08-08 12:56:39 +03:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2020-05-29 23:23:24 +03:00
|
|
|
int size = 6;
|
|
|
|
int *numbers = malloc(size * sizeof(int));
|
|
|
|
printf("Insert %d unsorted numbers: \n", size);
|
|
|
|
int i;
|
2020-06-28 18:25:37 +03:00
|
|
|
for (i = 0; i < size; ++i) scanf("%d", &numbers[i]);
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("Initial array: ");
|
|
|
|
display(numbers, size);
|
2019-08-08 12:56:39 +03:00
|
|
|
sort(numbers, size);
|
2020-05-29 23:23:24 +03:00
|
|
|
printf("Sorted array: ");
|
2019-08-08 12:56:39 +03:00
|
|
|
display(numbers, size);
|
|
|
|
free(numbers);
|
2020-05-29 23:23:24 +03:00
|
|
|
return 0;
|
2019-08-08 12:56:39 +03:00
|
|
|
}
|