G
G
gyladti2017-07-29 20:58:11
Python
gyladti, 2017-07-29 20:58:11

How to reduce the complexity of the algorithm?

Good evening, please explain I just started learning python, there is a list, it needs to be sorted in ascending order without sort() or sorted(). I made up the code, but its complexity is O(n**2). Is it possible to simplify my algorithm?

a = list(map(int, input().split()))

n = 0
while n < len(a):
    for i in range(1, len(a)):
        if a[i - 1] > a[i]:
            a[i - 1], a[i] = a[i], a[i - 1]
    n += 1
print(a)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2017-07-29
@gyladti

Here is a list of sorting algorithms , and each has a difficulty.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question