From 13900b96af90b6d3a9a41b04943d330eb4533d15 Mon Sep 17 00:00:00 2001 From: Yana Kalsina Date: Mon, 6 Oct 2025 19:31:48 +0300 Subject: [PATCH 01/14] Add all files from hw about optimal sorting --- src/optimalSortingDir/README.md | 6 ++ src/optimalSortingDir/main.c | 41 ++++++++++++ src/optimalSortingDir/sort.c | 33 ++++++++++ src/optimalSortingDir/sort.h | 4 ++ src/optimalSortingDir/sort.s | 109 ++++++++++++++++++++++++++++++++ 5 files changed, 193 insertions(+) create mode 100644 src/optimalSortingDir/README.md create mode 100644 src/optimalSortingDir/main.c create mode 100644 src/optimalSortingDir/sort.c create mode 100644 src/optimalSortingDir/sort.h create mode 100644 src/optimalSortingDir/sort.s diff --git a/src/optimalSortingDir/README.md b/src/optimalSortingDir/README.md new file mode 100644 index 0000000..d2b5cf4 --- /dev/null +++ b/src/optimalSortingDir/README.md @@ -0,0 +1,6 @@ +Инструкция по сборке + +gcc -S -O2 sort.c -o sort.s +gcc main.c -c +gcc main.o sort.s -o main + diff --git a/src/optimalSortingDir/main.c b/src/optimalSortingDir/main.c new file mode 100644 index 0000000..bcf92e5 --- /dev/null +++ b/src/optimalSortingDir/main.c @@ -0,0 +1,41 @@ +#include "sort.h" +#include +#include + +int main() +{ + + int* onlyNumbersList = calloc(200, sizeof(int)); + int length = 0; + char nextSymbol = 0; + while (nextSymbol != '\n') { + int inputNumber = 0; + int input = scanf("%d", &inputNumber); + if (input == 1) // scanf успешно прочитал введенное число + { + onlyNumbersList[length++] = inputNumber; // добавляем число в список + } + + else { + return 1; + } + + nextSymbol = getchar(); // читаем символ, который был введен дальше + if (nextSymbol == '\n') { + break; + } + } + + // for (int i = 0; i + +int sort(int* list, int length) +{ + + int* rememberList = calloc(length, sizeof(int)); // c помощью этого списка будем счиатать элементы, изменившие свою позицию + for (int i = 0; i < length; i++) { + rememberList[i] = list[i]; + } + // копируем переданный список + + // сортировка пузырьком + for (int i = 0; i < length; i++) { + for (int j = 0; j < length - 1 - i; j++) { + if (list[j] > list[j + 1]) { + int helpingVar = list[j]; + list[j] = list[j + 1]; + list[j + 1] = helpingVar; + } + } + } + + int count = 0; + // смотрим какие элементы изменили свою позицию + for (int k = 0; k < length; k++) { + if (list[k] != rememberList[k]) { + count += 1; + } + } + free(rememberList); + return count; +} diff --git a/src/optimalSortingDir/sort.h b/src/optimalSortingDir/sort.h new file mode 100644 index 0000000..220f1e4 --- /dev/null +++ b/src/optimalSortingDir/sort.h @@ -0,0 +1,4 @@ +#pragma once + +// функция сортировки +int sort(int* list, int length); diff --git a/src/optimalSortingDir/sort.s b/src/optimalSortingDir/sort.s new file mode 100644 index 0000000..dddde7e --- /dev/null +++ b/src/optimalSortingDir/sort.s @@ -0,0 +1,109 @@ + .file "sort.c" + .text + .p2align 4 + .globl sort + .type sort, @function +sort: +.LFB11: + .cfi_startproc + pushq %r13 + .cfi_def_cfa_offset 16 + .cfi_offset 13, -16 + pushq %r12 + .cfi_def_cfa_offset 24 + .cfi_offset 12, -24 + movslq %esi, %r12 + movl $4, %esi + pushq %rbp + .cfi_def_cfa_offset 32 + .cfi_offset 6, -32 + pushq %rbx + .cfi_def_cfa_offset 40 + .cfi_offset 3, -40 + movq %rdi, %rbx + movq %r12, %rdi + subq $8, %rsp + .cfi_def_cfa_offset 48 + call calloc@PLT + movq %rax, %r8 + testl %r12d, %r12d + jle .L9 + movl %r12d, %edx + movq %rbx, %rsi + leal -1(%r12), %ebp + movq %rax, %rdi + salq $2, %rdx + call memcpy@PLT + leaq 0(,%r12,4), %rdi + movq %rax, %r8 + leaq -4(%rbx,%rdi), %rsi + .p2align 4 + .p2align 3 +.L3: + movq %rbx, %rax + testl %ebp, %ebp + jle .L7 + .p2align 6 + .p2align 4 + .p2align 3 +.L5: + movq (%rax), %xmm0 + pshufd $0xe5, %xmm0, %xmm1 + movd %xmm0, %ecx + movd %xmm1, %edx + cmpl %edx, %ecx + jle .L4 + pshufd $225, %xmm0, %xmm0 + movq %xmm0, (%rax) +.L4: + addq $4, %rax + cmpq %rsi, %rax + jne .L5 +.L7: + subl $1, %ebp + cmpq %rbx, %rsi + je .L10 + subq $4, %rsi + jmp .L3 + .p2align 4,,10 + .p2align 3 +.L10: + xorl %eax, %eax + xorl %ebp, %ebp + .p2align 5 + .p2align 4 + .p2align 3 +.L6: + movl (%r8,%rax), %esi + cmpl %esi, (%rbx,%rax) + je .L8 + addl $1, %ebp +.L8: + addq $4, %rax + cmpq %rdi, %rax + jne .L6 +.L2: + movq %r8, %rdi + call free@PLT + addq $8, %rsp + .cfi_remember_state + .cfi_def_cfa_offset 40 + movl %ebp, %eax + popq %rbx + .cfi_def_cfa_offset 32 + popq %rbp + .cfi_def_cfa_offset 24 + popq %r12 + .cfi_def_cfa_offset 16 + popq %r13 + .cfi_def_cfa_offset 8 + ret +.L9: + .cfi_restore_state + xorl %ebp, %ebp + jmp .L2 + .cfi_endproc +.LFE11: + .size sort, .-sort + .ident "GCC: (GNU) 15.2.1 20250813" + .section .note.GNU-stack,"",@progbits From 8150c1841486a803b5651ef3bf89f12c5991a337 Mon Sep 17 00:00:00 2001 From: Yana Kalsina Date: Mon, 6 Oct 2025 19:36:35 +0300 Subject: [PATCH 02/14] Remove some silly things from files --- src/optimalSortingDir/main.c | 49 ++++++++++++++++-------------------- src/optimalSortingDir/sort.c | 49 ++++++++++++++++++------------------ 2 files changed, 46 insertions(+), 52 deletions(-) diff --git a/src/optimalSortingDir/main.c b/src/optimalSortingDir/main.c index bcf92e5..9d57c76 100644 --- a/src/optimalSortingDir/main.c +++ b/src/optimalSortingDir/main.c @@ -2,40 +2,33 @@ #include #include -int main() -{ - - int* onlyNumbersList = calloc(200, sizeof(int)); - int length = 0; - char nextSymbol = 0; - while (nextSymbol != '\n') { - int inputNumber = 0; - int input = scanf("%d", &inputNumber); - if (input == 1) // scanf успешно прочитал введенное число - { - onlyNumbersList[length++] = inputNumber; // добавляем число в список - } - - else { - return 1; - } +int main() { + + int *onlyNumbersList = calloc(200, sizeof(int)); + int length = 0; + char nextSymbol = 0; + while (nextSymbol != '\n') { + int inputNumber = 0; + int input = scanf("%d", &inputNumber); + if (input == 1) // scanf успешно прочитал введенное число + { + onlyNumbersList[length++] = inputNumber; // добавляем число в список + } - nextSymbol = getchar(); // читаем символ, который был введен дальше - if (nextSymbol == '\n') { - break; - } + else { + return 1; } - // for (int i = 0; i -int sort(int* list, int length) -{ +int sort(int *list, int length) { - int* rememberList = calloc(length, sizeof(int)); // c помощью этого списка будем счиатать элементы, изменившие свою позицию - for (int i = 0; i < length; i++) { - rememberList[i] = list[i]; - } - // копируем переданный список + int *rememberList = + calloc(length, sizeof(int)); // c помощью этого списка будем счиатать + // элементы, изменившие свою позицию + for (int i = 0; i < length; i++) { + rememberList[i] = list[i]; + } + // копируем переданный список - // сортировка пузырьком - for (int i = 0; i < length; i++) { - for (int j = 0; j < length - 1 - i; j++) { - if (list[j] > list[j + 1]) { - int helpingVar = list[j]; - list[j] = list[j + 1]; - list[j + 1] = helpingVar; - } - } + // сортировка пузырьком + for (int i = 0; i < length; i++) { + for (int j = 0; j < length - 1 - i; j++) { + if (list[j] > list[j + 1]) { + int helpingVar = list[j]; + list[j] = list[j + 1]; + list[j + 1] = helpingVar; + } } + } - int count = 0; - // смотрим какие элементы изменили свою позицию - for (int k = 0; k < length; k++) { - if (list[k] != rememberList[k]) { - count += 1; - } + int count = 0; + // смотрим какие элементы изменили свою позицию + for (int k = 0; k < length; k++) { + if (list[k] != rememberList[k]) { + count += 1; } - free(rememberList); - return count; + } + free(rememberList); + return count; } From dd020eab068bd86122e4430be2e5efac1071d6c1 Mon Sep 17 00:00:00 2001 From: Yana Kalsina Date: Thu, 9 Oct 2025 17:08:59 +0300 Subject: [PATCH 03/14] Remove mistakes --- src/optimalSortingDir/README.md | 5 +++-- src/optimalSortingDir/main.c | 10 ++++------ src/optimalSortingDir/sort.c | 12 ++++++------ src/optimalSortingDir/sort.h | 5 ++++- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/optimalSortingDir/README.md b/src/optimalSortingDir/README.md index d2b5cf4..7b596fe 100644 --- a/src/optimalSortingDir/README.md +++ b/src/optimalSortingDir/README.md @@ -1,6 +1,7 @@ Инструкция по сборке gcc -S -O2 sort.c -o sort.s -gcc main.c -c -gcc main.o sort.s -o main +gcc -Wall -Wextra -pedantic main.c -c + +gcc -Wall -Wextra -pedantic main.o sort.s -o main diff --git a/src/optimalSortingDir/main.c b/src/optimalSortingDir/main.c index 9d57c76..037fc3b 100644 --- a/src/optimalSortingDir/main.c +++ b/src/optimalSortingDir/main.c @@ -4,7 +4,7 @@ int main() { - int *onlyNumbersList = calloc(200, sizeof(int)); + int *MassiveWithNumbers = calloc(100, sizeof(int)); int length = 0; char nextSymbol = 0; while (nextSymbol != '\n') { @@ -12,23 +12,21 @@ int main() { int input = scanf("%d", &inputNumber); if (input == 1) // scanf успешно прочитал введенное число { - onlyNumbersList[length++] = inputNumber; // добавляем число в список + MassiveWithNumbers[length++] = inputNumber; // добавляем число в список } - else { return 1; } - nextSymbol = getchar(); // читаем символ, который был введен дальше if (nextSymbol == '\n') { break; } } - printf("%d\n", sort(onlyNumbersList, length)); + printf("%d\n", sort(MassiveWithNumbers, length)); return 0; } -// пример: ввод 1 2 3 4 +// пример: ввод 4 3 2 1 // должно вернуть : 4, так как каждая цифра изменила свою позицию diff --git a/src/optimalSortingDir/sort.c b/src/optimalSortingDir/sort.c index 5bd788a..c1ef5b7 100644 --- a/src/optimalSortingDir/sort.c +++ b/src/optimalSortingDir/sort.c @@ -3,11 +3,11 @@ int sort(int *list, int length) { - int *rememberList = + int *unsortedMassive = calloc(length, sizeof(int)); // c помощью этого списка будем счиатать // элементы, изменившие свою позицию for (int i = 0; i < length; i++) { - rememberList[i] = list[i]; + unsortedMassive[i] = list[i]; } // копируем переданный список @@ -15,9 +15,9 @@ int sort(int *list, int length) { for (int i = 0; i < length; i++) { for (int j = 0; j < length - 1 - i; j++) { if (list[j] > list[j + 1]) { - int helpingVar = list[j]; + int tmp = list[j]; list[j] = list[j + 1]; - list[j + 1] = helpingVar; + list[j + 1] = tmp; } } } @@ -25,10 +25,10 @@ int sort(int *list, int length) { int count = 0; // смотрим какие элементы изменили свою позицию for (int k = 0; k < length; k++) { - if (list[k] != rememberList[k]) { + if (list[k] != unsortedMassive[k]) { count += 1; } } - free(rememberList); + free(unsortedMassive); return count; } diff --git a/src/optimalSortingDir/sort.h b/src/optimalSortingDir/sort.h index 220f1e4..3b4e76e 100644 --- a/src/optimalSortingDir/sort.h +++ b/src/optimalSortingDir/sort.h @@ -1,4 +1,7 @@ #pragma once // функция сортировки -int sort(int* list, int length); +// функция принимает указатель на первый эллемент массива и длину массива +// возвращает кол-во элементов, изменивших свою позицию +// сортировка реализуется пузырьковым методом +int sort(int *list, int length); From 219ddcc8991d7e24c0f24096e6d4e047e8bbf38d Mon Sep 17 00:00:00 2001 From: Yana Kalsina Date: Fri, 10 Oct 2025 21:15:12 +0300 Subject: [PATCH 04/14] Changed README --- src/optimalSortingDir/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/optimalSortingDir/README.md b/src/optimalSortingDir/README.md index 7b596fe..2611945 100644 --- a/src/optimalSortingDir/README.md +++ b/src/optimalSortingDir/README.md @@ -1,6 +1,6 @@ Инструкция по сборке -gcc -S -O2 sort.c -o sort.s +gcc -Wall -Wextra -pedantic -S -O2 sort.c -o sort.s gcc -Wall -Wextra -pedantic main.c -c From ade98caa1cc5875d70aee43204237ad27876c93a Mon Sep 17 00:00:00 2001 From: Yana Kalsina Date: Fri, 10 Oct 2025 21:22:18 +0300 Subject: [PATCH 05/14] Change 'massive' into 'array' in some files --- src/optimalSortingDir/main.c | 6 +++--- src/optimalSortingDir/sort.c | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/optimalSortingDir/main.c b/src/optimalSortingDir/main.c index 037fc3b..071ad58 100644 --- a/src/optimalSortingDir/main.c +++ b/src/optimalSortingDir/main.c @@ -4,7 +4,7 @@ int main() { - int *MassiveWithNumbers = calloc(100, sizeof(int)); + int *ArrayWithNumbers = calloc(100, sizeof(int)); int length = 0; char nextSymbol = 0; while (nextSymbol != '\n') { @@ -12,7 +12,7 @@ int main() { int input = scanf("%d", &inputNumber); if (input == 1) // scanf успешно прочитал введенное число { - MassiveWithNumbers[length++] = inputNumber; // добавляем число в список + ArrayWithNumbers[length++] = inputNumber; // добавляем число в список } else { return 1; @@ -23,7 +23,7 @@ int main() { } } - printf("%d\n", sort(MassiveWithNumbers, length)); + printf("%d\n", sort(ArrayWithNumbers, length)); return 0; } diff --git a/src/optimalSortingDir/sort.c b/src/optimalSortingDir/sort.c index c1ef5b7..500d5a3 100644 --- a/src/optimalSortingDir/sort.c +++ b/src/optimalSortingDir/sort.c @@ -3,11 +3,11 @@ int sort(int *list, int length) { - int *unsortedMassive = + int *unsortedArray = calloc(length, sizeof(int)); // c помощью этого списка будем счиатать // элементы, изменившие свою позицию for (int i = 0; i < length; i++) { - unsortedMassive[i] = list[i]; + unsortedArray[i] = list[i]; } // копируем переданный список @@ -25,10 +25,10 @@ int sort(int *list, int length) { int count = 0; // смотрим какие элементы изменили свою позицию for (int k = 0; k < length; k++) { - if (list[k] != unsortedMassive[k]) { + if (list[k] != unsortedArray[k]) { count += 1; } } - free(unsortedMassive); + free(unsortedArray); return count; } From 70fba14dc716e5951d6a1098761462b5a9f871ed Mon Sep 17 00:00:00 2001 From: Yana Kalsina Date: Fri, 10 Oct 2025 21:41:50 +0300 Subject: [PATCH 06/14] Change list into array in sort.h and sort.c --- src/optimalSortingDir/sort.c | 14 +++++++------- src/optimalSortingDir/sort.h | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/optimalSortingDir/sort.c b/src/optimalSortingDir/sort.c index 500d5a3..b438eae 100644 --- a/src/optimalSortingDir/sort.c +++ b/src/optimalSortingDir/sort.c @@ -1,23 +1,23 @@ #include "sort.h" #include -int sort(int *list, int length) { +int sort(int *array, int length) { int *unsortedArray = calloc(length, sizeof(int)); // c помощью этого списка будем счиатать // элементы, изменившие свою позицию for (int i = 0; i < length; i++) { - unsortedArray[i] = list[i]; + unsortedArray[i] = array[i]; } // копируем переданный список // сортировка пузырьком for (int i = 0; i < length; i++) { for (int j = 0; j < length - 1 - i; j++) { - if (list[j] > list[j + 1]) { - int tmp = list[j]; - list[j] = list[j + 1]; - list[j + 1] = tmp; + if (array[j] > array[j + 1]) { + int tmp = array[j]; + array[j] = array[j + 1]; + array[j + 1] = tmp; } } } @@ -25,7 +25,7 @@ int sort(int *list, int length) { int count = 0; // смотрим какие элементы изменили свою позицию for (int k = 0; k < length; k++) { - if (list[k] != unsortedArray[k]) { + if (array[k] != unsortedArray[k]) { count += 1; } } diff --git a/src/optimalSortingDir/sort.h b/src/optimalSortingDir/sort.h index 3b4e76e..68aae17 100644 --- a/src/optimalSortingDir/sort.h +++ b/src/optimalSortingDir/sort.h @@ -4,4 +4,4 @@ // функция принимает указатель на первый эллемент массива и длину массива // возвращает кол-во элементов, изменивших свою позицию // сортировка реализуется пузырьковым методом -int sort(int *list, int length); +int sort(int *array, int length); From dc8deb2e53ada506fac3c42b3e7f1894f8b10584 Mon Sep 17 00:00:00 2001 From: Yana Kalsina Date: Fri, 10 Oct 2025 21:44:28 +0300 Subject: [PATCH 07/14] used clang-format to main.c --- src/optimalSortingDir/main.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/optimalSortingDir/main.c b/src/optimalSortingDir/main.c index 071ad58..16779ae 100644 --- a/src/optimalSortingDir/main.c +++ b/src/optimalSortingDir/main.c @@ -13,8 +13,7 @@ int main() { if (input == 1) // scanf успешно прочитал введенное число { ArrayWithNumbers[length++] = inputNumber; // добавляем число в список - } - else { + } else { return 1; } nextSymbol = getchar(); // читаем символ, который был введен дальше From ab13a47054a6cb6d97a67731b3618849e3c52407 Mon Sep 17 00:00:00 2001 From: Yana Kalsina Date: Sat, 11 Oct 2025 17:59:09 +0300 Subject: [PATCH 08/14] Fix some syntactic mistakes --- src/optimalSortingDir/main.c | 15 +++++++++------ src/optimalSortingDir/sort.c | 16 +++++----------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/optimalSortingDir/main.c b/src/optimalSortingDir/main.c index 16779ae..49b8220 100644 --- a/src/optimalSortingDir/main.c +++ b/src/optimalSortingDir/main.c @@ -4,26 +4,29 @@ int main() { - int *ArrayWithNumbers = calloc(100, sizeof(int)); + int *arrayWithNumbers = calloc(100, sizeof(int)); int length = 0; char nextSymbol = 0; while (nextSymbol != '\n') { int inputNumber = 0; int input = scanf("%d", &inputNumber); - if (input == 1) // scanf успешно прочитал введенное число + // scanf успешно прочитал введенное число + printf("%d", input); + if (input == 1) { - ArrayWithNumbers[length++] = inputNumber; // добавляем число в список + // добавляем число в список + arrayWithNumbers[length++] = inputNumber; } else { return 1; } - nextSymbol = getchar(); // читаем символ, который был введен дальше + // читаем символ, который был введен дальше + nextSymbol = getchar(); if (nextSymbol == '\n') { break; } } - printf("%d\n", sort(ArrayWithNumbers, length)); - + printf("%d\n", sort(arrayWithNumbers, length)); return 0; } diff --git a/src/optimalSortingDir/sort.c b/src/optimalSortingDir/sort.c index b438eae..77722de 100644 --- a/src/optimalSortingDir/sort.c +++ b/src/optimalSortingDir/sort.c @@ -1,16 +1,11 @@ #include "sort.h" #include +#include int sort(int *array, int length) { - - int *unsortedArray = - calloc(length, sizeof(int)); // c помощью этого списка будем счиатать - // элементы, изменившие свою позицию - for (int i = 0; i < length; i++) { - unsortedArray[i] = array[i]; - } - // копируем переданный список - + // c помощью этого списка будем считать элементы, изменившие свою позицию + int *unsortedArray = calloc(length, sizeof(int)); + memcpy(unsortedArray, array, sizeof(int)*length); // сортировка пузырьком for (int i = 0; i < length; i++) { for (int j = 0; j < length - 1 - i; j++) { @@ -21,12 +16,11 @@ int sort(int *array, int length) { } } } - int count = 0; // смотрим какие элементы изменили свою позицию for (int k = 0; k < length; k++) { if (array[k] != unsortedArray[k]) { - count += 1; + ++count; } } free(unsortedArray); From 97ee39ccb2593a079d86891dbba5d50e29b451f7 Mon Sep 17 00:00:00 2001 From: Yana Kalsina Date: Sat, 11 Oct 2025 23:17:38 +0300 Subject: [PATCH 09/14] Remove one useless line --- src/optimalSortingDir/main.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/optimalSortingDir/main.c b/src/optimalSortingDir/main.c index 49b8220..779ca84 100644 --- a/src/optimalSortingDir/main.c +++ b/src/optimalSortingDir/main.c @@ -11,7 +11,6 @@ int main() { int inputNumber = 0; int input = scanf("%d", &inputNumber); // scanf успешно прочитал введенное число - printf("%d", input); if (input == 1) { // добавляем число в список From eb6413ff1dfb710052a15ce481b16b3894227322 Mon Sep 17 00:00:00 2001 From: Yana Kalsina Date: Sat, 11 Oct 2025 23:20:12 +0300 Subject: [PATCH 10/14] Removed one empty line --- src/optimalSortingDir/main.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/optimalSortingDir/main.c b/src/optimalSortingDir/main.c index 779ca84..d64ffe9 100644 --- a/src/optimalSortingDir/main.c +++ b/src/optimalSortingDir/main.c @@ -24,7 +24,6 @@ int main() { break; } } - printf("%d\n", sort(arrayWithNumbers, length)); return 0; } From cf60c923273cf575a19b4831c861e553045fd9ec Mon Sep 17 00:00:00 2001 From: Yana Kalsina Date: Sat, 11 Oct 2025 23:26:16 +0300 Subject: [PATCH 11/14] Update sort.s, use clang-format to main.c and sort.c --- src/optimalSortingDir/main.c | 3 +- src/optimalSortingDir/sort.c | 2 +- src/optimalSortingDir/sort.s | 82 ++++++++++++++++++------------------ 3 files changed, 42 insertions(+), 45 deletions(-) diff --git a/src/optimalSortingDir/main.c b/src/optimalSortingDir/main.c index d64ffe9..677da21 100644 --- a/src/optimalSortingDir/main.c +++ b/src/optimalSortingDir/main.c @@ -11,8 +11,7 @@ int main() { int inputNumber = 0; int input = scanf("%d", &inputNumber); // scanf успешно прочитал введенное число - if (input == 1) - { + if (input == 1) { // добавляем число в список arrayWithNumbers[length++] = inputNumber; } else { diff --git a/src/optimalSortingDir/sort.c b/src/optimalSortingDir/sort.c index 77722de..025560c 100644 --- a/src/optimalSortingDir/sort.c +++ b/src/optimalSortingDir/sort.c @@ -5,7 +5,7 @@ int sort(int *array, int length) { // c помощью этого списка будем считать элементы, изменившие свою позицию int *unsortedArray = calloc(length, sizeof(int)); - memcpy(unsortedArray, array, sizeof(int)*length); + memcpy(unsortedArray, array, sizeof(int) * length); // сортировка пузырьком for (int i = 0; i < length; i++) { for (int j = 0; j < length - 1 - i; j++) { diff --git a/src/optimalSortingDir/sort.s b/src/optimalSortingDir/sort.s index dddde7e..4e35cde 100644 --- a/src/optimalSortingDir/sort.s +++ b/src/optimalSortingDir/sort.s @@ -6,42 +6,36 @@ sort: .LFB11: .cfi_startproc - pushq %r13 - .cfi_def_cfa_offset 16 - .cfi_offset 13, -16 pushq %r12 - .cfi_def_cfa_offset 24 - .cfi_offset 12, -24 + .cfi_def_cfa_offset 16 + .cfi_offset 12, -16 movslq %esi, %r12 movl $4, %esi pushq %rbp - .cfi_def_cfa_offset 32 - .cfi_offset 6, -32 - pushq %rbx - .cfi_def_cfa_offset 40 - .cfi_offset 3, -40 - movq %rdi, %rbx + .cfi_def_cfa_offset 24 + .cfi_offset 6, -24 + movq %rdi, %rbp movq %r12, %rdi - subq $8, %rsp - .cfi_def_cfa_offset 48 + pushq %rbx + .cfi_def_cfa_offset 32 + .cfi_offset 3, -32 + movq %r12, %rbx + salq $2, %r12 call calloc@PLT - movq %rax, %r8 - testl %r12d, %r12d - jle .L9 - movl %r12d, %edx - movq %rbx, %rsi - leal -1(%r12), %ebp + movq %r12, %rdx + movq %rbp, %rsi movq %rax, %rdi - salq $2, %rdx call memcpy@PLT - leaq 0(,%r12,4), %rdi movq %rax, %r8 - leaq -4(%rbx,%rdi), %rsi + testl %ebx, %ebx + jle .L9 + leal -1(%rbx), %edi + leaq -4(%rbp,%r12), %rsi .p2align 4 .p2align 3 .L3: - movq %rbx, %rax - testl %ebp, %ebp + movq %rbp, %rax + testl %edi, %edi jle .L7 .p2align 6 .p2align 4 @@ -60,8 +54,8 @@ sort: cmpq %rsi, %rax jne .L5 .L7: - subl $1, %ebp - cmpq %rbx, %rsi + subl $1, %edi + cmpq %rbp, %rsi je .L10 subq $4, %rsi jmp .L3 @@ -69,39 +63,43 @@ sort: .p2align 3 .L10: xorl %eax, %eax - xorl %ebp, %ebp + xorl %ebx, %ebx .p2align 5 .p2align 4 .p2align 3 .L6: - movl (%r8,%rax), %esi - cmpl %esi, (%rbx,%rax) + movl (%r8,%rax), %edi + cmpl %edi, 0(%rbp,%rax) je .L8 - addl $1, %ebp + addl $1, %ebx .L8: addq $4, %rax - cmpq %rdi, %rax + cmpq %rax, %r12 jne .L6 -.L2: movq %r8, %rdi call free@PLT - addq $8, %rsp - .cfi_remember_state - .cfi_def_cfa_offset 40 - movl %ebp, %eax + movl %ebx, %eax popq %rbx - .cfi_def_cfa_offset 32 - popq %rbp + .cfi_remember_state .cfi_def_cfa_offset 24 - popq %r12 + popq %rbp .cfi_def_cfa_offset 16 - popq %r13 + popq %r12 .cfi_def_cfa_offset 8 ret .L9: .cfi_restore_state - xorl %ebp, %ebp - jmp .L2 + xorl %ebx, %ebx + movq %r8, %rdi + call free@PLT + movl %ebx, %eax + popq %rbx + .cfi_def_cfa_offset 24 + popq %rbp + .cfi_def_cfa_offset 16 + popq %r12 + .cfi_def_cfa_offset 8 + ret .cfi_endproc .LFE11: .size sort, .-sort From bde99cc869e1170434527bdca2bca500d936e793 Mon Sep 17 00:00:00 2001 From: Yana Kalsina Date: Sat, 11 Oct 2025 23:45:27 +0300 Subject: [PATCH 12/14] Remove empty line --- src/optimalSortingDir/main.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/optimalSortingDir/main.c b/src/optimalSortingDir/main.c index 677da21..8178817 100644 --- a/src/optimalSortingDir/main.c +++ b/src/optimalSortingDir/main.c @@ -3,7 +3,6 @@ #include int main() { - int *arrayWithNumbers = calloc(100, sizeof(int)); int length = 0; char nextSymbol = 0; From fe3ecb74bbb185e991d3c675f1b70dec3fafab56 Mon Sep 17 00:00:00 2001 From: Yana Kalsina Date: Fri, 28 Nov 2025 21:05:51 +0300 Subject: [PATCH 13/14] Add cmake and clang-format config --- .clang-format | 244 +++++++++++++++++++++++++++ CMakeLists.txt | 7 + src/optimalSortingDir/CMakeLists.txt | 3 + 3 files changed, 254 insertions(+) create mode 100644 .clang-format create mode 100644 CMakeLists.txt create mode 100644 src/optimalSortingDir/CMakeLists.txt diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..84ea374 --- /dev/null +++ b/.clang-format @@ -0,0 +1,244 @@ +--- +Language: Cpp +# BasedOnStyle: WebKit +AccessModifierOffset: -4 +AlignAfterOpenBracket: DontAlign +AlignArrayOfStructures: None +AlignConsecutiveAssignments: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: true +AlignConsecutiveBitFields: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveDeclarations: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveMacros: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCompound: false + AlignFunctionPointers: false + PadOperators: false +AlignConsecutiveShortCaseStatements: + Enabled: false + AcrossEmptyLines: false + AcrossComments: false + AlignCaseColons: false +AlignEscapedNewlines: Right +AlignOperands: DontAlign +AlignTrailingComments: + Kind: Never + OverEmptyLines: 0 +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowBreakBeforeNoexceptSpecifier: Never +AllowShortBlocksOnASingleLine: Empty +AllowShortCaseLabelsOnASingleLine: false +AllowShortCompoundRequirementOnASingleLine: true +AllowShortEnumsOnASingleLine: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterExternBlock: false + AfterFunction: true + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakAdjacentStringLiterals: true +BreakAfterAttributes: Leave +BreakAfterJavaFieldAnnotations: false +BreakArrays: true +BreakBeforeBinaryOperators: All +BreakBeforeConceptDeclarations: Always +BreakBeforeBraces: WebKit +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeComma +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +ColumnLimit: 0 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: false +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: false +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseBlocks: false +IndentCaseLabels: false +IndentExternBlock: AfterExternBlock +IndentGotoLabels: true +IndentPPDirectives: None +IndentRequiresClause: true +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertBraces: false +InsertNewlineAtEOF: false +InsertTrailingCommas: None +IntegerLiteralSeparator: + Binary: 0 + BinaryMinDigits: 0 + Decimal: 0 + DecimalMinDigits: 0 + Hex: 0 + HexMinDigits: 0 +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +KeepEmptyLinesAtEOF: false +LambdaBodyIndentation: Signature +LineEnding: DeriveLF +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: Inner +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 4 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: true +ObjCSpaceBeforeProtocolList: true +PackConstructorInitializers: BinPack +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakScopeResolution: 500 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 0 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerAlignment: Left +PPIndentWidth: -1 +QualifierAlignment: Leave +ReferenceAlignment: Pointer +ReflowComments: true +RemoveBracesLLVM: false +RemoveParentheses: Leave +RemoveSemicolon: false +RequiresClausePosition: OwnLine +RequiresExpressionIndentation: OuterScope +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SkipMacroDefinitionBody: false +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: LexicographicNumeric +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: true +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeJsonColon: false +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: true + AfterOverloadedOperator: false + AfterPlacementOperator: true + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: true +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInContainerLiterals: true +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParens: Never +SpacesInParensOptions: + InCStyleCasts: false + InConditionalStatements: false + InEmptyParentheses: false + Other: false +SpacesInSquareBrackets: false +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseTab: Never +VerilogBreakBetweenInstancePorts: true +WhitespaceSensitiveMacros: + - BOOST_PP_STRINGIZE + - CF_SWIFT_NAME + - NS_SWIFT_NAME + - PP_STRINGIZE + - STRINGIZE \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..050faad --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.25) + +project(C_Homework C) + +add_subdirectory(src/optimalSortingDir) + +add_compile_options(-Wall -Wextra -Wpedantic) \ No newline at end of file diff --git a/src/optimalSortingDir/CMakeLists.txt b/src/optimalSortingDir/CMakeLists.txt new file mode 100644 index 0000000..f80a4c3 --- /dev/null +++ b/src/optimalSortingDir/CMakeLists.txt @@ -0,0 +1,3 @@ +add_library(sort sort.c) +add_executable(main main.c) +target_link_libraries(main PRIVATE sort) \ No newline at end of file From 281c073e9dcd61c1d1d00b4701c83ffa4816860b Mon Sep 17 00:00:00 2001 From: Yana Kalsina Date: Fri, 28 Nov 2025 21:08:07 +0300 Subject: [PATCH 14/14] Formatted files --- src/optimalSortingDir/main.c | 43 ++++++++++++++++++------------------ src/optimalSortingDir/sort.c | 43 ++++++++++++++++++------------------ src/optimalSortingDir/sort.h | 2 +- 3 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/optimalSortingDir/main.c b/src/optimalSortingDir/main.c index 8178817..2dc4673 100644 --- a/src/optimalSortingDir/main.c +++ b/src/optimalSortingDir/main.c @@ -2,28 +2,29 @@ #include #include -int main() { - int *arrayWithNumbers = calloc(100, sizeof(int)); - int length = 0; - char nextSymbol = 0; - while (nextSymbol != '\n') { - int inputNumber = 0; - int input = scanf("%d", &inputNumber); - // scanf успешно прочитал введенное число - if (input == 1) { - // добавляем число в список - arrayWithNumbers[length++] = inputNumber; - } else { - return 1; +int main() +{ + int* arrayWithNumbers = calloc(100, sizeof(int)); + int length = 0; + char nextSymbol = 0; + while (nextSymbol != '\n') { + int inputNumber = 0; + int input = scanf("%d", &inputNumber); + // scanf успешно прочитал введенное число + if (input == 1) { + // добавляем число в список + arrayWithNumbers[length++] = inputNumber; + } else { + return 1; + } + // читаем символ, который был введен дальше + nextSymbol = getchar(); + if (nextSymbol == '\n') { + break; + } } - // читаем символ, который был введен дальше - nextSymbol = getchar(); - if (nextSymbol == '\n') { - break; - } - } - printf("%d\n", sort(arrayWithNumbers, length)); - return 0; + printf("%d\n", sort(arrayWithNumbers, length)); + return 0; } // пример: ввод 4 3 2 1 diff --git a/src/optimalSortingDir/sort.c b/src/optimalSortingDir/sort.c index 025560c..0de45ae 100644 --- a/src/optimalSortingDir/sort.c +++ b/src/optimalSortingDir/sort.c @@ -2,27 +2,28 @@ #include #include -int sort(int *array, int length) { - // c помощью этого списка будем считать элементы, изменившие свою позицию - int *unsortedArray = calloc(length, sizeof(int)); - memcpy(unsortedArray, array, sizeof(int) * length); - // сортировка пузырьком - for (int i = 0; i < length; i++) { - for (int j = 0; j < length - 1 - i; j++) { - if (array[j] > array[j + 1]) { - int tmp = array[j]; - array[j] = array[j + 1]; - array[j + 1] = tmp; - } +int sort(int* array, int length) +{ + // c помощью этого списка будем считать элементы, изменившие свою позицию + int* unsortedArray = calloc(length, sizeof(int)); + memcpy(unsortedArray, array, sizeof(int) * length); + // сортировка пузырьком + for (int i = 0; i < length; i++) { + for (int j = 0; j < length - 1 - i; j++) { + if (array[j] > array[j + 1]) { + int tmp = array[j]; + array[j] = array[j + 1]; + array[j + 1] = tmp; + } + } } - } - int count = 0; - // смотрим какие элементы изменили свою позицию - for (int k = 0; k < length; k++) { - if (array[k] != unsortedArray[k]) { - ++count; + int count = 0; + // смотрим какие элементы изменили свою позицию + for (int k = 0; k < length; k++) { + if (array[k] != unsortedArray[k]) { + ++count; + } } - } - free(unsortedArray); - return count; + free(unsortedArray); + return count; } diff --git a/src/optimalSortingDir/sort.h b/src/optimalSortingDir/sort.h index 68aae17..68ac648 100644 --- a/src/optimalSortingDir/sort.h +++ b/src/optimalSortingDir/sort.h @@ -4,4 +4,4 @@ // функция принимает указатель на первый эллемент массива и длину массива // возвращает кол-во элементов, изменивших свою позицию // сортировка реализуется пузырьковым методом -int sort(int *array, int length); +int sort(int* array, int length);