- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting.py
More file actions
Latest commit
41 lines (32 loc) · 1.18 KB
/
Copy pathSorting.py
File metadata and controls
41 lines (32 loc) · 1.18 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
# Sorting of numbers
# we will stick to acending - read it backwards otherwise :P
defInsertionSort(A):
# As A[0] is sorted by itself we need to take the number to be sorted from 1 to n-1
foriinrange(1,len(A)):
key=A[i] # Take the next number in unsorted section
forjinrange(0,i): # go through the sorted numbers
if(key<A[j]):
A.insert(j,key) # COPY "key" into the right spot
A.pop(i+1) # remove "key" from its initial position
break
response=1
while(response==1):
print"Enter how many numbers are to be sorted"
n=int(raw_input())
A= []
print"Enter the "+str(n)+" numbers :-"
foriinrange(n):
A.append(float(raw_input()))
print"Enter 1 for Insertion Sort"
choice=int(raw_input())
if(choice==1):
print"Executing insertion sort on the following numbers"
printA
InsertionSort(A)
print"The sorted numbers are"
printA
else:
print"invalid option"
print"enter 1 for trying with a different set of numbers"
response=int(raw_input())
print"thanks for using my program"