-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sort.py
More file actions
56 lines (39 loc) · 1.96 KB
/
quick_sort.py
File metadata and controls
56 lines (39 loc) · 1.96 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
import cv2
import numpy as np
import random
#we were having issues with the images blowing up way too big for no reason so made this to fix it
def scale_to_fit(image, max_width=1920, max_height=1080):
#used for scaling so that the image isn't blown up too wide
h, w = image.shape[:2]
scale = min(max_width / w, max_height / h)
new_size = (int(w * scale), int(h * scale))
#Resizes an image to a specified width and height. The interpolation method cv2.INTER_AREA is used for downscaling images as it uses pixel area relation, providing high-quality results. - https://learnopencv.com/image-resizing-with-opencv/
return cv2.resize(image, new_size, interpolation=cv2.INTER_AREA)
def partition(arr, low, high, img_dict, img_win, delay):
pivot = arr[high]
i = low - 1
for j in range(low, high):
if arr[j] < pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
#update the displayed image
updated_image = np.hstack([img_dict[index] for index in arr])
scaled_image = scale_to_fit(updated_image) #scale to fit screen
cv2.imshow(img_win, scaled_image)
if cv2.waitKey(delay) == 27: #escape key to break early
return i
arr[i + 1], arr[high] = arr[high], arr[i + 1]
# Update the displayed image
updated_image = np.hstack([img_dict[index] for index in arr])
scaled_image = scale_to_fit(updated_image) #scale to fit screen
cv2.imshow(img_win, scaled_image)
if cv2.waitKey(delay) == 27: #escape key to break early
return i + 1
return i + 1
def quick_sort(arr, low, high, img_dict, img_win, delay):
if low < high:
#pass all required arguments to partition
pi = partition(arr, low, high, img_dict, img_win, delay)
#recursive calls (on left and right sub arrays to pivot)
quick_sort(arr, low, pi - 1, img_dict, img_win, delay)
quick_sort(arr, pi + 1, high, img_dict, img_win, delay)