Answer the question
In order to leave comments, you need to log in
How to write a sorting algorithm?
I started learning the language, now I'm in the lists section. I decided to write an algorithm that arranges numbers entered through a space in descending order. I want to know if there is an easier way to do this other than the automatic sorted function. The interest is purely student, the desire to find a different approach to the algorithm without introducing more advanced features than the basic ones.
THE CODE:
z = list(map(int, input().split()))
b = len(z)
x = []
c = []
v = []
tmp = -1
ask = True
while ask:
for i in z:
if i > tmp:
tmp = i
continue
else:
x = []
x += [tmp]
#print (x)
for i in z:
if i in x:
c.extend([i])
#print(c)
v = []
for i in z:
if not (i in c):
v.extend([i])
z = v
x = []
tmp = 0
if len(c) == b:
ask = False
print(c)
запрос-получение списка
определение самого большого числа в списке
запись самого большого числа в новый список
получение вспомогательного списка без самого большого числа и его отправка повторную на фильтрацию
после того как новый список сравнялся длиной с изначальным цикл прерывается
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question