Skip to content

Commit 4422644

Browse files
committed
Refactor flash_sort.py to resolve Ruff check errors
1 parent 5e8de60 commit 4422644

File tree

1 file changed

+49
-25
lines changed

1 file changed

+49
-25
lines changed

sorts/flash_sort.py

+49-25
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
"""
22
Implementation of Flash Sort in Python
3-
Author: Aman Gupta
3+
Author: Yash Kiradoo
44
55
For doctests, run the following command:
66
python3 -m doctest -v flash_sort.py
77
88
For manual testing, run:
99
python3 flash_sort.py
1010
"""
11+
1112
from __future__ import annotations
1213
from collections.abc import Callable
1314

14-
class FlashSort:
15-
def __init__(self, arr: list[int], n_classes: int,
16-
sort_key: Callable[[int], int] = lambda x: x):
1715

16+
class FlashSort:
17+
def __init__(
18+
self,
19+
arr: list[int],
20+
n_classes: int,
21+
sort_key: Callable[[int], int] = lambda item: item,
22+
) -> None: # Added return type hint
1823
self.arr = arr
1924
self.n = len(arr)
2025
self.n_classes = n_classes
@@ -44,39 +49,58 @@ def flash_sort(self) -> None:
4449
if self.n <= 1:
4550
return
4651

47-
min_val = min(self.arr, key=self.sort_key)
48-
max_val = max(self.arr, key=self.sort_key)
52+
min_value = min(self.arr, key=self.sort_key)
53+
max_value = max(self.arr, key=self.sort_key)
4954

50-
if self.sort_key(min_val) == self.sort_key(max_val):
55+
if self.sort_key(min_value) == self.sort_key(max_value):
5156
return
5257

53-
count = [0] * self.n_classes # Renamed variable `l` to `count`
54-
c1 = (self.n_classes - 1) / (self.sort_key(max_val) - self.sort_key(min_val))
58+
count_classes = [0] * self.n_classes # Renamed variable `count` for clarity
59+
classification_factor = (self.n_classes - 1) / (
60+
self.sort_key(max_value) - self.sort_key(min_value)
61+
)
5562

5663
# Classification step
5764
for i in range(self.n):
58-
k = int(c1 * (self.sort_key(self.arr[i]) - self.sort_key(min_val)))
59-
count[k] += 1
65+
class_index = int(
66+
classification_factor
67+
* (self.sort_key(self.arr[i]) - self.sort_key(min_value))
68+
)
69+
count_classes[class_index] += 1
6070

6171
# Cumulative step
6272
for i in range(1, self.n_classes):
63-
count[i] += count[i - 1]
73+
count_classes[i] += count_classes[i - 1]
6474

6575
# Permutation step
66-
i, nmove, flash = 0, 0, self.arr[0]
67-
while nmove < self.n:
68-
while i >= count[int(c1 * (self.sort_key(flash) - self.sort_key(min_val)))]:
76+
i, num_moves, flash_item = 0, 0, self.arr[0]
77+
while num_moves < self.n:
78+
while (
79+
i
80+
>= count_classes[
81+
int(
82+
classification_factor
83+
* (self.sort_key(flash_item) - self.sort_key(min_value))
84+
)
85+
]
86+
):
6987
i += 1
70-
flash = self.arr[i]
71-
k = int(c1 * (self.sort_key(flash) - self.sort_key(min_val)))
72-
73-
while i != count[k]:
74-
k = int(c1 * (self.sort_key(flash) - self.sort_key(min_val)))
75-
hold = self.arr[count[k] - 1]
76-
self.arr[count[k] - 1] = flash
77-
flash = hold
78-
count[k] -= 1
79-
nmove += 1
88+
flash_item = self.arr[i]
89+
class_index = int(
90+
classification_factor
91+
* (self.sort_key(flash_item) - self.sort_key(min_value))
92+
)
93+
94+
while i != count_classes[class_index]:
95+
class_index = int(
96+
classification_factor
97+
* (self.sort_key(flash_item) - self.sort_key(min_value))
98+
)
99+
temp_item = self.arr[count_classes[class_index] - 1]
100+
self.arr[count_classes[class_index] - 1] = flash_item
101+
flash_item = temp_item
102+
count_classes[class_index] -= 1
103+
num_moves += 1
80104

81105
# Final step: Insertion sort to finish
82106
for i in range(1, self.n):

0 commit comments

Comments
 (0)