Python- I need to make an application that receives float values and organize them without using . Sort

Asked

Viewed 51 times

-6

So far I’ve managed to do the following:

lista = []
a = 1
b = 0
while a > 0:
    a = float(input("Colque os valores: "))
    b = b + 1
    for i in range (b):
        lista.append(a)
n = len(lista)
for i in range(n-1):
    for j in range(n-1):
        if lista[i] > lista[i+1]:
            lista[i], lista[i+1] = lista[i+1], lista[i]
lista.reverse()
print("Primeiro lugar: ", lista[0], "Segundo lugar: ", lista[1], "Terceiro lugar: ", lista[2])

But the code in question is not working properly, can help me?

2 answers

1

@soaresde, I believe you’re looking to implement a bubble sort, right?

There are two problems with your script:

  1. Data entry
  2. Ordering loop

Data entry

while a > 0:
    a = float(input("Colque os valores: "))
    if a > 0:
        lista.append(a)

Ordering loop

for num in range(n-1,0,-1):
    for i in range(num):
        if lista[i]>lista[i+1]:
            lista[i], lista[i+1] = lista[i+1], lista[i] 

Note that the outside loop "walks backwards", while the inside "walks front" and starting at the number num

Example

>>> lista = [4.3, 2.1, 3.5, -1.7, 0.1, -6.2, 5.4, 7.5, 9.1, 8.4]
>>> n = len(lista)

>>> for num in range(n-1,0,-1):
...     for i in range(num):
...         if lista[i]>lista[i+1]:
...             lista[i], lista[i+1] = lista[i+1], lista[i]
...

>>> lista
[-6.2, -1.7, 0.1, 2.1, 3.5, 4.3, 5.4, 7.5, 8.4, 9.1]

Even though your script does not accept negative numbers, the bubble sort would accept the same without problems.

I hope I’ve helped

0

Another way to solve this question is by using the sort algorithm shell_sort.

To implement this algorithm we must:

  1. Capture values that will be mounted in the list;
  2. Sort the values from that list;
  3. Display the ordered values.

This way we have:

def shell_sort(v):
    h = len(v) // 2
    while h > 0:
        i = h
        while i < len(v):
            temp = v[i]
            troca = False
            j = i - h
            while (j >= 0) and (v[j] > temp):
                v[j + h] = v[j]
                troca = True
                j -= h
            if troca:
                v[j + h] = temp
            i += 1
        h = h // 2

    return v[-1], v[-2], v[-3]


lista = list(map(float, input('Digite os valores: ').split()))

prim, seg, terc = shell_sort(lista)

print(f'O primeiro lugar é: {prim}\nO segundo lugar é: {seg}\nO terceiro lugar é: {terc}')

Note that when we execute this code we receive the message: Digite os valores. Right now we must type all the values, in the same line, separated for a single space and press Enter.

From now on the code will perform all the work of ordering.

Testing the code:

if we want to put together a list of 8 type values float and then order them, we must:

Enter the values...

4.6 8.2 3.4 2.2 -6.7 -3.3 6.7 8.8

...and press enter. Then the way out will be...

O primeiro lugar é: 8.8
O segundo lugar é: 8.2
O terceiro lugar é: 6.7

Browser other questions tagged

You are not signed in. Login or sign up in order to post.