-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSelection_Sort
More file actions
55 lines (41 loc) · 1.19 KB
/
Selection_Sort
File metadata and controls
55 lines (41 loc) · 1.19 KB
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
'''
Sorting any sequence means to arrange the elements of that sequence according to some specific criterion.
for example:
--> Assending
--> Desending
Selection Sort:
Idea:
The selection sort algorithm sorts an array by repeatedly finding the minimum element from unsorted part and swap it with beginning element.
The algorithm maintains two subarrays in a given array.
-->The sorted subarray. (Left part)
-->Remaining unsorted subarray . (Right part)
In every iteration of selection sort, the minimum element from the unsorted subarray is picked and moved to the sorted subarray.
'''
Arr = [34, 5, 19, 2, 71,45,55]
for i in range(len(Arr)):
min_idx = i
for j in range(i+1, len(Arr)):
if Arr[min_idx] > Arr[j]:
min_idx = j
Arr[i], Arr[min_idx] = Arr[min_idx], Arr[i] #swaping the min element with first element
print ("Resultant array")
for i in range(len(Arr)):
print(Arr[i])
'''
Time complexity: o(n^2)
Auxiliary Space: O(1)
In-place and not-stable
Input:
An array [34, 5, 19, 2, 71,45,55]
Output:
Resultant array
2
5
19
34
45
55
71
Note: Python use TIM sort for general purpose of sorting.
Read more about TIM sort (https://www.geeksforgeeks.org/timsort/)
'''