quick sort
Download
Skip this Video
Download Presentation
Quick Sort

Loading in 2 Seconds...

play fullscreen
1 / 32

Quick Sort - PowerPoint PPT Presentation


  • 100 Views
  • Uploaded on

Quick Sort. Quick Sort. Divide : Pick any element p as the pivot , e.g, the first element Partition the remaining elements into FirstPart, which contains all elements < p SecondPart, which contains all elements ≥ p Recursively sort the FirstPart and SecondPart

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Quick Sort' - marilu


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
quick sort1
Quick Sort
  • Divide:
      • Pick any element p as the pivot, e.g, the first element
      • Partition the remaining elements into

FirstPart,which contains all elements< p

SecondPart, which contains all elements ≥ p

  • Recursively sort the FirstPart and SecondPart
  • Combine: no work is necessary since sorting

is done in place

quick sort2

Partition

Recursive call

p

p

p

p ≤ x

p ≤ x

Sorted

FirstPart

Sorted

SecondPart

x < p

Quick Sort

A:

pivot

FirstPart

SecondPart

x < p

Sorted

quick sort3
Quick Sort

Quick-Sort(A, left, right)

ifleft ≥ right return

else

middle ← Partition(A, left, right)

Quick-Sort(A, left, middle–1 )

Quick-Sort(A, middle+1, right)

end if

partition

p

x < p

p

p ≤ x

x < p

p ≤ x

p

Partition

A:

A:

A:

p

partition example1
Partition Example

i=0

A:

4

8

6

3

5

1

7

2

j=1

partition example2
Partition Example

i=0

A:

4

8

6

3

5

1

7

2

8

j=1

partition example3
Partition Example

i=0

A:

4

8

6

6

3

5

1

7

2

j=2

partition example4

i=1

3

8

Partition Example

i=0

A:

4

6

3

5

1

7

2

8

3

j=3

partition example5

i=1

Partition Example

A:

4

3

6

8

5

1

7

2

5

j=4

partition example6

i=1

Partition Example

A:

4

3

6

8

5

1

1

7

2

j=5

partition example7

i=2

1

6

Partition Example

A:

4

3

6

8

5

1

7

2

j=5

partition example8

i=2

Partition Example

A:

7

4

3

1

8

5

6

7

2

j=6

partition example9

i=2

i=3

Partition Example

A:

8

2

2

4

3

1

8

5

6

7

2

j=7

partition example12

x < 4

4 ≤ x

Partition Example

pivot in

correct position

A:

2

4

3

1

5

6

7

8

slide19
Partition(A, left, right)
  • x ← A[left]
  • i ← left
  • for j ← left+1 to right
  • if A[j] < x then
  • i ← i + 1
  • swap(A[i], A[j])
  • end if
  • end for j
  • swap(A[i], A[left])
  • return i

n = right – left +1

Time: cn for some constant c

Space: constant

quick sort a 0 7

2 3 1

4

5 6 7 8

Quick-Sort(A, 0, 7)

Partition

A:

4 8 6 3 5 1 7 2

slide21

1

3

2

Quick-Sort(A, 0, 7)

, partition

Quick-Sort(A, 0, 2)

A:

5 6 7 8

4

2 3 1

slide22

Quick-Sort(A, 0, 7)

Quick-Sort(A, 0, 0)

, base case

, return

5 6 7 8

4

3

1

2

1

slide23

3

3

Quick-Sort(A, 0, 7)

Quick-Sort(A, 1, 1)

, base case

5 6 7 8

4

1

2

slide24

1

1

3

3

2

2

Quick-Sort(A, 0, 7)

Quick-Sort(A, 0, 2), return

Quick-Sort(A, 2, 2),return

5 6 7 8

4

slide25

5 6 7 8

6 7 8

5

1

3

2

Quick-Sort(A, 0, 7)

Quick-Sort(A, 4, 7)

, partition

Quick-Sort(A, 2, 2),return

4

slide26

1

3

2

6

7 8

Quick-Sort(A, 0, 7)

Quick-Sort(A, 5, 7)

, partition

4

5

6

6 7 8

slide27

1

3

2

Quick-Sort(A, 0, 7)

Quick-Sort(A, 6, 7)

, partition

4

5

6

7

7 8

8

slide28

1

3

2

Quick-Sort(A, 0, 7)

, return

, base case

Quick-Sort(A, 7, 7)

4

5

6

7

8

8

slide29

1

3

2

Quick-Sort(A, 0, 7)

, return

Quick-Sort(A, 6, 7)

4

5

6

7

8

slide30

1

3

2

6

7

8

Quick-Sort(A, 0, 7)

, return

Quick-Sort(A, 5, 7)

4

5

slide31

5

1

3

2

6

7

8

Quick-Sort(A, 0, 7)

, return

Quick-Sort(A, 4, 7)

4

slide32

5

1

3

2

6

7

8

Quick-Sort(A, 0, 7)

, done!

Quick-Sort(A, 0, 7)

4

ad