Hacer un programa para organizar una lista de números en orden descendente sin algoritmos de clasificación incorporados

I was told to do this as homework. So far I did the following:

a = raw_input("Please enter a number:")
b = a.split()
for c in range(len(b)):
    b[c] = int(b[c])


while b != []:
    for i in range(len(b)):
        if big < b[i]:

print n

He aquí un ejemplo:


And the output is suppose to be:


The challenge is not to use sorting algorithms, which is hard for me to do since I don't know how to get around it.

The basis of that program is from:

a = raw_input("Please enter a number:")
b = a.split() 
for c in range(len(b)):
  b[c] = int(b[c])

small=b[0] while b != []:
    for i in range(len(b)):
        if small > b[i]:

print n

preguntado el 10 de marzo de 12 a las 08:03

Er... arranging numbers in order is a sorting algorithm. -

What do you mean "not to use sorting algorithms"? By definition any algorithm that places things in sorted order is a sorting algorithm. -

I mean that I can't use built-in sorting algorithms in python. -

el cual algoritmo de clasificación are you trying to implement? -

Shouldn't the input and output contain the same digits? -

3 Respuestas

Podrías probar algo como esto:

a = raw_input("Please enter a number:")
b = map(int, a)
while b:
    smallest = min(b)
print ''.join(map(str, n))

Véalo funcionando en línea: idea

It's the same algorithm as your existing code, but I've fixed a number of errors. Note that this is not an efficient algorithm. It runs in O(n2) time. Using the builtin sorted function you could achieve O(n log(n)) time, and with a contando ordenar you could even reach O(n) time.

Note that this sorts in ascendente order, not descending order. Since this is homework I'll leave it as an exercise for you to figure out what one thing you need to change to fix this.

Here are some other notes about the changes I made to your program:

  • Don't call split on your input. That splits on whitespace, which is probably not what you want since your example input doesn't contain any whitespace.
  • solía map en lugar de un for loop to convert all elements in a sequence.
  • solía min to find the smallest value instead of iterating over the sequence.

respondido 10 mar '12, 08:03

I tried it. Looks good since I'm familiar with some of the coding here. Also figured out what to change to make it descending. I got a question though... is it possible to get output in brackets? - Mark Soo

@MarkSoo: What do you mean? Like this? print ','.join('(' + x + ')' for x in n) - marca byers

Well just the output in brackets like this: [654311]. PS: I'm not sure where I can input that... - Mark Soo

@MarkSoo: print '[' + ''.join(map(str, n)) + ']' - marca byers

Maybe not the best way to sort a list but here is another example :) Im even able to do it without sorting them.

from Queue import Queue
import threading
import time

def sleeper(x, queue):

a = [3,2,1,4,3,2,5]
longest = max(a)
threads = {}
queue = Queue()
for num in a:
    threads[num] = threading.Thread(target=sleeper, args=(num, queue))

while not queue.empty():
    print queue.get()

respondido 10 mar '12, 08:03

For Python you can try the following code :

def Descending_Order(num): return int("".join(sorted(str(num), reverse=True)))

Respondido 18 ago 17, 12:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.