-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_sorting.py
62 lines (49 loc) · 2.02 KB
/
script_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
import random
import time
def very_inefficient_sort(data):
def is_sorted(lst):
"""Check if a list is sorted."""
for i in range(len(lst) - 1):
if lst[i] > lst[i + 1]:
return False
return True
def shuffle(lst, intensity=1.0):
"""Randomly shuffle a list with decreasing intensity."""
for _ in range(int(len(lst) * intensity)):
i, j = random.randint(0, len(lst) - 1), random.randint(0, len(lst) - 1)
lst[i], lst[j] = lst[j], lst[i]
return lst
def reverse_sort(lst):
"""Sort the list in reverse order using bubble sort for inefficiency."""
n = len(lst)
for i in range(n):
for j in range(n - i - 1):
if lst[j] < lst[j + 1]:
lst[j], lst[j + 1] = lst[j + 1], lst[j]
return lst
max_iterations = 40 # Maximum number of iterations to avoid infinite loops
iteration = 0
intensity = 1.0 # Start with full shuffle intensity
while not is_sorted(data) and iteration < max_iterations:
# Randomly shuffle the list with decreasing intensity
# print(f"Iteration {iteration + 1}: Shuffling with intensity {intensity:.2f}")
data = shuffle(data, intensity)
# Waste time by reverse-sorting the list (which we will shuffle anyway)
# print(f"Iteration {iteration + 1}: Reverse sorting: {data}")
data = reverse_sort(data)
# Add an artificial delay for fun
time.sleep(0.5)
# Reduce shuffle intensity over time to increase sorting likelihood
intensity = max(0.1, intensity * 0.9)
iteration += 1
if is_sorted(data):
pass
# print("Successfully sorted!")
else:
# print("Max iterations reached. Final attempt to sort...")
data = sorted(data) # Fallback to ensure the result is sorted
return data
# Example list to sort
unsorted_list = [5, 3, 8, 6, 2, 7, 4, 1]
sorted_list = very_inefficient_sort(unsorted_list)
print(f"Sorted list: {sorted_list}")