Download quickSort.py from shukdevdatta123/Sorting-Visualization: direct link, hf CLI and curl.
- Browser
- Download file 1.26 kB
-
https://huggingface.co/spaces/shukdevdatta123/Sorting-Visualization/resolve/1a247f5828b248381d49d62f2957cdc19bb8e453/quickSort.py
- Command line
-
hf download hf://spaces/shukdevdatta123/Sorting-Visualization@1a247f5828b248381d49d62f2957cdc19bb8e453/quickSort.py
-
curl -L -o quickSort.py https://huggingface.co/spaces/shukdevdatta123/Sorting-Visualization/resolve/1a247f5828b248381d49d62f2957cdc19bb8e453/quickSort.py
1.26 kB
| import time | |
| from colors import * | |
| def partition(data, start, end, draw_data, time_tick): | |
| i = start + 1 | |
| pivot = data[start] | |
| for j in range(start+1, end+1): | |
| if data[j] < pivot: | |
| data[i], data[j] = data[j], data[i] | |
| i += 1 | |
| data[start], data[i-1] = data[i-1], data[start] | |
| return i - 1 | |
| import random | |
| def rand_partition(data, start, end, draw_data, time_tick): | |
| rand = start + random.randrange(end - start + 1) | |
| data[rand], data[start] = data[start], data[rand] | |
| return partition(data, start, end, draw_data, time_tick) | |
| def quick_sort(data, start, end, draw_data, time_tick): | |
| if start < end: | |
| #pivot_position = partition(data, start, end, draw_data, time_tick) | |
| pivot_position = rand_partition(data, start, end, draw_data, time_tick) | |
| quick_sort(data, start, pivot_position-1, draw_data, time_tick) | |
| quick_sort(data, pivot_position+1, end, draw_data, time_tick) | |
| draw_data(data, [PURPLE if x >= start and x < pivot_position else YELLOW if x == | |
| pivot_position else DARK_BLUE if x > pivot_position and x <= end else BLUE for x in range(len(data))]) | |
| time.sleep(time_tick) | |
| draw_data(data, [BLUE for x in range(len(data))]) |