Y
Y
Yaroslav #2021-12-11 19:29:14
Python
Yaroslav #, 2021-12-11 19:29:14

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)


Code visualization
запрос-получение списка

61b4d159b9536365286953.png

определение самого большого числа в списке

61b4d182503f7642294799.png

запись самого большого числа в новый список

61b4d18f32081694419818.png

получение вспомогательного списка без самого большого числа и его отправка повторную на фильтрацию

61b4d19e6ef75712888638.png

после того как новый список сравнялся длиной с изначальным цикл прерывается

61b4d1b1b36fc795275545.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2021-12-11
@Rsa97

List of sorting algorithms

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question