0% found this document useful (0 votes)
24 views

Bubble Insertion Selection Sorting Algos

The code implements selection sort to sort a list of numbers in ascending order. It iterates through the list, finds the minimum element index, and swaps it with the element at the current index if they are out of order, incrementing the swap counter. It prints the sorted list and number of swaps performed. Selection sort has the fewest swaps of the three algorithms for a given array length.

Uploaded by

utsav kakkad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Bubble Insertion Selection Sorting Algos

The code implements selection sort to sort a list of numbers in ascending order. It iterates through the list, finds the minimum element index, and swaps it with the element at the current index if they are out of order, incrementing the swap counter. It prints the sorted list and number of swaps performed. Selection sort has the fewest swaps of the three algorithms for a given array length.

Uploaded by

utsav kakkad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

In [12]:

l1=[int(item) for item in input('enter list items').split()]


n=len(l1)
nop=0
for i in range(n):
for j in range(n-1):
if(l1[j]>l1[j+1]):
l1[j],l1[j+1]=l1[j+1],l1[j]
print('list after {0}th operation is {1}'.format(j,l1))
nop+=1
print('no. of operations till {0}th iteration is {1}'.format(i,nop))
print(l1)
#bubble sort

enter list items61 98 72 85 95 119 105 23 481


list after 1th operation is [61, 72, 98, 85, 95, 119, 105, 23, 481]
list after 2th operation is [61, 72, 85, 98, 95, 119, 105, 23, 481]
list after 3th operation is [61, 72, 85, 95, 98, 119, 105, 23, 481]
list after 5th operation is [61, 72, 85, 95, 98, 105, 119, 23, 481]
list after 6th operation is [61, 72, 85, 95, 98, 105, 23, 119, 481]
no. of operations till 0th iteration is 5
list after 5th operation is [61, 72, 85, 95, 98, 23, 105, 119, 481]
no. of operations till 1th iteration is 6
list after 4th operation is [61, 72, 85, 95, 23, 98, 105, 119, 481]
no. of operations till 2th iteration is 7
list after 3th operation is [61, 72, 85, 23, 95, 98, 105, 119, 481]
no. of operations till 3th iteration is 8
list after 2th operation is [61, 72, 23, 85, 95, 98, 105, 119, 481]
no. of operations till 4th iteration is 9
list after 1th operation is [61, 23, 72, 85, 95, 98, 105, 119, 481]
no. of operations till 5th iteration is 10
list after 0th operation is [23, 61, 72, 85, 95, 98, 105, 119, 481]
no. of operations till 6th iteration is 11
no. of operations till 7th iteration is 11
no. of operations till 8th iteration is 11
[23, 61, 72, 85, 95, 98, 105, 119, 481]
In [13]:

#sort a list using insertion sort


l1=[int(item) for item in input('enter list items').split()]
nop=0
for i in range(1,len(l1)):
k=l1[i]
while(l1[i-1]>k and i>0):
l1[i],l1[i-1]=l1[i-1],l1[i]
nop+=1
i=i-1
print(l1)
print('no of operations',nop)

enter list items61 98 72 85 95 119 105 23 481


[23, 61, 72, 85, 95, 98, 105, 119, 481]
no of operations 11

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'''

enter list items61 98 72 85 95 119 105 23 481


[23, 61, 72, 85, 95, 98, 105, 119, 481]
no of operations 3

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'''

You might also like