mergesort, quicksort Oct. 6, 2017 1 Time complexity 2 2 - - PowerPoint PPT Presentation

β–Ά
mergesort quicksort
SMART_READER_LITE
LIVE PREVIEW

mergesort, quicksort Oct. 6, 2017 1 Time complexity 2 2 - - PowerPoint PPT Presentation

COMP 250 Lecture 13 mergesort, quicksort Oct. 6, 2017 1 Time complexity 2 2 convert to binary List operations: insertion/selection/ findMax, remove bubble sort grade school


slide-1
SLIDE 1

1

COMP 250

Lecture 13

mergesort, quicksort

  • Oct. 6, 2017
slide-2
SLIDE 2

Time complexity

2

𝑃 π‘œ

  • List operations:

findMax, remove

  • grade school addition
  • r subtraction
  • …..

𝑃 π‘œ2

  • insertion/selection/

bubble sort

  • grade school

multiplication

  • ……

𝑃 π‘šπ‘π‘•2 π‘œ

  • convert to binary
  • binary search
  • ……
slide-3
SLIDE 3

210 β‰ˆ 103 220 β‰ˆ 106 230 β‰ˆ 109

Computers perform ~109 operations per second.

slide-4
SLIDE 4

π‘œ

210 β‰ˆ 103 220 β‰ˆ 106 230 β‰ˆ 109

π‘œ2

106 1012 1018 π‘šπ‘π‘•2 π‘œ 10 20 30

Computers perform ~109 operations per second.

centuries second minutes…hours

slide-5
SLIDE 5

Better sorting algorithms ?

5

𝑃 π‘œ < ? < 𝑃( π‘œ2 )

slide-6
SLIDE 6

Mergesort

Given a list, partition it into two halves (1st & 2nd). Sort each half (recursively). Merge the two halves. This turns out to be much faster than the other list sorting algorithms we have seen.

6

slide-7
SLIDE 7

7

8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 7 16 2 5 4 13 1 3 6 8 10 11 2 4 5 7 13 16 1 2 3 4 5 6 7 8 10 11 13 16

mergesort mergesort merge partition

slide-8
SLIDE 8

8

mergesort(list){ if list.length == 1 return list else{ mid = (list.size - 1) / 2 list1 = list.getElements(0,mid) list2 = list.getElements(mid+1, list.size-1) list1 = mergesort(list1) list2 = mergesort(list2) return merge( list1, list2 ) } }

slide-9
SLIDE 9

9

mergesort(list){ if list.length == 1 return list else{ mid = (list.size - 1) / 2 list1 = list.getElements(0,mid) list2 = list.getElements(mid+1, list.size-1) list1 = mergesort(list1) list2 = mergesort(list2) return merge( list1, list2 ) } }

slide-10
SLIDE 10

10

mergesort(list){ if list.length == 1 return list else{ mid = (list.size - 1) / 2 list1 = list.getElements(0,mid) list2 = list.getElements(mid+1, list.size-1) list1 = mergesort(list1) list2 = mergesort(list2) return merge( list1, list2 ) } }

slide-11
SLIDE 11

11

8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 7 16 2 5 4 13 1 3 6 8 10 11 2 4 5 7 13 16 1 2 3 4 5 6 7 8 10 11 13 16

mergesort mergesort merge partition

slide-12
SLIDE 12

12

1 3 6 8 10 11 2 4 5 7 13 16

merge

head1 head2

slide-13
SLIDE 13

13

1 3 6 8 10 11 2 4 5 7 13 16 1

merge

head1 head2

slide-14
SLIDE 14

14

1 3 6 8 10 11 2 4 5 7 13 16 1 2

merge

head1 head2

slide-15
SLIDE 15

15

1 3 6 8 10 11 2 4 5 7 13 16 1 2 3

merge

head1 head2

slide-16
SLIDE 16

16

1 3 6 8 10 11 2 4 5 7 13 16 1 2 3 4

merge

head1 head2

slide-17
SLIDE 17

17

1 3 6 8 10 11 2 4 5 7 13 16 1 2 3 4 5

merge

head1 head2

slide-18
SLIDE 18

18

1 3 6 8 10 11 2 4 5 7 13 16 1 2 3 4 5 6

merge

head1 head2

slide-19
SLIDE 19

19

1 3 6 8 10 11 2 4 5 7 13 16 1 2 3 4 5 6 7

merge

head1 head2

slide-20
SLIDE 20

20

1 3 6 8 10 11 2 4 5 7 13 16 1 2 3 4 5 6 7 8 10 11

merge

head2

….and so on until

  • ne list is empty.
slide-21
SLIDE 21

21

1 3 6 8 10 11 2 4 5 7 13 16 1 2 3 4 5 6 7 8 10 11 13 16

merge

Then, copy the remaining elements.

slide-22
SLIDE 22

22

merge( list1, list2){ initialize list to be empty while (list1 is not empty) & (list2 is not empty){ if (list1.first < list2.first) list.addlast( list1.removeFirst(list1) ) else list.addlast( list2.removeFirst(list2) ) } while list1 is not empty list.addlast( list1.removeFirst(list1) ) while list2 is not empty list.addlast( list2.removeFirst(list2) ) return list }

slide-23
SLIDE 23

23

merge( list1, list2){ initialize list to be empty while (list1 is not empty) & (list2 is not empty){ if (list1.first < list2.first) list.addlast( list1.removeFirst(list1) ) else list.addlast( list2.removeFirst(list2) ) } while list1 is not empty list.addlast( list1.removeFirst(list1) ) while list2 is not empty list.addlast( list2.removeFirst(list2) ) return list }

slide-24
SLIDE 24

8 10 3 11 6 1 7 16 2 5 4 13

mergesort(list){ if list.length == 1 return list else{ mid = (list.size - 1) / 2 list1 = list.getElements(0,mid) list2 = list.getElements(mid+1, list.size-1) list1 = mergesort(list1) list2 = mergesort(list2) return merge( list1, list2 ) } }

slide-25
SLIDE 25

8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 7 16 2 5 4 13 list1 list2

slide-26
SLIDE 26

8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 list1 list2

slide-27
SLIDE 27

8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 8 10 3 list1 list2

slide-28
SLIDE 28

28

8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 8 10 3 8 10 8 10 list1 list2

slide-29
SLIDE 29

8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 8 10 3 8 10 8 10 3 8 10 list1 list2

slide-30
SLIDE 30

8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 8 10 3 11 6 1 8 11 10 6 6 11 8 10 1 6 11 3 8 10 list1 list2

slide-31
SLIDE 31

8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 8 10 3 11 6 1 8 11 10 6 6 11 8 10 1 6 11 3 8 10 1 3 6 8 10 11 list1 list2

slide-32
SLIDE 32

32

8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 5 4 7 16 2 8 10 3 11 6 1 7 16 2 5 4 13 13 5 8 11 7 10 6 16 4 4 5 7 16 6 11 8 10 1 6 11 3 8 10 2 7 16 4 5 13 1 3 6 8 10 11 2 4 5 7 13 16 list1 list2

slide-33
SLIDE 33

33

8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 7 16 2 5 4 13 8 10 3 11 6 1 5 4 7 16 2 8 10 3 11 6 1 7 16 2 5 4 13 13 5 8 11 7 10 6 16 4 4 5 7 16 6 11 8 10 1 6 11 3 8 10 2 7 16 4 5 13 1 3 6 8 10 11 2 4 5 7 13 16 1 2 3 4 5 6 7 8 10 11 13 16

slide-34
SLIDE 34

34

π‘šπ‘π‘•2π‘œ

π‘œ

Q: How many

  • perations are

required to mergesort a list of size π‘œ ? A: 𝑃( π‘œ π‘šπ‘π‘•2 π‘œ ) This will become more clear a few lectures from now when we discuss recurrences. π‘šπ‘π‘•2π‘œ

π‘œ

slide-35
SLIDE 35

π‘œ

210 β‰ˆ 103 220 β‰ˆ 106 230 β‰ˆ 109

π‘œ2

106 1012 1018 π‘šπ‘π‘•2 π‘œ 10 20 30 𝒐 π’Žπ’‘π’‰πŸ‘ 𝒐 πŸπŸπŸ“ ~πŸπŸπŸ– ~𝟐𝟏𝟐𝟏

π‘œ π‘šπ‘π‘•2 π‘œ is much closer to π‘œ than to π‘œ2

slide-36
SLIDE 36

π‘œ

210 β‰ˆ 103 220 β‰ˆ 106 230 β‰ˆ 109

π‘œ2

106 1012 1018 π‘šπ‘π‘•2 π‘œ 10 20 30

Computers perform ~109 operations per second.

milliseconds minutes hours centuries

𝒐 π’Žπ’‘π’‰πŸ‘ 𝒐 πŸπŸπŸ“ ~πŸπŸπŸ– ~𝟐𝟏𝟐𝟏

slide-37
SLIDE 37

37

mergesort quicksort

𝑃 π‘œ < 𝑃( π‘œ π‘šπ‘π‘•2 π‘œ) β‰ͺ 𝑃( π‘œ2 )

bubble sort selection sort insertion sort

slide-38
SLIDE 38

Quicksort

38

quicksort(list){ if list.length <= 1 return list else{ pivot = list.removeFirst() // or some other element list1 = list.getElementsLessThan(pivot) list2 = list.getElementsGreaterOrEqual(pivot) list1 = quicksort(list1) list2 = quicksort(list2) } return concatenate( list1, e, list2 ) }

slide-39
SLIDE 39

Quicksort

39

quicksort(list){ if list.length <= 1 return list else{ pivot = list.removeFirst() // or some other element list1 = list.getElementsLessThan(pivot) list2 = list.getElementsGreaterOrEqual(pivot) list1 = quicksort(list1) list2 = quicksort(list2) } return concatenate( list1, e, list2 ) }

slide-40
SLIDE 40

40

8 10 3 11 6 1 7 16 2 5 4 13 3 6 1 7 2 5 4 10 11 16 13 1 2 3 4 5 6 7 8 10 11 13 16

quicksort quicksort partition

1 2 3 4 5 6 7 10 11 13 16

concatenate pivot

slide-41
SLIDE 41

Quicksort can be done β€œin place”

41

quicksort( low, high ){ if low > high return else{ pivot = ___; // select index in {low, …, high} partitionIndex = makePartition (low, high, pivot) quicksort(low, partitionIndex - 1) quicksort(partionIndex + 1, high) } }

Quicksort partitioning can be done β€˜in place’ using a clever swapping and scanning technique. (See web for details, if interested.)

slide-42
SLIDE 42

42

8 10 3 11 6 1 7 16 2 5 4 13 3 6 1 7 2 5 4 8 10 11 16 13

quicksort quicksort partition

1 2 3 4 5 6 7 8 10 11 13 16

pivot

slide-43
SLIDE 43

Mergesort vs. Quicksort

  • Mergesort typically uses an extra list. More space can

hurt performance for big lists.

  • We will discuss worst case performance of quicksort

later in the course.

  • See stackoverflow if you want opinions on which is

better.

43