-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting.py
41 lines (33 loc) · 977 Bytes
/
sorting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import random
def quick_sort_columns(nums, fst, lst):
if fst >= lst:
return
i, j = fst, lst
pivot = nums[0][random.randint(fst, lst)]
while i <= j:
while nums[0][i] < pivot:
i += 1
while nums[0][j] > pivot:
j -= 1
if i <= j:
for num in nums:
num[i], num[j] = num[j], num[i]
i, j = i + 1, j - 1
quick_sort_columns(nums, fst, j)
quick_sort_columns(nums, i, lst)
def quick_sort_rows(nums, fst, lst):
if fst >= lst:
return
i, j = fst, lst
pivot = nums[random.randint(fst, lst)][0]
while i <= j:
while nums[i][0] < pivot:
i += 1
while nums[j][0] > pivot:
j -= 1
if i <= j:
for x in range(0, len(nums[0])):
nums[i][x], nums[j][x] = nums[j][x], nums[i][x]
i, j = i + 1, j - 1
quick_sort_rows(nums, fst, j)
quick_sort_rows(nums, i, lst)