Skip to content
This repository was archived by the owner on Jun 2, 2024. It is now read-only.

Commit faa49d5

Browse files
authored
Merge pull request #395 from shravani05/selection-sort
Added Selection Sort implementation in Python
2 parents ac5931a + f038f52 commit faa49d5

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class SelectionSort:
2+
""" SelectionSort Algorithm Implementation in Python 3
3+
arr : Unorded list
4+
output : Returns list in ascending order.
5+
time complexity : O(n^2)
6+
Example :
7+
>>> sort = SelectionSort()
8+
Selection Sort Algorithm is Initialized
9+
>>> sort([19, 11, -5, 0, 5, 7])
10+
[-5, 0, 5, 7, 11, 19]
11+
"""
12+
13+
def __init__(self):
14+
print("Selection Sort Algorithm is Initialized")
15+
16+
def __call__(self, arr):
17+
for i in range(len(arr)):
18+
min_idx = i
19+
for j in range(i + 1, len(arr)):
20+
if arr[j] < arr[min_idx]:
21+
min_idx = j
22+
arr[i], arr[min_idx] = arr[min_idx], arr[i]
23+
return arr

0 commit comments

Comments
 (0)