From 4ba2e5a9404653067bc923d93c0f7e99540b3b9c Mon Sep 17 00:00:00 2001 From: stuffacc Date: Thu, 9 Oct 2025 21:14:31 +0300 Subject: [PATCH 1/5] Sort task --- 4/main.c | 29 +++++++++ 4/optimal_sort.c | 38 +++++++++++ 4/optimal_sort.s | 161 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 228 insertions(+) create mode 100644 4/main.c create mode 100644 4/optimal_sort.c create mode 100644 4/optimal_sort.s diff --git a/4/main.c b/4/main.c new file mode 100644 index 0000000..7d52341 --- /dev/null +++ b/4/main.c @@ -0,0 +1,29 @@ +#include + +void printArray(int size, int arr[]); +void sortArray(int start, int end, int arr[]); + + +void main() { + int arr[100]; + int n = 0; + + while (n < 100 && scanf("%d", &arr[n]) == 1) { + n++; + + if (getchar() == '\n') { + break; + } + } + printArray(n, arr); + sortArray(0, n, arr); + printArray(n, arr); +} + + +void printArray(int size, int arr[]) { + for (int i = 0; i < size; i++) { + printf("%d ", arr[i]); + } + printf("\n"); +} \ No newline at end of file diff --git a/4/optimal_sort.c b/4/optimal_sort.c new file mode 100644 index 0000000..c79c174 --- /dev/null +++ b/4/optimal_sort.c @@ -0,0 +1,38 @@ +#include + +void sortArray(int start, int end, int arr[]); + +void sortArray(int start, int end, int arr[]) { + if (end - start <= 1) { + return; + } + + int pivot = arr[end - 1]; + int left = start; + int right = end - 2; + + while (left <= right) { + while (arr[left] <= pivot && left <= right) { + left++; + } + + while (arr[right] > pivot && left <= right) { + right--; + } + + if (left < right) { + int temp = arr[left]; + arr[left] = arr[right]; + arr[right] = temp; + left++; + right--; + } + } + + arr[end - 1] = arr[left]; + arr[left] = pivot; + + sortArray(start, left, arr); + sortArray(left + 1, end, arr); + +} diff --git a/4/optimal_sort.s b/4/optimal_sort.s new file mode 100644 index 0000000..25db176 --- /dev/null +++ b/4/optimal_sort.s @@ -0,0 +1,161 @@ + .file "optimal_sort.c" + .text + .globl sortArray + .type sortArray, @function +sortArray: +.LFB0: + .cfi_startproc + endbr64 + pushq %rbp + .cfi_def_cfa_offset 16 + .cfi_offset 6, -16 + movq %rsp, %rbp + .cfi_def_cfa_register 6 + subq $32, %rsp + movl %edi, -20(%rbp) + movl %esi, -24(%rbp) + movq %rdx, -32(%rbp) + movl -24(%rbp), %eax + subl -20(%rbp), %eax + cmpl $1, %eax + jle .L13 + movl -24(%rbp), %eax + cltq + salq $2, %rax + leaq -4(%rax), %rdx + movq -32(%rbp), %rax + addq %rdx, %rax + movl (%rax), %eax + movl %eax, -8(%rbp) + movl -20(%rbp), %eax + movl %eax, -16(%rbp) + movl -24(%rbp), %eax + subl $2, %eax + movl %eax, -12(%rbp) + jmp .L11 +.L7: + addl $1, -16(%rbp) +.L5: + movl -16(%rbp), %eax + cltq + leaq 0(,%rax,4), %rdx + movq -32(%rbp), %rax + addq %rdx, %rax + movl (%rax), %eax + cmpl %eax, -8(%rbp) + jl .L8 + movl -16(%rbp), %eax + cmpl -12(%rbp), %eax + jle .L7 + jmp .L8 +.L10: + subl $1, -12(%rbp) +.L8: + movl -12(%rbp), %eax + cltq + leaq 0(,%rax,4), %rdx + movq -32(%rbp), %rax + addq %rdx, %rax + movl (%rax), %eax + cmpl %eax, -8(%rbp) + jge .L9 + movl -16(%rbp), %eax + cmpl -12(%rbp), %eax + jle .L10 +.L9: + movl -16(%rbp), %eax + cmpl -12(%rbp), %eax + jge .L11 + movl -16(%rbp), %eax + cltq + leaq 0(,%rax,4), %rdx + movq -32(%rbp), %rax + addq %rdx, %rax + movl (%rax), %eax + movl %eax, -4(%rbp) + movl -12(%rbp), %eax + cltq + leaq 0(,%rax,4), %rdx + movq -32(%rbp), %rax + addq %rdx, %rax + movl -16(%rbp), %edx + movslq %edx, %rdx + leaq 0(,%rdx,4), %rcx + movq -32(%rbp), %rdx + addq %rcx, %rdx + movl (%rax), %eax + movl %eax, (%rdx) + movl -12(%rbp), %eax + cltq + leaq 0(,%rax,4), %rdx + movq -32(%rbp), %rax + addq %rax, %rdx + movl -4(%rbp), %eax + movl %eax, (%rdx) + addl $1, -16(%rbp) + subl $1, -12(%rbp) +.L11: + movl -16(%rbp), %eax + cmpl -12(%rbp), %eax + jle .L5 + movl -16(%rbp), %eax + cltq + leaq 0(,%rax,4), %rdx + movq -32(%rbp), %rax + addq %rdx, %rax + movl -24(%rbp), %edx + movslq %edx, %rdx + salq $2, %rdx + leaq -4(%rdx), %rcx + movq -32(%rbp), %rdx + addq %rcx, %rdx + movl (%rax), %eax + movl %eax, (%rdx) + movl -16(%rbp), %eax + cltq + leaq 0(,%rax,4), %rdx + movq -32(%rbp), %rax + addq %rax, %rdx + movl -8(%rbp), %eax + movl %eax, (%rdx) + movq -32(%rbp), %rdx + movl -16(%rbp), %ecx + movl -20(%rbp), %eax + movl %ecx, %esi + movl %eax, %edi + call sortArray + movl -16(%rbp), %eax + leal 1(%rax), %ecx + movq -32(%rbp), %rdx + movl -24(%rbp), %eax + movl %eax, %esi + movl %ecx, %edi + call sortArray + jmp .L1 +.L13: + nop +.L1: + leave + .cfi_def_cfa 7, 8 + ret + .cfi_endproc +.LFE0: + .size sortArray, .-sortArray + .ident "GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0" + .section .note.GNU-stack,"",@progbits + .section .note.gnu.property,"a" + .align 8 + .long 1f - 0f + .long 4f - 1f + .long 5 +0: + .string "GNU" +1: + .align 8 + .long 0xc0000002 + .long 3f - 2f +2: + .long 0x3 +3: + .align 8 +4: From 7608c4a9613a27f6408398d46d4531bc563ef246 Mon Sep 17 00:00:00 2001 From: stuffacc Date: Sun, 12 Oct 2025 14:20:24 +0300 Subject: [PATCH 2/5] Build instruction --- 4/build.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 4/build.md diff --git a/4/build.md b/4/build.md new file mode 100644 index 0000000..c37b2cc --- /dev/null +++ b/4/build.md @@ -0,0 +1,6 @@ +# Copy - Paste + +gcc -c optimal_sort.s -o optimal_sort.o +gcc -c main.c -o main.o +gcc main.o optimal_sort.o +./a.out From a1ecde01dd449641cd159ad01ec75ed5fd88b29d Mon Sep 17 00:00:00 2001 From: stuffacc Date: Sun, 12 Oct 2025 14:22:14 +0300 Subject: [PATCH 3/5] Update build.md --- 4/build.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/4/build.md b/4/build.md index c37b2cc..3b81ce1 100644 --- a/4/build.md +++ b/4/build.md @@ -1,6 +1,9 @@ # Copy - Paste gcc -c optimal_sort.s -o optimal_sort.o + gcc -c main.c -o main.o + gcc main.o optimal_sort.o + ./a.out From c1dba0f103fbc7b15ed6282894ececbd8b589151 Mon Sep 17 00:00:00 2001 From: stuffacc Date: Sun, 12 Oct 2025 14:56:54 +0300 Subject: [PATCH 4/5] new output --- 4/main.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/4/main.c b/4/main.c index 7d52341..dafa662 100644 --- a/4/main.c +++ b/4/main.c @@ -2,22 +2,27 @@ void printArray(int size, int arr[]); void sortArray(int start, int end, int arr[]); - +int changes(int size, int arr1[], int arr2[]); void main() { int arr[100]; + int before[100]; int n = 0; while (n < 100 && scanf("%d", &arr[n]) == 1) { + before[n] = arr[n]; n++; if (getchar() == '\n') { break; } } + printArray(n, arr); sortArray(0, n, arr); printArray(n, arr); + + printf("%d %d\n", n, changes(n, before, arr)); } @@ -26,4 +31,15 @@ void printArray(int size, int arr[]) { printf("%d ", arr[i]); } printf("\n"); +} + +int changes(int size, int arr1[], int arr2[]) { + int diff = 0; + for (int i = 0; i < size; i++) { + if (arr1[i] != arr2[i]) { + diff++; + } + } + + return diff; } \ No newline at end of file From df953a3fecaac5a0b0b0867f0ecd971359fc783c Mon Sep 17 00:00:00 2001 From: stuffacc Date: Wed, 24 Dec 2025 20:55:46 +0300 Subject: [PATCH 5/5] FIX --- 4/main.c | 42 ++++++++++---------------- 4/optimal_sort.c | 76 +++++++++++++++++++++++++----------------------- 4/optimal_sort.h | 4 +++ 4/optimal_sort.s | 26 ++++++++++------- 4 files changed, 74 insertions(+), 74 deletions(-) create mode 100644 4/optimal_sort.h diff --git a/4/main.c b/4/main.c index dafa662..29ef0fe 100644 --- a/4/main.c +++ b/4/main.c @@ -1,45 +1,33 @@ -#include +#include "optimal_sort.h" void printArray(int size, int arr[]); -void sortArray(int start, int end, int arr[]); -int changes(int size, int arr1[], int arr2[]); -void main() { +void main() +{ int arr[100]; int before[100]; int n = 0; while (n < 100 && scanf("%d", &arr[n]) == 1) { - before[n] = arr[n]; - n++; - - if (getchar() == '\n') { - break; - } + before[n] = arr[n]; + n++; + + if (getchar() == '\n') { + break; + } } printArray(n, arr); - sortArray(0, n, arr); + int changes = sortArray(0, n, arr, 0); printArray(n, arr); - - printf("%d %d\n", n, changes(n, before, arr)); -} + printf("%d %d\n", n, changes); +} -void printArray(int size, int arr[]) { +void printArray(int size, int arr[]) +{ for (int i = 0; i < size; i++) { - printf("%d ", arr[i]); + printf("%d ", arr[i]); } printf("\n"); } - -int changes(int size, int arr1[], int arr2[]) { - int diff = 0; - for (int i = 0; i < size; i++) { - if (arr1[i] != arr2[i]) { - diff++; - } - } - - return diff; -} \ No newline at end of file diff --git a/4/optimal_sort.c b/4/optimal_sort.c index c79c174..5efe359 100644 --- a/4/optimal_sort.c +++ b/4/optimal_sort.c @@ -1,38 +1,40 @@ -#include - -void sortArray(int start, int end, int arr[]); - -void sortArray(int start, int end, int arr[]) { - if (end - start <= 1) { - return; - } - - int pivot = arr[end - 1]; - int left = start; - int right = end - 2; - - while (left <= right) { - while (arr[left] <= pivot && left <= right) { - left++; - } - - while (arr[right] > pivot && left <= right) { - right--; - } - - if (left < right) { - int temp = arr[left]; - arr[left] = arr[right]; - arr[right] = temp; - left++; - right--; - } - } - - arr[end - 1] = arr[left]; - arr[left] = pivot; - - sortArray(start, left, arr); - sortArray(left + 1, end, arr); - +#include "optimal_sort.h" + +int sortArray(int start, int end, int arr[], int changes) +{ + if (end - start <= 1) { + return 0; + } + + int pivot = arr[end - 1]; + int left = start; + int right = end - 2; + + while (left <= right) { + while (arr[left] <= pivot && left <= right) { + left++; + } + + while (arr[right] > pivot && left <= right) { + right--; + } + + if (left < right) { + int temp = arr[left]; + arr[left] = arr[right]; + arr[right] = temp; + left++; + right--; + changes += 2; + } + } + + arr[end - 1] = arr[left]; + arr[left] = pivot; + + changes += 2; + changes += sortArray(start, left, arr, changes); + changes += sortArray(left + 1, end, arr, changes); + + return changes; } diff --git a/4/optimal_sort.h b/4/optimal_sort.h new file mode 100644 index 0000000..04a5fed --- /dev/null +++ b/4/optimal_sort.h @@ -0,0 +1,4 @@ +#pragma once +#include + +int sortArray(int start, int end, int arr[], int changes); diff --git a/4/optimal_sort.s b/4/optimal_sort.s index 25db176..c3585b8 100644 --- a/4/optimal_sort.s +++ b/4/optimal_sort.s @@ -11,14 +11,18 @@ sortArray: .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 - subq $32, %rsp + subq $48, %rsp movl %edi, -20(%rbp) movl %esi, -24(%rbp) movq %rdx, -32(%rbp) + movl %ecx, -36(%rbp) movl -24(%rbp), %eax subl -20(%rbp), %eax cmpl $1, %eax - jle .L13 + jg .L2 + movl $0, %eax + jmp .L3 +.L2: movl -24(%rbp), %eax cltq salq $2, %rax @@ -94,6 +98,7 @@ sortArray: movl %eax, (%rdx) addl $1, -16(%rbp) subl $1, -12(%rbp) + addl $2, -36(%rbp) .L11: movl -16(%rbp), %eax cmpl -12(%rbp), %eax @@ -118,23 +123,24 @@ sortArray: addq %rax, %rdx movl -8(%rbp), %eax movl %eax, (%rdx) + addl $2, -36(%rbp) + movl -36(%rbp), %ecx movq -32(%rbp), %rdx - movl -16(%rbp), %ecx + movl -16(%rbp), %esi movl -20(%rbp), %eax - movl %ecx, %esi movl %eax, %edi call sortArray + addl %eax, -36(%rbp) movl -16(%rbp), %eax - leal 1(%rax), %ecx + leal 1(%rax), %edi + movl -36(%rbp), %ecx movq -32(%rbp), %rdx movl -24(%rbp), %eax movl %eax, %esi - movl %ecx, %edi call sortArray - jmp .L1 -.L13: - nop -.L1: + addl %eax, -36(%rbp) + movl -36(%rbp), %eax +.L3: leave .cfi_def_cfa 7, 8 ret