Skip to content

Commit 10ee21d

Browse files
authored
Merge branch 'master' into algo/transitiveClosure
2 parents cb8f5a2 + 9d52683 commit 10ee21d

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,6 +1302,7 @@
13021302
* [Shell Sort](sorts/shell_sort.py)
13031303
* [Shrink Shell Sort](sorts/shrink_shell_sort.py)
13041304
* [Slowsort](sorts/slowsort.py)
1305+
* [Stalin Sort](sorts/stalin_sort.py)
13051306
* [Stooge Sort](sorts/stooge_sort.py)
13061307
* [Strand Sort](sorts/strand_sort.py)
13071308
* [Tim Sort](sorts/tim_sort.py)

machine_learning/k_means_clust.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@
3737
heterogeneity,
3838
k
3939
)
40-
5. Transfers Dataframe into excel format it must have feature called
40+
5. Plot the labeled 3D data points with centroids.
41+
plot_kmeans(
42+
X,
43+
centroids,
44+
cluster_assignment
45+
)
46+
6. Transfers Dataframe into excel format it must have feature called
4147
'Clust' with k means clustering numbers in it.
4248
"""
4349

@@ -126,6 +132,19 @@ def plot_heterogeneity(heterogeneity, k):
126132
plt.show()
127133

128134

135+
def plot_kmeans(data, centroids, cluster_assignment):
136+
ax = plt.axes(projection="3d")
137+
ax.scatter(data[:, 0], data[:, 1], data[:, 2], c=cluster_assignment, cmap="viridis")
138+
ax.scatter(
139+
centroids[:, 0], centroids[:, 1], centroids[:, 2], c="red", s=100, marker="x"
140+
)
141+
ax.set_xlabel("X")
142+
ax.set_ylabel("Y")
143+
ax.set_zlabel("Z")
144+
ax.set_title("3D K-Means Clustering Visualization")
145+
plt.show()
146+
147+
129148
def kmeans(
130149
data, k, initial_centroids, maxiter=500, record_heterogeneity=None, verbose=False
131150
):
@@ -193,6 +212,7 @@ def kmeans(
193212
verbose=True,
194213
)
195214
plot_heterogeneity(heterogeneity, k)
215+
plot_kmeans(dataset["data"], centroids, cluster_assignment)
196216

197217

198218
def report_generator(

sorts/stalin_sort.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
Stalin Sort algorithm: Removes elements that are out of order.
3+
Elements that are not greater than or equal to the previous element are discarded.
4+
Reference: https://medium.com/@kaweendra/the-ultimate-sorting-algorithm-6513d6968420
5+
"""
6+
7+
8+
def stalin_sort(sequence: list[int]) -> list[int]:
9+
"""
10+
Sorts a list using the Stalin sort algorithm.
11+
12+
>>> stalin_sort([4, 3, 5, 2, 1, 7])
13+
[4, 5, 7]
14+
15+
>>> stalin_sort([1, 2, 3, 4])
16+
[1, 2, 3, 4]
17+
18+
>>> stalin_sort([4, 5, 5, 2, 3])
19+
[4, 5, 5]
20+
21+
>>> stalin_sort([6, 11, 12, 4, 1, 5])
22+
[6, 11, 12]
23+
24+
>>> stalin_sort([5, 0, 4, 3])
25+
[5]
26+
27+
>>> stalin_sort([5, 4, 3, 2, 1])
28+
[5]
29+
30+
>>> stalin_sort([1, 2, 3, 4, 5])
31+
[1, 2, 3, 4, 5]
32+
33+
>>> stalin_sort([1, 2, 8, 7, 6])
34+
[1, 2, 8]
35+
"""
36+
result = [sequence[0]]
37+
for element in sequence[1:]:
38+
if element >= result[-1]:
39+
result.append(element)
40+
41+
return result
42+
43+
44+
if __name__ == "__main__":
45+
import doctest
46+
47+
doctest.testmod()

0 commit comments

Comments
 (0)