-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSortAlgorithm.py
More file actions
71 lines (50 loc) · 1.86 KB
/
Copy pathquickSortAlgorithm.py
File metadata and controls
71 lines (50 loc) · 1.86 KB
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
import time
import sortingUtils
from sortingUtils import wait
startTime = 0
def partition(array, low, high, delay):
global startTime
if low == 0 and high == len(array) - 1:
startTime = time.perf_counter()
pivot = array[high]
i = low - 1
sortingUtils.selectedIndices = [high]
for j in range(low, high):
sortingUtils.comparisons += 1
sortingUtils.comparedIndices = [j, high]
if array[j] < pivot:
i += 1
sortingUtils.comparedIndices = [i, j]
array[i], array[j] = array[j], array[i]
sortingUtils.swapDataSound.play()
sortingUtils.swaps += 1
sortingUtils.sortTimeVisual = time.perf_counter() - startTime
yield
wait(delay)
sortingUtils.comparedIndices.clear()
sortingUtils.selectedIndices.clear()
array[i + 1], array[high] = array[high], array[i + 1]
if low == 0 and high == len(array) - 1:
sortingUtils.sortTimeVisual = time.perf_counter() - startTime
return i + 1
def quickSort(array, low, high, delay):
global startTime
if low < high:
part = yield from partition(array, low, high, delay)
yield from quickSort(array, low, part - 1, delay)
yield from quickSort(array, part + 1, high, delay)
def partitionNoVisible(array, low, high):
pivot = array[high]
i = low - 1
for j in range(low, high):
if array[j] < pivot:
i += 1
array[i], array[j] = array[j], array[i]
array[i + 1], array[high] = array[high], array[i + 1]
return i + 1
def quickSortNoVisible(array, low, high):
timeArray = array.copy()
if low < high:
part = partitionNoVisible(timeArray, low, high)
quickSortNoVisible(timeArray, low, part - 1)
quickSortNoVisible(timeArray, part + 1, high)