1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir...

18
1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics

Transcript of 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir...

Page 1: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

1

Sorting(Quicksort)

CS 201 Data Structures and Algorithms

Text: Read Weiss, § 7.7

Izmir University of Economics

Page 2: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

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 optimized inner loop.

• O(N2) worst-case performance, but this can be made exponentially unlikely with a little effort.

Izmir University of Economics

Page 3: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

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

Page 4: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

4

Picking the Pivot

Izmir University of Economics

Page 5: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

5

Picking the Pivot Median-of-Three Partitioning

• The median of a group of N numbers is the floor(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• left=8, right=0, center=6 (= floor((left+right)/2) )

Izmir University of Economics

Page 6: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

6

Partitioning Strategy

• First step: get the pivot element out of the way by swapping it with the last element. Middle element is 6, hence, the pivot.

• i starts at the first element and j starts at the next-to-last element.

Izmir University of Economics

8 1 4 9 6 3 5 2 7 0

leftpivot

centerright

Page 7: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

7

Partitioning Strategy – Example (1)

Izmir University of Economics

8 1 4 9 0 3 5 2 7 6

i j

8 1 4 9 0 3 5 2 7 6

i j

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.

Page 8: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

8Izmir University of Economics

Partitioning Strategy – Example (2)

2 1 4 9 0 3 5 8 7 6

i j

2 1 4 9 0 3 5 8 7 6

i j

2 1 4 5 0 3 9 8 7 6

i j

Aft

er f

irst

swap

Aft

er s

econ

d sw

ap

Page 9: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

9

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

Izmir University of Economics

Partitioning Strategy – Example (3)

2 1 4 5 0 3 9 8 7 6

j i

Bef

ore

third

sw

ap

2 1 4 5 0 3 6 8 7 9

i pivot

Aft

er s

wap

with

the

pivo

t

IMPORTANT: In case of equality both i and j will stop.

Page 10: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

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.

Page 11: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

11

Quicksort – Implementation (1)

Izmir University of Economics

typedef int ElementType;

void Swap( ElementType *Lhs, ElementType *Rhs ){ ElementType Tmp = *Lhs; *Lhs = *Rhs; *Rhs = Tmp;}

void Quicksort( ElementType A[ ], int N ){ Qsort( A, 0, N - 1 );}

Page 12: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

12Izmir University of Economics

Quicksort – Implementation (2)/* Return median of Left, Center, and Right *//* Order these and hide the pivot */

ElementType Median3( ElementType A[ ], int Left, int Right ){ int Center = ( Left + Right ) / 2;

if( A[ Left ] > A[ Center ] ) Swap( &A[ Left ], &A[ Center ] ); if( A[ Left ] > A[ Right ] ) Swap( &A[ Left ], &A[ Right ] ); if( A[ Center ] > A[ Right ] ) Swap( &A[ Center ], &A[ Right ] );

/* Invariant: A[ Left ] <= A[ Center ] <= A[ Right ] */

Swap( &A[ Center ], &A[ Right - 1 ] ); /* Hide pivot */ return A[ Right - 1 ]; /* Return pivot */}

Page 13: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

13Izmir University of Economics

Quicksort – Implementation (3)#define Cutoff ( 10 )void Qsort( ElementType A[ ], int Left, int Right ){ int i, j; ElementType Pivot;

if( Left + Cutoff <= Right ) { Pivot = Median3( A, Left, Right ); i = Left; j = Right - 1; for( ; ; ) { while( A[ ++i ] < Pivot ){ } while( A[ --j ] > Pivot ){ } if( i < j ) Swap( &A[ i ], &A[ j ] ); else break; } Swap( &A[ i ], &A[ Right - 1 ] ); /* Restore pivot */

Qsort( A, Left, i - 1 ); Qsort( A, i + 1, Right ); } else /* Do an insertion sort on the subarray */ InsertionSort( A + Left, Right - Left + 1 );}

Page 14: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

14Izmir University of Economics

Quicksort – Be Careful

This is not correct

i = Left + 1; j = Right - 2; for( ; ; ) { while( A[ i ] < Pivot ) i++; while( A[ j ] > Pivot ) j--; if( i < j ) Swap( &A[ i ], &A[ j ] ); else break; }

Page 15: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

Analysis of Quicksort

Izmir University of Economics 15

• 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.

Page 16: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

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

i2

Izmir University of Economics 16

Page 17: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

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 17

Page 18: 1 Sorting (Quicksort) CS 201 Data Structures and Algorithms Text: Read Weiss, § 7.7 Izmir University of Economics.

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

)(

22

0

21

0

1

0

NciTNTN

cNiTNNT

cNiNTiTN

NT

N

i

N

i

N

i

cNNTNNNT

ccNNTNTNNNT

2)1()1()(

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

1

2)1(

1

)(

N

c

N

NT

N

NT

• 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

)(

cTT

N

c

N

NT

N

NTN

c

N

NT

N

NTN

c

N

NT

N

NT

)log()(

)23)1((log2

2

)1(

1

)(

12

2

)1(

1

)( 1

3

NNONT

NcT

N

NT

ic

T

N

NT

e

N

i

18