-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
118 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,2 @@ | ||
webinar | ||
qsort.py |
Binary file not shown.
This file contains 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,12 @@ | ||
def bubble_sort(x): | ||
N = len(x) | ||
for i in range(0, N - 1): | ||
for j in range(0, N - i - 1): | ||
if x[j] > x[j + 1]: | ||
x[j], x[j + 1] = x[j + 1], x[j] | ||
|
||
|
||
if __name__ == '__main__': | ||
input() | ||
x = list(map(int, input().split())) | ||
print(' '.join(map(str, x))) |
This file contains 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,15 @@ | ||
def insertion_sort(x): | ||
N = len(x) | ||
for i in range(1, N): | ||
key = x[i] | ||
j = i - 1 | ||
while j >= 0 and key < x[j]: | ||
x[j + 1] = x[j] | ||
j -= 1 | ||
x[j + 1] = key | ||
|
||
|
||
if __name__ == '__main__': | ||
input() | ||
x = list(map(int, input().split())) | ||
print(' '.join(map(str, x))) |
This file contains 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,31 @@ | ||
def merge(x, l, m, r): | ||
tmp = [] | ||
i1 = l | ||
i2 = m | ||
while i1 < m or i2 < r: | ||
if (i2 >= r) or ((i1 < m) and | ||
(x[i1] < x[i2])): | ||
tmp.append(x[i1]) | ||
i1 += 1 | ||
else: | ||
tmp.append(x[i2]) | ||
i2 += 1 | ||
x[l:r] = tmp | ||
|
||
|
||
def merge_sort(x, l=0, r=None): | ||
if r is None: | ||
r = len(x) | ||
if r - l > 1: | ||
m = (l + r) // 2 # Finding the mid of the array | ||
merge_sort(x, l, m) # Sorting the first half | ||
merge_sort(x, m, r) # Sorting the second half | ||
merge(x, l, m, r) | ||
|
||
|
||
if __name__ == '__main__': | ||
input() | ||
x = list(map(int, input().split())) | ||
N = len(x) | ||
merge_sort(x, 0, N) | ||
print(' '.join(map(str, x))) |
This file contains 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,35 @@ | ||
import random | ||
|
||
|
||
def partition(x, l, r, pivot): | ||
""" | ||
:param x: Source array (list) | ||
:param l: left border of partitioning range (int) | ||
:param r: right border (not included) of partitioning range (int) | ||
:param pivot: pivot element to divide array (any item from x[l, r)). | ||
:return: il, ir -- desired partition | ||
This function should reorder elements of x within [l, r) region | ||
in the way, these conditions are true: | ||
x[l:il] < pivot | ||
x[il:ir] == pivot | ||
x[ir:r] > pivot | ||
""" | ||
# Your code goes here | ||
pass | ||
|
||
|
||
def qsort(x, l=0, r=None): | ||
if r is None: | ||
r = len(x) | ||
if (r - l) > 1: | ||
pivot = x[random.randint(l, r - 1)] | ||
il, ir = partition(x, l, r, pivot) | ||
qsort(x, l, il) | ||
qsort(x, ir, r) | ||
|
||
|
||
if __name__ == '__main__': | ||
N = int(input()) | ||
x = list(map(int, input().split())) | ||
qsort(x) | ||
print(' '.join(map(str, x))) |
This file contains 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,14 @@ | ||
def selection_sort(x): | ||
N = len(x) | ||
for i in range(N - 1): | ||
i_min = i | ||
for j in range(i + 1, N): | ||
if x[i_min] > x[j]: | ||
i_min = j | ||
x[i], x[i_min] = x[i_min], x[i] | ||
|
||
|
||
if __name__ == '__main__': | ||
input() | ||
x = list(map(int, input().split())) | ||
print(' '.join(map(str, x))) |