lecture 13 - mcgill cimlanger/250/13-mergesort-slides.pdf · 2017-10-06 · 210 ≈103 220≈106...
TRANSCRIPT
1
COMP 250
Lecture 13
mergesort, quicksort
Oct. 6, 2017
Time complexity
2
𝑂 𝑛• List operations:
findMax, remove
• grade school addition or subtraction
• …..
𝑂 𝑛2
• insertion/selection/bubble sort
• grade school multiplication
• ……
𝑂 𝑙𝑜𝑔2 𝑛• convert to binary
• binary search
• ……
210 ≈ 103
220 ≈ 106
230 ≈ 109
Computers perform ~109 operations per second.
𝑛
210 ≈ 103
220 ≈ 106
230 ≈ 109
𝑛2
106
1012
1018
𝑙𝑜𝑔2 𝑛
10
20
30
Computers perform ~109 operations per second.
centuriessecond
minutes…hours
Better sorting algorithms ?
5
𝑂 𝑛 < ? < 𝑂( 𝑛2 )
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
7
810
311
617
16254
13
810
311
61
716
254
13
1368
1011
2457
1316
12345678
10111316
mergesort
mergesort
mergepartition
8
mergesort(list){if list.length == 1
return listelse{
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 )
}}
9
mergesort(list){if list.length == 1
return listelse{
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 )
}}
10
mergesort(list){if list.length == 1
return listelse{
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 )
}}
11
810
311
617
16254
13
810
311
61
716
254
13
1368
1011
2457
1316
12345678
10111316
mergesort
mergesort
mergepartition
12
1368
1011
2457
1316
merge
head1
head2
13
1368
1011
2457
1316
1
merge
head1
head2
14
1368
1011
2457
1316
12
merge
head1
head2
15
1368
1011
2457
1316
123
merge
head1
head2
16
1368
1011
2457
1316
1234
merge
head1
head2
17
1368
1011
2457
1316
12345
merge
head1
head2
18
1368
1011
2457
1316
123456
merge
head1
head2
19
1368
1011
2457
1316
1234567merge
head1
head2
20
1368
1011
2457
1316
123456781011
merge
head2
….and so on until one list is empty.
21
1368
1011
2457
1316
1234567810111316
merge
Then, copy the remaining elements.
22
merge( list1, list2){initialize list to be emptywhile (list1 is not empty) & (list2 is not empty){
if (list1.first < list2.first)list.addlast( list1.removeFirst(list1) )
elselist.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}
23
merge( list1, list2){initialize list to be emptywhile (list1 is not empty) & (list2 is not empty){
if (list1.first < list2.first)list.addlast( list1.removeFirst(list1) )
elselist.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}
810
311
617
16254
13
mergesort(list){if list.length == 1
return listelse{
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 )
}}
810
311
617
16254
13
810
311
61
716
254
13
list1
list2
810
311
617
16254
13
810
311
61
716
254
13
810
3
1161
list1
list2
810
311
617
16254
13
810
311
61
716
254
13
810
3
1161
810
3
list1
list2
28
810
311
617
16254
13
810
311
61
716
254
13
810
3
1161
810
3
8
10
810
list1
list2
810
311
617
16254
13
810
311
61
716
254
13
810
3
1161
810
3
8
10
810 3
810
list1
list2
810
311
617
16254
13
810
311
61
716
254
13
810
3
1161
810
3
116
1
8
11
10
6
611
810
16
11
38
10list1
list2
810
311
617
16254
13
810
311
61
716
254
13
810
3
1161
810
3
116
1
8
11
10
6
611
810
16
11
38
10
1368
1011
list1
list2
32
810
311
617
16254
13
810
311
61
716
254
13
810
3
1161
54
716
2
810
3
116
1
716
2
54
1313
5
8
11
7
10
6
16
445
716
611
810
16
11
38
10
27
16
45
13
1368
1011
2457
1316
list1
list2
33
810
311
617
16254
13
810
311
61
716
254
13
810
3
1161
54
716
2
810
3
116
1
716
2
54
1313
5
8
11
7
10
6
16
445
716
611
810
16
11
38
10
27
16
45
13
1368
1011
2457
1316
1234567810111316
34
𝑙𝑜𝑔2𝑛
𝑛
Q: How many operations are required to mergesorta list of size 𝑛 ?
A: 𝑂( 𝑛 𝑙𝑜𝑔2 𝑛 )
This will become more clear a few lectures from now when we discuss recurrences.
𝑙𝑜𝑔2𝑛
𝑛
𝑛
210 ≈ 103
220 ≈ 106
230 ≈ 109
𝑛2
106
1012
1018
𝑙𝑜𝑔2 𝑛
10
20
30
𝒏 𝒍𝒐𝒈𝟐 𝒏
𝟏𝟎𝟒
~𝟏𝟎𝟕
~𝟏𝟎𝟏𝟎
𝑛 𝑙𝑜𝑔2 𝑛 is much closer to 𝑛than to 𝑛2
𝑛
210 ≈ 103
220 ≈ 106
230 ≈ 109
𝑛2
106
1012
1018
𝑙𝑜𝑔2 𝑛
10
20
30
Computers perform ~109 operations per second.
milliseconds minutes hours centuries
𝒏 𝒍𝒐𝒈𝟐 𝒏
𝟏𝟎𝟒
~𝟏𝟎𝟕
~𝟏𝟎𝟏𝟎
37
mergesortquicksort
𝑂 𝑛 < 𝑂( 𝑛 𝑙𝑜𝑔2 𝑛) ≪ 𝑂( 𝑛2 )
bubble sortselection sortinsertion sort
Quicksort
38
quicksort(list){if list.length <= 1
return listelse{
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 )
}
Quicksort
39
quicksort(list){if list.length <= 1
return listelse{
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 )
}
40
810
311
617
16254
13
3617254
10111613
12345678
10111316
quicksort
quicksort
partition
1234567
10111316
concatenate
pivot
Quicksort can be done “in place”
41
quicksort( low, high ){if low > high
returnelse{
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.)
42
810
311
617
16254
13
36172548
10111613
quicksort
quicksort
partition
12345678
10111316
pivot
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