-
Notifications
You must be signed in to change notification settings - Fork 0
ДЗ №4 Оптимальная сортировка Максим Ипатов #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
imaxde
wants to merge
9
commits into
main
Choose a base branch
from
06oct25
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
831fe69
Merge sort header
imaxde f268341
Merge sort in C
imaxde 4da9add
Create main file for sorting
imaxde fecebc4
Return code count changes
imaxde 313413a
Merge sort in Assembler
imaxde 711d444
Create build instruction
imaxde 7f123d4
Build instruction
imaxde 953eaac
Update merge sort module
imaxde 0eb23ae
Fix styleguide violations
imaxde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| cd src/06oct25 | ||
| gcc -Wall -Wextra -O2 merge_sort.c -S | ||
| gcc -Wall -Wextra -O2 merge_sort.s main.c -o main |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #include <stdio.h> | ||
| #include "merge_sort.h" | ||
|
|
||
| int main(void) | ||
| { | ||
| int number = 0; | ||
| int data[100] = { 0 }; | ||
| int originalData[100] = { 0 }; | ||
| int index = 0; | ||
|
|
||
| printf("введите числа (для завершения нажмите enter+ctrl+d):\n"); | ||
|
|
||
| while (scanf("%d", &number) == 1) { | ||
| data[index] = number; | ||
| // сохраняем копию исходного массива | ||
| originalData[index] = number; | ||
| index++; | ||
| }; | ||
|
|
||
| mergeSort(data, index); | ||
| printf("отсортированные числа:\n"); | ||
|
|
||
| for (int i = 0; i < index; i++) | ||
| printf("%d ", data[i]); | ||
| printf("\n"); | ||
|
|
||
| // подсчитываем различия | ||
| int movedCount = 0; | ||
| for (int i = 0; i < index; i++) { | ||
| if (data[i] != originalData[i]) { | ||
| movedCount++; | ||
| } | ||
| } | ||
|
|
||
| return movedCount; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #include "merge_sort.h" | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| static void merge(int data[], int leftSize, int rightSize) | ||
| { | ||
| int size = leftSize + rightSize; | ||
| int* buffer = malloc((size_t)size * sizeof(int)); | ||
| int i = 0; | ||
| int j = 0; | ||
| int k = 0; | ||
| int* left = data; | ||
| int* right = data + leftSize; | ||
|
|
||
| while (i < leftSize && j < rightSize) { | ||
| if (left[i] <= right[j]) { | ||
| buffer[k++] = left[i++]; | ||
| } else { | ||
| buffer[k++] = right[j++]; | ||
| } | ||
| } | ||
|
|
||
| while (i < leftSize) | ||
| buffer[k++] = left[i++]; | ||
|
|
||
| while (j < rightSize) | ||
| buffer[k++] = right[j++]; | ||
|
|
||
| memcpy(data, buffer, (size_t)size * sizeof(int)); | ||
| free(buffer); | ||
| } | ||
|
|
||
| void mergeSort(int data[], int size) | ||
| { | ||
| if (size < 2) | ||
| return; | ||
|
|
||
| int mid = size / 2; | ||
| mergeSort(data, mid); | ||
| mergeSort(data + mid, size - mid); | ||
| merge(data, mid, size - mid); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #pragma once | ||
|
|
||
| /* Сортировка слиянием */ | ||
| void mergeSort(int data[], int size); |
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Я вот так сходу не уверен, что Вы собрали этот код с оптимизациями. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| .file "merge_sort.c" | ||
| .text | ||
| .p2align 4 | ||
| .globl mergeSort | ||
| .type mergeSort, @function | ||
| mergeSort: | ||
| .LFB31: | ||
| .cfi_startproc | ||
| endbr64 | ||
| pushq %r15 | ||
| .cfi_def_cfa_offset 16 | ||
| .cfi_offset 15, -16 | ||
| pushq %r14 | ||
| .cfi_def_cfa_offset 24 | ||
| .cfi_offset 14, -24 | ||
| pushq %r13 | ||
| .cfi_def_cfa_offset 32 | ||
| .cfi_offset 13, -32 | ||
| pushq %r12 | ||
| .cfi_def_cfa_offset 40 | ||
| .cfi_offset 12, -40 | ||
| movslq %esi, %r12 | ||
| pushq %rbp | ||
| .cfi_def_cfa_offset 48 | ||
| .cfi_offset 6, -48 | ||
| pushq %rbx | ||
| .cfi_def_cfa_offset 56 | ||
| .cfi_offset 3, -56 | ||
| subq $40, %rsp | ||
| .cfi_def_cfa_offset 96 | ||
| cmpl $1, %r12d | ||
| jle .L1 | ||
| movl %r12d, %r15d | ||
| movq %rdi, %r14 | ||
| sarl %r15d | ||
| movl %r15d, %esi | ||
| call mergeSort | ||
| movslq %r15d, %rax | ||
| movl %r12d, %r9d | ||
| subl %r15d, %r9d | ||
| leaq (%r14,%rax,4), %r8 | ||
| movq %r8, %rdi | ||
| movl %r9d, %esi | ||
| movl %r9d, 24(%rsp) | ||
| movq %r8, 16(%rsp) | ||
| call mergeSort | ||
| leaq 0(,%r12,4), %rax | ||
| movq %rax, %rdi | ||
| movq %rax, 8(%rsp) | ||
| call malloc@PLT | ||
| movl 24(%rsp), %r9d | ||
| movq 16(%rsp), %r8 | ||
| movq %rax, %r12 | ||
| testl %r9d, %r9d | ||
| je .L11 | ||
| movl $1, %edx | ||
| xorl %ebp, %ebp | ||
| xorl %r13d, %r13d | ||
| jmp .L6 | ||
| .p2align 4,,10 | ||
| .p2align 3 | ||
| .L19: | ||
| movl %esi, %eax | ||
| addl $1, %r13d | ||
| movl %eax, -4(%r12,%rdx,4) | ||
| leaq 1(%rdx), %rax | ||
| cmpl %r13d, %r15d | ||
| jle .L13 | ||
| .L20: | ||
| cmpl %ebp, %r9d | ||
| jle .L13 | ||
| movq %rax, %rdx | ||
| .L6: | ||
| movslq %r13d, %rax | ||
| movl (%r14,%rax,4), %esi | ||
| movslq %ebp, %rax | ||
| movl (%r8,%rax,4), %eax | ||
| cmpl %eax, %esi | ||
| jle .L19 | ||
| movl %eax, -4(%r12,%rdx,4) | ||
| addl $1, %ebp | ||
| leaq 1(%rdx), %rax | ||
| cmpl %r13d, %r15d | ||
| jg .L20 | ||
| .L13: | ||
| movl %edx, %ebx | ||
| cmpl %r13d, %r15d | ||
| jle .L8 | ||
| .L3: | ||
| movslq %ebx, %rax | ||
| movq %r8, 24(%rsp) | ||
| leaq (%r12,%rax,4), %rdi | ||
| leal -1(%r15), %eax | ||
| addl %ebx, %r15d | ||
| movl %r9d, 16(%rsp) | ||
| subl %r13d, %eax | ||
| movl %r15d, %ebx | ||
| leaq 4(,%rax,4), %rdx | ||
| movslq %r13d, %rax | ||
| subl %r13d, %ebx | ||
| leaq (%r14,%rax,4), %rsi | ||
| call memcpy@PLT | ||
| movq 24(%rsp), %r8 | ||
| movl 16(%rsp), %r9d | ||
| .L8: | ||
| cmpl %ebp, %r9d | ||
| jle .L10 | ||
| subl $1, %r9d | ||
| movslq %ebx, %rdx | ||
| movslq %ebp, %rax | ||
| subl %ebp, %r9d | ||
| leaq (%r12,%rdx,4), %rdi | ||
| leaq (%r8,%rax,4), %rsi | ||
| leaq 4(,%r9,4), %rdx | ||
| call memcpy@PLT | ||
| .L10: | ||
| movq 8(%rsp), %rdx | ||
| movq %r14, %rdi | ||
| movq %r12, %rsi | ||
| call memcpy@PLT | ||
| addq $40, %rsp | ||
| .cfi_remember_state | ||
| .cfi_def_cfa_offset 56 | ||
| movq %r12, %rdi | ||
| popq %rbx | ||
| .cfi_def_cfa_offset 48 | ||
| popq %rbp | ||
| .cfi_def_cfa_offset 40 | ||
| popq %r12 | ||
| .cfi_def_cfa_offset 32 | ||
| popq %r13 | ||
| .cfi_def_cfa_offset 24 | ||
| popq %r14 | ||
| .cfi_def_cfa_offset 16 | ||
| popq %r15 | ||
| .cfi_def_cfa_offset 8 | ||
| jmp free@PLT | ||
| .p2align 4,,10 | ||
| .p2align 3 | ||
| .L1: | ||
| .cfi_restore_state | ||
| addq $40, %rsp | ||
| .cfi_remember_state | ||
| .cfi_def_cfa_offset 56 | ||
| popq %rbx | ||
| .cfi_def_cfa_offset 48 | ||
| popq %rbp | ||
| .cfi_def_cfa_offset 40 | ||
| popq %r12 | ||
| .cfi_def_cfa_offset 32 | ||
| popq %r13 | ||
| .cfi_def_cfa_offset 24 | ||
| popq %r14 | ||
| .cfi_def_cfa_offset 16 | ||
| popq %r15 | ||
| .cfi_def_cfa_offset 8 | ||
| ret | ||
| .p2align 4,,10 | ||
| .p2align 3 | ||
| .L11: | ||
| .cfi_restore_state | ||
| xorl %ebx, %ebx | ||
| xorl %ebp, %ebp | ||
| xorl %r13d, %r13d | ||
| jmp .L3 | ||
| .cfi_endproc | ||
| .LFE31: | ||
| .size mergeSort, .-mergeSort | ||
| .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: |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Инструкции по сборке нет :(