diff --git a/.clang-format b/.clang-format index 3754daf..84ea374 100644 --- a/.clang-format +++ b/.clang-format @@ -241,4 +241,4 @@ WhitespaceSensitiveMacros: - CF_SWIFT_NAME - NS_SWIFT_NAME - PP_STRINGIZE - - STRINGIZE + - STRINGIZE \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 2b068e4..5e92337 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.25) project(C_Homework C) +add_subdirectory(src/optimalSortingDir) add_subdirectory(src/CheckHowLintersWork) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 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 diff --git a/src/optimalSortingDir/README.md b/src/optimalSortingDir/README.md new file mode 100644 index 0000000..2611945 --- /dev/null +++ b/src/optimalSortingDir/README.md @@ -0,0 +1,7 @@ +Инструкция по сборке + +gcc -Wall -Wextra -pedantic -S -O2 sort.c -o sort.s + +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 new file mode 100644 index 0000000..2dc4673 --- /dev/null +++ b/src/optimalSortingDir/main.c @@ -0,0 +1,31 @@ +#include "sort.h" +#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; + } + // читаем символ, который был введен дальше + nextSymbol = getchar(); + if (nextSymbol == '\n') { + break; + } + } + printf("%d\n", sort(arrayWithNumbers, length)); + return 0; +} + +// пример: ввод 4 3 2 1 +// должно вернуть : 4, так как каждая цифра изменила свою позицию diff --git a/src/optimalSortingDir/sort.c b/src/optimalSortingDir/sort.c new file mode 100644 index 0000000..0de45ae --- /dev/null +++ b/src/optimalSortingDir/sort.c @@ -0,0 +1,29 @@ +#include "sort.h" +#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 count = 0; + // смотрим какие элементы изменили свою позицию + for (int k = 0; k < length; k++) { + if (array[k] != unsortedArray[k]) { + ++count; + } + } + free(unsortedArray); + return count; +} diff --git a/src/optimalSortingDir/sort.h b/src/optimalSortingDir/sort.h new file mode 100644 index 0000000..68ac648 --- /dev/null +++ b/src/optimalSortingDir/sort.h @@ -0,0 +1,7 @@ +#pragma once + +// функция сортировки +// функция принимает указатель на первый эллемент массива и длину массива +// возвращает кол-во элементов, изменивших свою позицию +// сортировка реализуется пузырьковым методом +int sort(int* array, int length); diff --git a/src/optimalSortingDir/sort.s b/src/optimalSortingDir/sort.s new file mode 100644 index 0000000..4e35cde --- /dev/null +++ b/src/optimalSortingDir/sort.s @@ -0,0 +1,107 @@ + .file "sort.c" + .text + .p2align 4 + .globl sort + .type sort, @function +sort: +.LFB11: + .cfi_startproc + pushq %r12 + .cfi_def_cfa_offset 16 + .cfi_offset 12, -16 + movslq %esi, %r12 + movl $4, %esi + pushq %rbp + .cfi_def_cfa_offset 24 + .cfi_offset 6, -24 + movq %rdi, %rbp + movq %r12, %rdi + pushq %rbx + .cfi_def_cfa_offset 32 + .cfi_offset 3, -32 + movq %r12, %rbx + salq $2, %r12 + call calloc@PLT + movq %r12, %rdx + movq %rbp, %rsi + movq %rax, %rdi + call memcpy@PLT + movq %rax, %r8 + testl %ebx, %ebx + jle .L9 + leal -1(%rbx), %edi + leaq -4(%rbp,%r12), %rsi + .p2align 4 + .p2align 3 +.L3: + movq %rbp, %rax + testl %edi, %edi + 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, %edi + cmpq %rbp, %rsi + je .L10 + subq $4, %rsi + jmp .L3 + .p2align 4,,10 + .p2align 3 +.L10: + xorl %eax, %eax + xorl %ebx, %ebx + .p2align 5 + .p2align 4 + .p2align 3 +.L6: + movl (%r8,%rax), %edi + cmpl %edi, 0(%rbp,%rax) + je .L8 + addl $1, %ebx +.L8: + addq $4, %rax + cmpq %rax, %r12 + jne .L6 + movq %r8, %rdi + call free@PLT + movl %ebx, %eax + popq %rbx + .cfi_remember_state + .cfi_def_cfa_offset 24 + popq %rbp + .cfi_def_cfa_offset 16 + popq %r12 + .cfi_def_cfa_offset 8 + ret +.L9: + .cfi_restore_state + 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 + .ident "GCC: (GNU) 15.2.1 20250813" + .section .note.GNU-stack,"",@progbits