-
Notifications
You must be signed in to change notification settings - Fork 18
/
Selection_Sort.py
138 lines (118 loc) · 3.36 KB
/
Selection_Sort.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Selection Sort
'''
Inside the function create a loop with a loop variable i that counts from 0 to the length of the list – 1.
Create a variable smallest with initial value i.
Create an inner loop with a loop variable j that counts from i + 1 up to the length of the list – 1.
Inside the inner loop, if the elements at index j is smaller than the element at index smallest, then set smallest equal to j.
After the inner loop finishes, swap the elements at indexes i and smallest.
Author: Shriya Madan & Kaushal Agarwal
'''
#Improvement in input methods
'''
To take different type of input " list_input() " main method is called in Driver Code.
It provides to possible option to user as either user enter the length of array or not.
if user enter length of array then array takes input for user until length of reached in different interactive form. Some example are given below:-
example length of array is 10
input format 1-
2 3 2 5 5 65 7 4 10 966
input format 2-
52 363
12 3365
1252 6662 545322
12 3
56
input format 3-
23
45
12
52
32
85
10
12
32
52
if user choose option second to not specify the length of array then array store input untill 'user enter '$' or any character except integer or float'
input format 1-
2 3 2 5 5 65 7 4 10 966 $
input format 2-
52 363
12 3365
1252 6662 545322
12 3
56 $
input format 3-
23
45
12
52
32
85
10
12
32
52
$
Improved by Kaushal Agarwal
'''
def with_length():
flag = 1
while(flag):
print("Length of array")
try:
n = int(input())
flag=0
except:
flag=1
print("It only store first ",n," elements.")
print("\nEnter your elements.")
arr =[]
while len(arr)<n:
val = list(map(float,input().split()))
length = n - len(arr)
if len(val)>length:
arr = arr+val[:length]
else:
arr = arr+val
return arr
def without_length():
print("Array only takes integer or float inputs. Enter '$' to stop")
arr = list(map(float,input().split()))
while(1):
val = input().split()
for x in val:
try:
x = float(x)
except:
return arr
arr.append(float(x))
def list_input():
ch =""
while ch not in ['y','n','Y','N']:
print("Want to specify the length of arry y/n ",end=' ')
ch = input()
if ch=='Y' or ch=='y':
arr = with_length()
return arr
else:
arr= without_length()
return arr
def selection_sort(alist):
"""
Sorts a given list using a selection sort algorithm
:param alist: a list to be sorted
:return: None
"""
for i in range(0, len(alist) - 1): # loop for number of elements in alist
smallest = i # smallest holds the value of i
for j in range(i + 1, len(alist)):
if alist[j] < alist[smallest]: # comparing if value at index j is smaller
smallest = j # then insert value of j in smallest(so we will get smallest value in this)
alist[i], alist[smallest] = alist[smallest], alist[i] # swapping value of smallest and i
if __name__ == '__main__':
arr = list_input() # improved way of taking input us list_input()
print ('Given array is', end='\n')
print(*arr)
selection_sort(arr)
print('Sorted array is:', end='\n')
print(*arr)