quick sort - ft. meira/codeanimation/quicksort.pdf · pdf file...

Post on 26-Jul-2020

0 views

Category:

Documents

Embed Size (px)

TRANSCRIPT

• Notas de Aula - Prof Luis Meira - Unifesp SJC

Quick Sort

2 1 9 8 5 4 2 1 7 6

to p o

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

Quick Sort

QUICKSORT(A, p, r)

1 if p < r

2 then q ← PARTITION(A, p, r)

3 QUICKSORT(A, p, q - 1)

4 QUICKSORT(A, q + 1, r)

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

Quick Sort

PARTITION(A, p, r)

1 pivo ← A[p]

2 topo ← p

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5 4 2 1 7 6

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5 4 2 1

Pivo

7 6

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5 4 2 1

Pivo

7 6

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5 4 2 1

Pivo

7 6 to

p o

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5 4 2 1

Pivo

7 6 to

p o

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5

4

2 1

Pivo

7 6 to

p o

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5

4

2 1

Pivo

7 6 to

p o

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5

4

2 1

Pivo

7 6

i

to p o

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5

4

2 1

Pivo

7 6

i

to p o

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5

4

2 1

Pivo

7 6

i

to p o

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5

4

2 1

Pivo

7 6

i

to p o

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5

4

2 1

Pivo

7 6

i

to p o

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5

4

2 1

Pivo

7 6

i

to p o

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5

4

2 1

Pivo

7 6

i

to p o

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5

4

2 1

Pivo

7 6

i

to p o

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5

4

2 1

Pivo

7 6

i

to p o

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5

4

2 1

Pivo

7 6

i

to p o

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo

Saturday, July 11, 2009

• Notas de Aula - Prof Luis Meira - Unifesp SJC

2 1 9 8 5

4

2 1

Pivo

7 6

i

to p o

PARTITION(A, p, r)

1 topo ← p

2 pivo ← A[p]

3 for i ← p+1 to r

4 do if A[i] < Pivo

5 v[topo] ← v[i]

6 v[i] ← v[topo+1]

7 topo++

8 v[topo] ← pivo