Data Structures and Algorithms Chapter 7: Sorting (Quicksort) Text: - - PowerPoint PPT Presentation

data structures and algorithms chapter 7 sorting
SMART_READER_LITE
LIVE PREVIEW

Data Structures and Algorithms Chapter 7: Sorting (Quicksort) Text: - - PowerPoint PPT Presentation

CE 221 Data Structures and Algorithms Chapter 7: Sorting (Quicksort) Text: Read Weiss, 7.7 Izmir University of Economics 1 Quicksort As the name implies quicksort is the fastest known sorting algorithm in practice. Average running


slide-1
SLIDE 1

1

Chapter 7: Sorting (Quicksort) CE 221 Data Structures and Algorithms

Izmir University of Economics

Text: Read Weiss, § 7.7

slide-2
SLIDE 2

2

Quicksort

  • As the name implies quicksort is the

fastest known sorting algorithm in practice.

  • Average running time is O(NlogN).
  • Fast, due to a very tight and highly
  • ptimized inner loop.
  • O(N2) worst-case performance, but this

can be made exponentially unlikely with a little effort.

Izmir University of Economics

slide-3
SLIDE 3

3

Quicksort – Basic Algorithm

  • Sorting an array S consists of 4 easy steps:

quicksort(S) {

  • 1. If the number of elements in S is 0 or 1, then

return

  • 2. Pick any element v in S. This is called the pivot.
  • 3. Partition S-{v} into 2 disjoint groups:

S1={xєS-{v}| x<=v} and S2={xєS-{v}| x>=v}

  • 4. return {quicksort(S1) {v}, quicksort(S2)}

}

  • Partition step ambiguous for elements equal to the pivot.

Izmir University of Economics

slide-4
SLIDE 4

4

Picking the Pivot

Izmir University of Economics

slide-5
SLIDE 5

5

Picking the Pivot Median-of-Three Partitioning

  • The median of a group of N numbers is the ceiling(N/2)th

largest number. Unfortunately, this is hard to calculate and would slow down the quicksort considerably.

  • A good estimate can be obtained by picking three

elements randomly and using the median of these as pivot.

  • Common course is to use as pivot the median of the left,

right and center elements.

  • Example: input: 8, 1, 4, 9, 6, 3, 5, 2, 7, 0
  • leftmost element= 8, rightmost element = 0,
  • centermost element = 6 (at index floor( (left + right) / 2) )

Izmir University of Economics

slide-6
SLIDE 6

6

Partitioning Strategy

  • First step: sort these three elements and then get the

pivot element out of the way by swapping it with the next-to-last element.

Izmir University of Economics

8 1 4 9 6 3 5 2 7

left center right

1 4 9 6 3 5 2 7 8

left pivot center right

1 4 9 7 3 5 2 6 8

left center pivot right

slide-7
SLIDE 7

7

Partitioning Strategy – Example (1)

Izmir University of Economics

  • Index i starts at position (left + 1) and j starts at (right - 2).
  • move all the small elements to the left part of the array and all the large

elements to the right part. Small and large are with respect to the pivot.

  • While i is to the left of j, move i right skipping over elements that are smaller

than the pivot. We move j left, skipping over elements that are larger than the pivot.

  • When i and j have stopped, i is pointing at a large and j is pointing at a small
  • element. If i is to the left of j, i and j are swapped.

1 4 9 7 3 5 2 6 8

i j

1 4 9 7 3 5 2 6 8

i j

slide-8
SLIDE 8

8 Izmir University of Economics

Partitioning Strategy – Example (2)

After first swap After second swap

1 4 2 7 3 5 9 6 8

i j

1 4 2 7 3 5 9 6 8

i j

1 4 2 5 3 7 9 6 8

i j

slide-9
SLIDE 9

9

  • At this stage, i and j have crossed, so no swap is
  • performed. The final part is to swap the pivot

element with the element pointed to by i.

Izmir University of Economics

Partitioning Strategy – Example (3)

Before third swap After swap with the pivot IMPORTANT: In case of equality both i and j will stop.

1 4 2 5 3 7 9 6 8

j i

1 4 2 5 3 6 9 7 8

slide-10
SLIDE 10

10

Small Arrays

Izmir University of Economics

  • For very small arrays (N <= 20), quicksort does

not perform well as insertion sort.

  • Furthermore, because quicksort is recursive,

these cases will occur frequently. A common solution is not to use quicksort recursively for small arrays, but instead use a sorting algorithm that is efficient for small arrays, such as insertion sort.

  • Using this strategy can actually save about 15%

in the running time.

slide-11
SLIDE 11

11

Quicksort – Implementation (1)

Izmir University of Economics public static <AnyType> void swapReferences(AnyType [] a, int index1, int index2) { AnyType tmp = a[ index1 ]; a[ index1 ] = a[ index2 ]; a[ index2 ] = tmp; }

slide-12
SLIDE 12

12 Izmir University of Economics

Quicksort – Implementation (2)

slide-13
SLIDE 13

13 Izmir University of Economics

Quicksort – Be Careful

This is not correct

i = left + 1, j = right - 2; for( ; ; ) { while( a[ i ].compareTo(pivot) < 0 ) i++; while( a[ j ].compareTo(pivot) > 0 ) j--; if( i < j ) swapReferences( a, i, j ); else break; }

slide-14
SLIDE 14

Analysis of Quicksort

Izmir University of Economics 14

  • Assume a random pivot (no median-of-three

partitioning), no cutoff

– T(0) = T(1) = 1 – T(N) = T(i) + T(N-i-1) + cN where i = |S1| is the number of elements in S1.

  • Worst-case, Best-case, Average-Case analyses

will be performed.

slide-15
SLIDE 15

Worst-Case Analysis

  • If the pivot is the smallest element, all the time.

Then i = 0 and if we ignore T(0)=1,

– T(N)=T(N-1)+cN, N>1 – T(N-1)=T(N-2)+c(N-1) – T(N-2)=T(N-3)+c(N-2) – ... – T(2)=T(1)+c(2)

  • adding up all these equations
  • T(N)=T(1)+c = O(N2)

 N i

i

2 Izmir University of Economics 15

slide-16
SLIDE 16

Best-Case Analysis

  • In the best case the pivot is in the middle.
  • T(N)=2T(N/2)+cN
  • Divide both sides of the equation by N

– T(N)/N=T(N/2)/(N/2)+c – T(N/2)/(N/2)=T(N/4)/(N/4)+c – ... – T(2)/(2)=T(1)/(1)+c

  • adding up all the equations
  • T(N)/N= T(1)/1+clogN
  • T(N)=cNlogN+N=O(NlogN)

Izmir University of Economics 16

slide-17
SLIDE 17

Average-Case Analysis

  • Assume each of the sizes for

S1is equally likely and hence probability is 1/N.

  • Subtract (2) from (1)
  • Divide both sides by N(N+1)

Izmir University of Economics

   

) 2 ( ) 1 ( ) ( 2 ) 1 ( ) 1 ( ) 1 ( ) ( 2 ) ( ) 1 ( ) ( 1 ) (

2 2 2 1 1

                  

  

     

N c i T N T N cN i T N NT cN i N T i T N N T

N i N i N i

cN N T N N NT c cN N T N T N N NT 2 ) 1 ( ) 1 ( ) ( 2 ) 1 ( 2 ) 1 ( ) 1 ( ) (            1 2 ) 1 ( 1 ) (      N c N N T N N T

  • Now we can telescope
  • adding all equations

3 2 2 ) 1 ( 3 ) 2 ( 1 2 2 ) 3 ( 1 ) 2 ( 2 1 ) 2 ( ) 1 ( 1 2 ) 1 ( 1 ) ( c T T N c N N T N N T N c N N T N N T N c N N T N N T                     ) log ( ) ( ) 2 3 ) 1 ( (log 2 2 ) 1 ( 1 ) ( 1 2 2 ) 1 ( 1 ) (

1 3

N N O N T N c T N N T i c T N N T

e N i

         

 

17

slide-18
SLIDE 18

Homework Assignments

  • 7.19, 7.20, 7.21, 7.22, 7.31, 7.44 (7.39 in

3/e), 7.45 (7.40 in 3/e), 7.54 (7.49 in 3/e), 7.55 (7.50 in 3/e)

  • You are requested to study and solve the
  • exercises. Note that these are for you to

practice only. You are not to deliver the results to me.

Izmir University of Economics 18