7 сентября 2022
id 1353347414k = [5,9,2,7,8,4,3]
for i in range(len(k)):
for j in range(i+1,len(k)):
if k[i]>k[j] :
k[i],k[j]= k[j],k[i]
print(k)
is this correct ,,?? for INSERTION SORT..?
id 1353347414ohh.....how to run then insertion sort..?
There's a slight difference between insertion sort and bubble sort :
Bubble sort breaks a bubble in every iteration (if there's any).
A bubble is when a number in lower index is greater than a number in higher indexes if u wanna sort the list in ascending order. (Like the if statement in ur code)
However what insertion sort does is that it splits the list into sorted and unsorted parts and for every number in unsorted part it finds its place in the sorted part and inserts it in that index. (Note that by splitting the list into sorted and unsorted parts I dont mean it creats two other lists and puts numbers in them, it split them by initializing a variable that keeps the index.)
МMThere's a slight difference between insertion sort and bubble sort :
Bubble sort breaks a bubble in every iteration (if there's any).
A bubble is when a number in lower index is greater than a number in higher indexes if u wanna sort the list in ascending order. (Like the if statement in ur code)
Ho
thank you for clearing the doubt... i will try it..
id 1353347414thank you for clearing the doubt... i will try it..
Also there's this site that I found useful when I learned about these algos. It might help u too so here's the link
https://visualgo.net/en/sorting
МMAlso there's this site that I found useful when I learned about these algos. It might help u too so here's the link
https://visualgo.net/en/sorting
thank you so much, now my doubt is clear between insertion, bubble and selection sort