Skip to content

Commit 89b6bb7

Browse files
authored
Merge pull request #20 from kingkaushalagarwal/master
Updated_input_method_Selection_Sort: Gives choice of defining the length of array.
2 parents 6bd21a6 + 52160af commit 89b6bb7

File tree

1 file changed

+103
-2
lines changed

1 file changed

+103
-2
lines changed

Selection_Sort.py

+103-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,107 @@
99
Author: Shriya Madan
1010
'''
1111

12+
#Improvement in input methods
13+
'''
14+
To take different type of input " list_input() " main method is called in Driver Code.
15+
It provides to possible option to user as either user enter the length of array or not.
16+
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:-
17+
example length of array is 10
18+
input format 1-
19+
2 3 2 5 5 65 7 4 10 966
20+
21+
input format 2-
22+
52 363
23+
12 3365
24+
1252 6662 545322
25+
12 3
26+
56
27+
28+
input format 3-
29+
23
30+
45
31+
12
32+
52
33+
32
34+
85
35+
10
36+
12
37+
32
38+
52
39+
40+
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'
41+
input format 1-
42+
2 3 2 5 5 65 7 4 10 966 $
43+
44+
input format 2-
45+
52 363
46+
12 3365
47+
1252 6662 545322
48+
12 3
49+
56 $
50+
51+
input format 3-
52+
23
53+
45
54+
12
55+
52
56+
32
57+
85
58+
10
59+
12
60+
32
61+
52
62+
$
63+
Improved by Kaushal Agarwal
64+
'''
65+
66+
67+
def with_length():
68+
flag = 1
69+
while(flag):
70+
print("Length of array")
71+
try:
72+
n = int(input())
73+
flag=0
74+
except:
75+
flag=1
76+
77+
print("It only store first ",n," elements.")
78+
print("\nEnter your elements.")
79+
arr =[]
80+
while len(arr)<n:
81+
val = list(map(float,input().split()))
82+
length = n - len(arr)
83+
if len(val)>length:
84+
arr = arr+val[:length]
85+
else:
86+
arr = arr+val
87+
return arr
88+
def without_length():
89+
print("Array only takes integer or float inputs. Enter '$' to stop")
90+
arr = list(map(float,input().split()))
91+
while(1):
92+
val = input().split()
93+
for x in val:
94+
try:
95+
x = float(x)
96+
except:
97+
return arr
98+
arr.append(float(x))
99+
100+
101+
def list_input():
102+
ch =""
103+
while ch not in ['y','n','Y','N']:
104+
print("Want to specify the length of arry y/n ",end=' ')
105+
ch = input()
106+
if ch=='Y' or ch=='y':
107+
arr = with_length()
108+
return arr
109+
else:
110+
arr= without_length()
111+
return arr
112+
12113

13114
def selection_sort(alist):
14115
"""
@@ -27,11 +128,11 @@ def selection_sort(alist):
27128

28129
if __name__ == '__main__':
29130

30-
arr = [1, 12, 11, -2, 13, 0, 6, 7]
131+
arr = list_input() # improved way of taking input us list_input()
31132
print ('Given array is', end='\n')
32133
print(*arr)
33134

34135
selection_sort(arr)
35136

36137
print('Sorted array is:', end='\n')
37-
print(*arr)
138+
print(*arr)

0 commit comments

Comments
 (0)