diff --git a/src/hw_Optimal_Sort/main.c b/src/hw_Optimal_Sort/main.c new file mode 100644 index 0000000..be27b34 --- /dev/null +++ b/src/hw_Optimal_Sort/main.c @@ -0,0 +1,16 @@ +#include +#include "sort.h" + +int main() +{ + int array[100]; + int count = 0; + + while (count < 100 && scanf("%d", &array[count]) == 1) { + count++; + } + + int moved = sort_array(array, count); + + return moved; +} diff --git a/src/hw_Optimal_Sort/sort.c b/src/hw_Optimal_Sort/sort.c new file mode 100644 index 0000000..b535d0e --- /dev/null +++ b/src/hw_Optimal_Sort/sort.c @@ -0,0 +1,16 @@ +int sort_array(int* array, int count) { + int moved_count = 0; + // сортировка пузырьком + for (int i = 0; i < count - 1; i++) { + for (int j = 0; j < count - i - 1; j++) { + if (array[j] > array[j + 1]) { + int temp = array[j]; + array[j] = array[j + 1]; + array[j + 1] = temp; + moved_count++; + } + } + } + + return moved_count; +}