-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsimple_quick_sort.py
More file actions
33 lines (28 loc) · 1.07 KB
/
simple_quick_sort.py
File metadata and controls
33 lines (28 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import random
def quickSort(array, leftIndex, rightIndex):
#Define o pivot como primeiro elemento.
pivot = array[leftIndex]
#i, é o valor que define o próximo valor do array a ser trocado -1.
i = leftIndex
#loop que varre o array procurando os valores menores que o pivot e trocando com o i.
for j in range(i+1, rightIndex):
if (array[j] < pivot):
i += 1
array[i], array[j] = array[j], array[i]
#por fim, troca o i com o pivot, a fim de deixar todos os menores que o pivot na esquerda, e os maiores a direita.
array[i], array[leftIndex] = array[leftIndex], array[i]
#Executa a mesma ação de forma recursiva até que só sobre um elemento no array.
quickSort(array, leftIndex, i)
quickSort(array, i+1, rightIndex)
tentativas = 0
#Test
while True:
h = random.sample(range(0,100),100)
ordenada_certa = sorted(h)
quickSort(h, 0, len(h))
if ordenada_certa != h:
print (h)
break
else:
tentativas += 1
print ("Arrays ordenados com sucesso: "+str(tentativas))