-
Notifications
You must be signed in to change notification settings - Fork 0
Quick sort #2
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
stuffacc
wants to merge
5
commits into
master
Choose a base branch
from
sort
base: master
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
Quick sort #2
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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,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 | ||
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,33 @@ | ||
| #include "optimal_sort.h" | ||
|
|
||
| void printArray(int size, int arr[]); | ||
|
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. я бы сделал заголовочник и его подключал |
||
|
|
||
| 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); | ||
| int changes = sortArray(0, n, arr, 0); | ||
| printArray(n, arr); | ||
|
|
||
| printf("%d %d\n", n, changes); | ||
| } | ||
|
|
||
| void printArray(int size, int arr[]) | ||
| { | ||
| for (int i = 0; i < size; i++) { | ||
| printf("%d ", arr[i]); | ||
| } | ||
| printf("\n"); | ||
| } | ||
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,40 @@ | ||
| #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; | ||
| } |
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 | ||
| #include <stdio.h> | ||
|
|
||
| int sortArray(int start, int end, int arr[], int changes); |
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,167 @@ | ||
| .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 $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 | ||
| jg .L2 | ||
| movl $0, %eax | ||
| jmp .L3 | ||
| .L2: | ||
| 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) | ||
| addl $2, -36(%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) | ||
| addl $2, -36(%rbp) | ||
| movl -36(%rbp), %ecx | ||
| movq -32(%rbp), %rdx | ||
| movl -16(%rbp), %esi | ||
| movl -20(%rbp), %eax | ||
| movl %eax, %edi | ||
| call sortArray | ||
| addl %eax, -36(%rbp) | ||
| movl -16(%rbp), %eax | ||
| leal 1(%rax), %edi | ||
| movl -36(%rbp), %ecx | ||
| movq -32(%rbp), %rdx | ||
| movl -24(%rbp), %eax | ||
| movl %eax, %esi | ||
| call sortArray | ||
| addl %eax, -36(%rbp) | ||
| movl -36(%rbp), %eax | ||
| .L3: | ||
| 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: |
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.
можно всю сборку в одну строчку, но ок --- исправлять не надо