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}')
5 Respostas
+ 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
+ 2
TQ you for all,finally got cleared
0
Second part need some detailed explanation,
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}')