To write a program to perform selection sort and insertion sort using python programming.
- Hardware – PCs
- Anaconda – Python 3.7 Installation / Moodle-Code Runner
- Set the first unsorted element as the minimum
- For each of the unsorted elements, check if the element < current minimum.
- If yes, set the element as the new minimum.
- Swap minimum with first unsorted position.
- Repeat the steps 2 and 3 for all the elements in the array.
- Set the first element as sorted element j.
- For each unsorted element X, check if current sorted element j >X.
- If yes, move sorted element to the right by 1.
- Break the loop and insert X.
- Repeat the steps 2 to 4 for sorting all the elements in the array.
i) #Selection Sort
'''
#DEVELOPED BY : RAGUL RAAJAN T
#REG.NO:212223100043
'''
Unsorted=eval(input())
def selsort(Unsorted):
n = len(Unsorted)
for i in range(n-1):
min_pos = i
for j in range(i,n):
if Unsorted[j]<Unsorted[min_pos]:
Unsorted[j],Unsorted[min_pos] = Unsorted[min_pos],Unsorted[j]
return Unsorted
print(selsort(Unsorted))
ii) #Insertion Sort
'''
#DEVELOPED BY : RAGUL RAAJAN T
#REG.NO:212223100043
'''
def Insertionsort(arr):
for i in range(1,len(arr)):
j = i
while arr[j]<arr[j-1] and j>0:
arr[j],arr[j-1] = arr[j-1], arr[j]
j-=1
return arr
arr = eval(input())
print(Insertionsort(arr))
i) #Selection Sort:
Thus the program is written to perform selection sort and insertion sort using python programming.