-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting.py
90 lines (68 loc) · 2.07 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def insertion_sort_history(arr):
history = [arr.copy()]
i = 1
while i < len(arr):
j = i
while j > 0 and arr[j - 1] > arr[j]:
arr[j], arr[j - 1] = arr[j - 1], arr[j]
history.append(arr.copy())
j -= 1
i += 1
return history
def bubble_sort_history(arr):
history = [arr.copy()]
flag = True
while flag:
flag = False
for i in range(len(arr) - 1):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
history.append(arr.copy())
flag = True
return history
def partition_lomuto(arr, history, low, high):
pivot = arr[high]
i = low
for j in range(low, high):
if arr[j] < pivot:
if j != i:
arr[i], arr[j] = arr[j], arr[i]
history.append(arr.copy())
i += 1
if i != high:
arr[i], arr[high] = arr[high], arr[i]
history.append(arr.copy())
return i
def quicksort_lomuto(arr, history, low, high):
if low < high:
p = partition_lomuto(arr, history, low, high)
quicksort_lomuto(arr, history, low, p - 1)
quicksort_lomuto(arr, history, p + 1, high)
def quicksort_lomuto_history(arr):
history = [arr.copy()]
quicksort_lomuto(arr, history, 0, len(arr) - 1)
return history
def partition_hoare(arr, history, low, high):
pivot = arr[low]
i = low - 1
j = high + 1
while True:
i += 1
while arr[i] < pivot:
i += 1
j -= 1
while arr[j] > pivot:
j -= 1
if i >= j:
return j
arr[i], arr[j] = arr[j], arr[i]
history.append(arr.copy())
def quicksort_hoare(arr, history, low, high):
if low < high:
split_index = partition_hoare(arr, history, low, high)
quicksort_hoare(arr, history, low, split_index)
quicksort_hoare(arr, history, split_index + 1, high)
def quicksort_hoare_history(arr):
history = [arr.copy()]
quicksort_hoare(arr, history, 0, len(arr) - 1)
return history