From dfc6e5988b8bc95410038cab0988737bf16047c0 Mon Sep 17 00:00:00 2001 From: DolzhenkoAlexa Date: Tue, 7 Oct 2025 20:50:06 +0000 Subject: [PATCH 1/2] Add homework optimal sort --- src/hw_Optimal_Sort/main.c | 25 +++++++++++++++++++++++++ src/hw_Optimal_Sort/sort.c | 16 ++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/hw_Optimal_Sort/main.c create mode 100644 src/hw_Optimal_Sort/sort.c diff --git a/src/hw_Optimal_Sort/main.c b/src/hw_Optimal_Sort/main.c new file mode 100644 index 0000000..ea4fa97 --- /dev/null +++ b/src/hw_Optimal_Sort/main.c @@ -0,0 +1,25 @@ +#include + +// Объявление функции +extern int sort_array(int array[], int count); + +// Основной код - объявление массива на 100 чисел, создание счетчика, чтобы знать со сколькими числами надо будет работать, чтение чисел через for с scanf и вывод +int main() { + int array[100]; + int count = 0; + + int i = 0; + while (i < 100 && scanf("%d", &array[i]) == 1) { + i++; + count++; + } + + int moved = sort_array(array, count); + + printf("Отсортированный массив:"); + for (int i = 0; i < count; i++) { + printf("%d ", array[i]); + } + printf("\n"); + return 0; +} 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; +} From 3a2a3a82af71bb1621a3bb88ecbd0d6c4f6b0e7f Mon Sep 17 00:00:00 2001 From: DolzhenkoAlexa Date: Tue, 23 Dec 2025 20:21:37 +0300 Subject: [PATCH 2/2] Update main.c --- src/hw_Optimal_Sort/main.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/hw_Optimal_Sort/main.c b/src/hw_Optimal_Sort/main.c index ea4fa97..be27b34 100644 --- a/src/hw_Optimal_Sort/main.c +++ b/src/hw_Optimal_Sort/main.c @@ -1,25 +1,16 @@ #include +#include "sort.h" -// Объявление функции -extern int sort_array(int array[], int count); - -// Основной код - объявление массива на 100 чисел, создание счетчика, чтобы знать со сколькими числами надо будет работать, чтение чисел через for с scanf и вывод -int main() { +int main() +{ int array[100]; int count = 0; - - int i = 0; - while (i < 100 && scanf("%d", &array[i]) == 1) { - i++; + + while (count < 100 && scanf("%d", &array[count]) == 1) { count++; } - + int moved = sort_array(array, count); - - printf("Отсортированный массив:"); - for (int i = 0; i < count; i++) { - printf("%d ", array[i]); - } - printf("\n"); - return 0; + + return moved; }