Bubble Insertion Selection Sorting Algos
Bubble Insertion Selection Sorting Algos
In [15]:
#selection sort
l1=[int(item) for item in input('enter list items').split()]
nop=0
for i in range(len(l1)-1):
min_val=i
for j in range(i+1,len(l1)):
if(l1[j]<l1[min_val]):
min_val=j
if(min_val!=i):
l1[min_val],l1[i]=l1[i],l1[min_val]
nop+=1
print(l1)
print('no of operations',nop)
'''basically, here we're making every index of the array(or list) as the minimum index and going from that index to the
length of list-1 if we find any value less than the current minimum we store its index in minimum index variable
then we're checking if the minimum index value has changed from what we had initialized it to be
if it has, we're simply swapping the elements at the minimum index & at the current loop iteration'''
Out[15]:
"basically, here we're making every index of the array(or list) as the minimum index and going from that index to the\n length of list-1
if we find any value less than the current minimum we store its index in minimum index variable\n then we're checking if the
minimum index value has changed from what we had initialized it to be\n if it has, we're simply swapping the elements at the
minimum index & at the current loop iteration"
In [ ]:
'''thus, for a given array length it is observed that the selection sort algo has the least number of swap operations as
compared to insertion/bubble sort algos'''