0

Python,can any one help in explaining this

list1=[] n=int(input("Enter how many elements?")) for i in range(n): ele=int(input("Enter any integer")) list1.append(ele) print(f'Before sorting elements {list1}') for i in range(len(list1)): for j in range(len(list1)-1): if list1[j]>list1[j+1]: list1[j],list1[j+1]=list1[j+1],list1[j] print(f'After Sorting elements {list1}')

26th Jan 2022, 4:56 PM
NAVEEN VYAS
5 Answers
+ 2
First part of the code gets a list from the user. Second part sort that list by determining if the values next to each other is greater than the value on the right. If so swap the left with the right. Repeat the same logic n times where n is the size of the list. This is called a bubble sort. here's an image of what it's doing https://images.app.goo.gl/beKesaqdpmgh4Hpj6
26th Jan 2022, 5:03 PM
Sandy Ho
Sandy Ho - avatar
+ 2
TQ you for all,finally got cleared
26th Jan 2022, 6:08 PM
NAVEEN VYAS
0
Second part need some detailed explanation,
26th Jan 2022, 5:06 PM
NAVEEN VYAS
0
Thank you I understood clear,but still small little confused how this logic is applicable..., for i in range(len(list1)): for j in range(len(list1)-1): if list1[j]>list1[j+1]: list1[j],list1[j+1]=list1[j+1],list1[j] print(f'After Sorting elements {list1}')
26th Jan 2022, 5:31 PM
NAVEEN VYAS