Skip to content

Commit ea649ea

Browse files
besobeso
authored andcommitted
Implement Tim Sort Algorithm
Implements koii-network#12858 Implements koii-network#12829 Implements koii-network#12825 Implements koii-network#12786 Implements koii-network#12770 Implements koii-network#12725 Implements koii-network#12648 # Implement Tim Sort Algorithm ## Task Write a function to implement the tim sort algorithm. ## Acceptance Criteria All tests must pass. ## Summary of Changes Added a comprehensive implementation of Tim Sort algorithm, which is a hybrid sorting algorithm derived from merge sort and insertion sort. The implementation includes optimizations for small arrays and provides efficient sorting with O(n log n) time complexity. ## Test Cases - Verify Tim Sort correctly sorts an array of integers in ascending order - Verify Tim Sort handles an empty array without errors - Verify Tim Sort correctly sorts an array with duplicate elements - Check Tim Sort performance with large randomly generated arrays - Ensure Tim Sort maintains stability of element ordering - Validate Tim Sort works with different data types (integers, floats, strings) This PR was created automatically by a Koii Network AI Agent powered by Together.ai. This PR was created automatically by a Koii Network AI Agent powered by Together.ai. This PR was created automatically by a Koii Network AI Agent powered by Together.ai. This PR was created automatically by a Koii Network AI Agent powered by Together.ai. This PR was created automatically by a Koii Network AI Agent powered by Together.ai. This PR was created automatically by a Koii Network AI Agent powered by Together.ai.
1 parent c693c97 commit ea649ea

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

prometheus/sorting_algorithms.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
def tim_sort(array, n=None, bucket_size=32):
2+
"""
3+
Implement Tim Sort algorithm.
4+
5+
Tim Sort is a hybrid sorting algorithm derived from merge sort and insertion sort.
6+
It includes optimizations for small arrays and provides efficient sorting with O(n log n)
7+
time complexity.
8+
9+
:param array: The array to be sorted.
10+
:param n: Optional. The length of the array. If not provided, it will be determined
11+
automatically.
12+
:param bucket_size: Optional. The bucket size for initial sorting. Default is 32.
13+
:return: None
14+
"""
15+
if n is None:
16+
n = len(array)
17+
18+
if n <= 1:
19+
return
20+
21+
# Split the array into smaller chunks and sort them using insertion sort
22+
for i in range(0, n, bucket_size):
23+
end = min(i + bucket_size, n)
24+
insertion_sort(array, i, end)
25+
26+
# Merge the sorted chunks using the merge sort algorithm
27+
for size in range(bucket_size, n, bucket_size):
28+
for start in range(0, n, 2 * size):
29+
mid = start + size
30+
end = min(start + 2 * size, n)
31+
merge(array, start, mid, end)
32+
33+
34+
def insertion_sort(array, start, end):
35+
"""
36+
Perform insertion sort on the given subarray.
37+
38+
:param array: The array to be sorted.
39+
:param start: The start index of the subarray.
40+
:param end: The end index of the subarray.
41+
:return: None
42+
"""
43+
for i in range(start + 1, end):
44+
key = array[i]
45+
j = i - 1
46+
while j >= start and array[j] > key:
47+
array[j + 1] = array[j]
48+
j -= 1
49+
array[j + 1] = key
50+
51+
52+
def merge(array, start, mid, end):
53+
"""
54+
Merge two sorted subarrays into a single sorted array.
55+
56+
:param array: The array to be merged.
57+
:param start: The start index of the first subarray.
58+
:param mid: The end index of the first subarray and the start index of the second subarray.
59+
:param end: The end index of the second subarray.
60+
:return: None
61+
"""
62+
left_array = array[start:mid]
63+
right_array = array[mid:end]
64+
65+
left_index = 0
66+
right_index = 0
67+
current_index = start
68+
69+
while left_index < len(left_array) and right_index < len(right_array):
70+
if left_array[left_index] <= right_array[right_index]:
71+
array[current_index] = left_array[left_index]
72+
left_index += 1
73+
else:
74+
array[current_index] = right_array[right_index]
75+
right_index += 1
76+
current_index += 1
77+
78+
while left_index < len(left_array):
79+
array[current_index] = left_array[left_index]
80+
left_index += 1
81+
current_index += 1
82+
83+
while right_index < len(right_array):
84+
array[current_index] = right_array[right_index]
85+
right_index += 1
86+
current_index += 1

0 commit comments

Comments
 (0)