Escolar Documentos
Profissional Documentos
Cultura Documentos
#3.2 - Quicksort
#3.2 - Quicksort
DE DADOS I
6 1 12 40 1 29 2 3 17 5
int partition(int [] v, int low, int high) {
int i, p, firsthigh;
firsthigh = low;
p = high;
for (i=low; i<high; i++)
if (v[i] < v[p]) {
swap(v, i, firsthigh);
firsthigh = firsthigh + 1;
}
swap(v, p, firsthigh);
return firsthigh;
}
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5 3
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = ?
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = ?
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5 8
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2)
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5 9
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2)
pivot = ?
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2)
pivot = 2
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2)
pivot = 2
quicksort(v, 0, 1)
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2)
pivot = 2
quicksort(v, 0, 1)
pivot = ?
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2)
pivot = 2
quicksort(v, 0, 1)
pivot = 0
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2)
pivot = 2
quicksort(v, 0, 1)
pivot = 0
v
quicksort(v, 0, -1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2)
pivot = 2
quicksort(v, 0, 1)
pivot = 0
v
quicksort(v, 0, -1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2)
pivot = 2
quicksort(v, 0, 1)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2)
pivot = 2
quicksort(v, 0, 1)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2)
pivot = 2
quicksort(v, 0, 1)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2)
pivot = 2
quicksort(v, 0, 1) quicksort(v, 3, 2)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 20 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2)
pivot = 2
quicksort(v, 0, 1) quicksort(v, 3, 2)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 2117 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2)
pivot = 2
quicksort(v, 0, 1) quicksort(v, 3, 2)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 22 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2) quicksort(v, 4, 3)
pivot = 2
quicksort(v, 0, 1) quicksort(v, 3, 2)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 23 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2) quicksort(v, 4, 3)
pivot = 2
quicksort(v, 0, 1) quicksort(v, 3, 2)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 24 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3)
pivot = 3
quicksort(v, 0, 2) quicksort(v, 4, 3)
pivot = 2
quicksort(v, 0, 1) quicksort(v, 3, 2)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 25 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3
quicksort(v, 0, 2) quicksort(v, 4, 3)
pivot = 2
quicksort(v, 0, 1) quicksort(v, 3, 2)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = ?
quicksort(v, 0, 2) quicksort(v, 4, 3)
pivot = 2
quicksort(v, 0, 1) quicksort(v, 3, 2)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
quicksort(v, 0, 2) quicksort(v, 4, 3)
pivot = 2
quicksort(v, 0, 1) quicksort(v, 3, 2)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
quicksort(v, 0, 1) quicksort(v, 3, 2)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
quicksort(v, 0, 1) quicksort(v, 3, 2)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
quicksort(v, 0, 1) quicksort(v, 3, 2)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
quicksort(v, 0, 1) quicksort(v, 3, 2)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
quicksort(v, 0, 1) quicksort(v, 3, 2)
pivot = 0
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 34 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 35 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
v
quicksort(v, 0, -1) quicksort(v, 1, 1) 0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 36 17 5
Quicksort (árvore de recursão)
quicksort(v, 0, 9)
pivot = 4
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
quicksort(v, 0, 2) quicksort(v, 4, 3) quicksort(v, 5, 4) quicksort(v, 6, 9)
pivot = 2 pivot = 8
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
quicksort(v, 0, 3) quicksort(v, 5, 9)
pivot = 3 pivot = 5
v
0 1 2 3 4 5 6 7 8 9
6 1 12 40 1 29 2 3 17 5
6 1 12 40 1 29 2 3 17 5
Executa: quicksort(v, 0, 9)
48
6 1 12 40 1 29 2 3 17 5
6 1 12 40 1 29 2 3 17 5
low high
firsthigh p
i
6 1 12 40 1 29 2 3 17 5
low high
firsthigh p
i
6 1 12 40 1 29 2 3 17 5
low high
firsthigh p
i
6 1 12 40 1 29 2 3 17 5
low high
firsthigh p
i
6 1 12 40 1 29 2 3 17 5
low high
firsthigh p
i
1 6 12 40 1 29 2 3 17 5
low high
firsthigh p
i
1 6 12 40 1 29 2 3 17 5
low high
firsthigh p
i
1 6 12 40 1 29 2 3 17 5
low high
firsthigh p
i
1 6 12 40 1 29 2 3 17 5
low high
firsthigh p
i
1 6 12 40 1 29 2 3 17 5
low high
firsthigh p
i
1 6 12 40 1 29 2 3 17 5
low high
firsthigh p
i
1 6 12 40 1 29 2 3 17 5
low high
firsthigh p
i
1 6 12 40 1 29 2 3 17 5
low high
firsthigh p
i
1 6 12 40 1 29 2 3 17 5
low high
firsthigh p
i
1 1 12 40 6 29 2 3 17 5
low high
firsthigh p
i
1 1 12 40 6 29 2 3 17 5
low high
firsthigh p
i
1 1 12 40 6 29 2 3 17 5
low high
firsthigh p
i
1 1 12 40 6 29 2 3 17 5
low high
firsthigh p
i
1 1 12 40 6 29 2 3 17 5
low high
firsthigh p
i
1 1 12 40 6 29 2 3 17 5
low high
firsthigh p
i
1 1 12 40 6 29 2 3 17 5
low high
firsthigh p
i
1 1 2 40 6 29 12 3 17 5
low high
firsthigh p
i
1 1 2 40 6 29 12 3 17 5
low high
firsthigh p
i
1 1 2 40 6 29 12 3 17 5
low high
firsthigh p
i
1 1 2 40 6 29 12 3 17 5
low high
firsthigh p
i
1 1 2 40 6 29 12 3 17 5
low high
firsthigh p
i
1 1 2 3 6 29 12 40 17 5
low high
firsthigh p
i
1 1 2 3 6 29 12 40 17 5
low high
firsthigh p
i
1 1 2 3 6 29 12 40 17 5
low high
firsthigh p
i
1 1 2 3 6 29 12 40 17 5
low high
firsthigh p
i
1 1 2 3 6 29 12 40 17 5
low high
firsthigh p
i
1 1 2 3 6 29 12 40 17 5
low high
firsthigh p
i
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
i
Retorna: firsthigh = 4
Executa: pivot = partition(v, 0, 9)
82
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
Executa: quicksort(v, 0, 3)
Retornou: pivot = partition(v, 0, 9) = 4
84
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
i
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
i
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
i
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
i
1 1 2 3 5 29 12 40 17 6
low high
p
i firsthigh
1 1 2 3 5 29 12 40 17 6
low high
p
firsthigh
1 1 2 3 5 29 12 40 17 6
low high
p
firsthigh
1 1 2 3 5 29 12 40 17 6
low high
p
firsthigh
Retorna: firsthigh = 3
Executa: pivot = partition(v, 0, 3)
Executa: quicksort(v, 0, 3)
Retornou: pivot = partition(v, 0, 9) = 4
103
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
Executa: quicksort(v, 0, 2)
Retournou: pivot = partition(v, 0, 3) = 3
Executa: quicksort(v, 0, 3)
Retornou: pivot = partition(v, 0, 9) = 4
105
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
p
i firsthigh
1 1 2 3 5 29 12 40 17 6
low high
p
firsthigh
1 1 2 3 5 29 12 40 17 6
low high
p
firsthigh
1 1 2 3 5 29 12 40 17 6
low high
p
firsthigh
Retorna: firsthigh = 2
Executa: pivot = partition(v, 0, 2)
Executa: quicksort(v, 0, 2)
Retournou: pivot = partition(v, 0, 3) = 3
Executa: quicksort(v, 0, 3)
Retornou: pivot = partition(v, 0, 9) = 4
119
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
Executa: quicksort(v, 0, 1)
Retornou: pivot = partition(v, 0, 2) = 2
Executa: quicksort(v, 0, 2)
Retournou: pivot = partition(v, 0, 3) = 3
Executa: quicksort(v, 0, 3)
Retornou: pivot = partition(v, 0, 9) = 4
121
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
Retorna: firsthigh = 0
Executa: pivot = partition(v, 0, 1)
Executa: quicksort(v, 0, 1)
Retornou: pivot = partition(v, 0, 2) = 2
Executa: quicksort(v, 0, 2)
Retournou: pivot = partition(v, 0, 3) = 3
Executa: quicksort(v, 0, 3)
Retornou: pivot = partition(v, 0, 9) = 4
127
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
Executa: quicksort(v, 1, 1)
Retornou: pivot = partition(v, 0, 1) = 0
Executa: quicksort(v, 0, 1)
Retornou: pivot = partition(v, 0, 2) = 2
Executa: quicksort(v, 0, 2)
Retournou: pivot = partition(v, 0, 3) = 3
Executa: quicksort(v, 0, 3)
Retornou: pivot = partition(v, 0, 9) = 4
132
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
Retorna recursão.
Retornou: pivot = partition(v, 0, 1) = 0
Executa: quicksort(v, 0, 1)
Retornou: pivot = partition(v, 0, 2) = 2
Executa: quicksort(v, 0, 2)
Retournou: pivot = partition(v, 0, 3) = 3
Executa: quicksort(v, 0, 3)
Retornou: pivot = partition(v, 0, 9) = 4
135
1 1 2 3 5 29 12 40 17 6
Executa: quicksort(v, 3, 2)
Retornou: pivot = partition(v, 0, 2) = 2
Executa: quicksort(v, 0, 2)
Retournou: pivot = partition(v, 0, 3) = 3
Executa: quicksort(v, 0, 3)
Retornou: pivot = partition(v, 0, 9) = 4
136
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
Retorna recursão.
Retornou: pivot = partition(v, 0, 2) = 2
Executa: quicksort(v, 0, 2)
Retournou: pivot = partition(v, 0, 3) = 3
Executa: quicksort(v, 0, 3)
Retornou: pivot = partition(v, 0, 9) = 4
139
1 1 2 3 5 29 12 40 17 6
Executa: quicksort(v, 4, 3)
Retournou: pivot = partition(v, 0, 3) = 3
Executa: quicksort(v, 0, 3)
Retornou: pivot = partition(v, 0, 9) = 4
140
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
Retorna recursão.
Retournou: pivot = partition(v, 0, 3) = 3
Executa: quicksort(v, 0, 3)
Retornou: pivot = partition(v, 0, 9) = 4
143
1 1 2 3 5 29 12 40 17 6
Executa: quicksort(v, 5, 9)
Retornou: pivot = partition(v, 0, 9) = 4
144
1 1 2 3 5 29 12 40 17 6
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 29 12 40 17 6
low high
firsthigh p
1 1 2 3 5 6 12 40 17 29
low high
firsthigh p
Retorna: firsthigh = 5
Executa: pivot = partition(v, 5, 9)
Executa: quicksort(v, 5, 9)
Retornou: pivot = partition(v, 0, 9) = 4
156
1 1 2 3 5 6 12 40 17 29
1 1 2 3 5 6 12 40 17 29
Executa: quicksort(v, 5, 4)
Retornou: pivot = partition(v, 5, 9) = 5
Executa: quicksort(v, 5, 9)
Retornou: pivot = partition(v, 0, 9) = 4
158
1 1 2 3 5 6 12 40 17 29
1 1 2 3 5 6 12 40 17 29
1 1 2 3 5 6 12 40 17 29
Executa: quicksort(v, 6, 9)
Retornou: pivot = partition(v, 5, 9) = 5
Executa: quicksort(v, 5, 9)
Retornou: pivot = partition(v, 0, 9) = 4
161
1 1 2 3 5 6 12 40 17 29
1 1 2 3 5 6 12 40 17 29
low high
firsthigh p
1 1 2 3 5 6 12 40 17 29
low high
firsthigh p
1 1 2 3 5 6 12 40 17 29
low high
firsthigh p
1 1 2 3 5 6 12 40 17 29
low high
firsthigh p
1 1 2 3 5 6 12 40 17 29
low high
firsthigh p
1 1 2 3 5 6 12 40 17 29
low high
firsthigh p
1 1 2 3 5 6 12 40 17 29
low high
firsthigh p
1 1 2 3 5 6 12 40 17 29
low high
firsthigh p
1 1 2 3 5 6 12 40 17 29
low high
firsthigh p
1 1 2 3 5 6 12 40 17 29
low high
firsthigh p
1 1 2 3 5 6 12 17 40 29
low high
firsthigh p
1 1 2 3 5 6 12 17 40 29
low high
firsthigh p
1 1 2 3 5 6 12 17 40 29
low high
firsthigh p
1 1 2 3 5 6 12 17 40 29
low high
firsthigh p
1 1 2 3 5 6 12 17 29 40
low high
firsthigh p
Retorna: firsthigh = 8
Executa: pivot = partition(v, 6, 9)
Executa: quicksort(v, 6, 9)
Retornou: pivot = partition(v, 5, 9) = 5
Executa: quicksort(v, 5, 9)
Retornou: pivot = partition(v, 0, 9) = 4
177
1 1 2 3 5 6 12 17 29 40
1 1 2 3 5 6 12 17 29 40
Executa: quicksort(v, 6, 7)
Retornou: pivot = partition(v, 6, 9) = 8
Executa: quicksort(v, 6, 9)
Retornou: pivot = partition(v, 5, 9) = 5
Executa: quicksort(v, 5, 9)
Retornou: pivot = partition(v, 0, 9) = 4
179
1 1 2 3 5 6 12 17 29 40
1 1 2 3 5 6 12 17 29 40
low high
firsthigh p
1 1 2 3 5 6 12 17 29 40
low high
firsthigh p
1 1 2 3 5 6 12 17 29 40
low high
firsthigh p
1 1 2 3 5 6 12 17 29 40
low high
firsthigh p
1 1 2 3 5 6 12 17 29 40
low high
p
i firsthigh
1 1 2 3 5 6 12 17 29 40
low high
p
firsthigh
1 1 2 3 5 6 12 17 29 40
low high
p
firsthigh
1 1 2 3 5 6 12 17 29 40
low high
p
firsthigh
Retorna: firsthigh = 7 i
1 1 2 3 5 6 12 17 29 40
1 1 2 3 5 6 12 17 29 40
Executa: quicksort(v, 6, 6)
Retornou: pivot = partition(v, 6, 7) = 7
Executa: quicksort(v, 6, 7)
Retornou: pivot = partition(v, 6, 9) = 8
Executa: quicksort(v, 6, 9)
Retornou: pivot = partition(v, 5, 9) = 5
Executa: quicksort(v, 5, 9)
Retornou: pivot = partition(v, 0, 9) = 4
190
1 1 2 3 5 6 12 17 29 40
1 1 2 3 5 6 12 17 29 40
1 1 2 3 5 6 12 17 29 40
Executa: quicksort(v, 8, 7)
Retornou: pivot = partition(v, 6, 7) = 7
Executa: quicksort(v, 6, 7)
Retornou: pivot = partition(v, 6, 9) = 8
Executa: quicksort(v, 6, 9)
Retornou: pivot = partition(v, 5, 9) = 5
Executa: quicksort(v, 5, 9)
Retornou: pivot = partition(v, 0, 9) = 4
193
1 1 2 3 5 6 12 17 29 40
1 1 2 3 5 6 12 17 29 40
1 1 2 3 5 6 12 17 29 40
Retorna recursão.
Retornou: pivot = partition(v, 6, 7) = 7
Executa: quicksort(v, 6, 7)
Retornou: pivot = partition(v, 6, 9) = 8
Executa: quicksort(v, 6, 9)
Retornou: pivot = partition(v, 5, 9) = 5
Executa: quicksort(v, 5, 9)
Retornou: pivot = partition(v, 0, 9) = 4
196
1 1 2 3 5 6 12 17 29 40
Executa: quicksort(v, 9, 9)
Retornou: pivot = partition(v, 6, 9) = 8
Executa: quicksort(v, 6, 9)
Retornou: pivot = partition(v, 5, 9) = 5
Executa: quicksort(v, 5, 9)
Retornou: pivot = partition(v, 0, 9) = 4
197
1 1 2 3 5 6 12 17 29 40
1 1 2 3 5 6 12 17 29 40
1 1 2 3 5 6 12 17 29 40
Retorna recursão.
Retornou: pivot = partition(v, 6, 9) = 8
Executa: quicksort(v, 6, 9)
Retornou: pivot = partition(v, 5, 9) = 5
Executa: quicksort(v, 5, 9)
Retornou: pivot = partition(v, 0, 9) = 4
200
1 1 2 3 5 6 12 17 29 40
Retorna recursão.
Retornou: pivot = partition(v, 5, 9) = 5
Executa: quicksort(v, 5, 9)
Retornou: pivot = partition(v, 0, 9) = 4
201
1 1 2 3 5 6 12 17 29 40
Retorna recursão.
Retornou: pivot = partition(v, 0, 9) = 4
202
1 1 2 3 5 6 12 17 29 40
1 1 2 3 5 6 12 17 29 40