How to make quicksort "manual"

Asked

Viewed 78 times

0

I’ve already made one Selectionsort manual in Pyhon see below:

lista = []
# digite 0 para encerrar a entrada de dados
while True:
  x = input('Entrada: ')
  if x == '0': break
  else: lista.append(x)
nota = []
for x in lista:
  nota.append(0)

# metodo SelectionSort
for a in range(len(lista)):
  for b in range(a + 1, len(lista)):
    while True:
      print('1: %s' % lista[a])
      print('2: %s' % lista[b])
      x = int(input('Digite: '))
      print()
      if x >= 0 and x <= 2: break
    if x == 1:
      nota[a] = nota[a] + 1
    elif x == 2:
      nota[b] = nota[b] + 1

If you run the above code, you will realize that it is about Selectionsort, but with one difference, the Selectionsort for the execution, to "ask" the user who is the largest item of the "list" vector and reflects in the "note" vector".

Example:

Imagine that I put as input this item: adult, baby, teenager, child. and compare in "who is older?" Then the list vectors will be after the entry:

lista = [adulto, bebe, adolencete, criança]  
nota = [0, 0, 0, 0]

And after the comparison will look like this:

lista = [adulto, bebe, adolencete, criança]  
nota = [3 , 0, 2, 1]

But I wanted to make a "quicksort" version, but all the attempts I make the quicksort compare to me. I want a "quicksort" that "stops" the execution and ask me who is bigger or not, and reflect the vector "note"

I did a quicksort version, but I don’t know where I’m going wrong. (I’m 1 week into this)

lista = []
# digite 0 para encerrar a entrada de dados
while True:
  x = input('Entrada: ')
  if x == '0': break
  else: lista.append(x)

nota = []
for x in lista:
  nota.append(0)

# metodo Quicksort
def quick(vetor):
  if len(vetor) <= 1:
    return vetor
menor, igual, maior = [], [], []
meio = len(vetor) // 2
pivo = vetor[meio]
for x in vetor:
  while True:
    print('1: %s' % x)
    print('2: %s' % pivo)
    a = int(input('Digite: '))
    print()
    if a >= 0 and a <= 2: break
  if a == 2:
    menor.append(x)
    b = vetor.index(x)
    nota[b] = nota[b] + 1
  elif a == 1:
    maior.append(x)
    b = vetor.index(pivo)
    nota[b] = nota[b] + 1
  else: igual.append(x)
return quick(menor) + igual + quick(maior)
quick(lista)

Solved

I get it, the objtivo in my algorithm is to use Quicksort to sort things like, "Most beautiful actresses of Hollywood" or "Best car of my life", anyway, I get it, see the solution below:

def perguntar(a, b):
while True:
  print('1: %s' % a)
  print('2: %s' % b)
  x = int(input('Digite: '))
  print()
  if x >= 0 and x <= 2: break
return x
def quick(v):
  if len(v) <= 1:
    return v
  else:
    less = []
    meio = []
    more = []
    n = len(v) // 2
    pivo = v[n]
    for i in v:
      if i == pivo:
        meio.append(i)
      else:
        r = perguntar(i, pivo)
        if r == 1:
          less.append(i)
        else:
          more.append(i)
    print(less,meio,more)
    print()
    return quick(less) + quick(meio) + quick(more)
def ver(lista):
  for x in range(len(lista)):
    print('%2d: %s' % (x+1, lista[x]))
while True:
  lista = []
  ## digite 0 para parar a entrada de dados.
  ## digite sair para parar.
  while True:
    x = input('Entrada: ')
    if x == '0' or x == 'sair': break
    else:
      if lista != 'sair':
        lista.append(x)
  lista = quick(lista)
  ver(lista)
  if x == 'sair': break

Who wants to test, use as data input:
Which programming languages you like best.
Which people are most striking in your life.
What are the best movies you’ve ever seen.

now I’m gonna do this in C.

  • 1

    failed to comment on what the problem is with your attempt. What happens? Gives error? Gives result?

  • Gives error, results come out wrong, but in version "selectionsort" comes out right

  • 1

    edit the question, put this information... Put together the input example you used which to test, the result you gave and how it would be right, point out where the problem is, all this in the question.

  • Thank you Nosklo, I’ve edited the question

No answers

Browser other questions tagged

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