I have already done a 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 code above, you will notice that it is SelectionSort , but with a difference, SelectionSort for execution, to "ask" the user who is the largest vector item "list" and reflects in the "note" vector.
Example:
Imagine that I put this item as an entry: adult, baby, teen, child. and make the comparison in "who is older?" Then the vectors list will be after the input:
lista = [adulto, bebe, adolencete, criança]
nota = [0, 0, 0, 0]
And after the comparison it will look like this:
lista = [adulto, bebe, adolencete, criança]
nota = [3 , 0, 2, 1]
But I wanted to make a "quicksort" version, but every attempt I make quicksort compare to me. I want a "quicksort" to "stop" execution and ask me who is bigger or not, and reflect the "note" vector
I did a quicksort version, but I do not 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)
Resolved
I get, the goal in my algorithm is to use Quicksort to sort things like, "Hollywood's Most Beautiful Actresses" or "Best Car of My Life", anyway, I can, 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 do you like best
Which people are most striking in your life.
Which are the best movies you've ever watched.
Now I'm going to do this in C.