+ 2
Sorting list without builtin functions
Hi i am learning python. Our mentor made homework: to sort this list [1, 2, 4, -5, 7, 9, 3, 2], in ascending order. Without using sort, min max. We studied for now Loops, List , if condition. What functions should i use? While ? In range?
3 Antworten
+ 1
You can use for loop (in range).
Tips : use a for loop into an other for loop...
0
(⌐■_■)
Hope this link helps you
https://www.sololearn.com/compiler-playground/c75as28IuUKW
lst = [1, 2, 4, -5, 7, 9, 3, 2]
for x in range(len(lst)):
for y in range(len(lst)-1):
if lst[y]>lst[y+1]:
lst[y],lst[y+1] = lst[y+1],lst[y]
x=0
print(lst)
0
list=[1, 2, 4, -5, 7, 9, 3, 2]
b=True
while b:
b=False
for i in range(len(list)-1):
if list[i]>list[i+1]:
b=True
list[i],list[i+1]=list[i+1],list[i]
print(list)