Sorting Chapter 9 1. Objectives Selection Sort, Insertion Sort, Quick Sort, and Merge Sort. 2.
-
Upload
percival-gibson -
Category
Documents
-
view
265 -
download
2
Transcript of Sorting Chapter 9 1. Objectives Selection Sort, Insertion Sort, Quick Sort, and Merge Sort. 2.
1
Sorting
Chapter 9
2
Objectives
bull Selection Sort bull Insertion Sortbull Quick Sort and bull Merge Sort
3
Introductionbull Common problem sort a list of values starting
from lowest to highestndash Telephone directoryndash Words of dictionary in alphabetical orderndash Students names listed alphabetically
bull Choose a criteria which is used to order databull Given a list of records that have keys we use
these keys to define an ordering of the items in the list
4
Example 1
5
The Insertion Sort Algorithm
bull Given an array of integersbull The Insertion Sort algorithm views the array as
having a sorted side and an unsorted sidebull The sorted side starts with just the first
element which is not necessarily the smallest element
bull The sorted side grows by taking the front element from the unsorted side
6
7
Example 2
8
Example 3
9
Example 4
10
The Selection Sort Algorithm
bull Start by finding the smallest elementbull Swap the smallest entry with the first elementbull Part of the array is sortedbull Find the smallest element in the unsorted sidebull Swap with the front of the unsorted sidebull The size of the sorted side is increased by one
elementbull Continue until the unsorted side has just one
number Why
11
The Selection Sort Algorithm(cont)
12
Example 1
13
Example 2
14
Quick sort Algorithm
bull Given an array of n elements (eg integers)bull 1048708 If array only contains one element returnbull 1048708 Else
bull pick one element to use as pivotbull Partition elements into two sub-arraysndashElements less than or equal to pivotndashElements greater than pivot
bull Quick sort two sub-arraysbull Return results
15
Pick
bull 6 5 3 1 8 7 2 4bull 6 5 3 1 8 7 2 4bull 2 5 3 1 8 7 6 4bull 2 1 3 5 8 7 6 4bull 1 2 3 5 8 7 6 4bull 1 2 3 4 8 7 6 5bull 1 2 3 4 5 6 7 8
16
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
2
Objectives
bull Selection Sort bull Insertion Sortbull Quick Sort and bull Merge Sort
3
Introductionbull Common problem sort a list of values starting
from lowest to highestndash Telephone directoryndash Words of dictionary in alphabetical orderndash Students names listed alphabetically
bull Choose a criteria which is used to order databull Given a list of records that have keys we use
these keys to define an ordering of the items in the list
4
Example 1
5
The Insertion Sort Algorithm
bull Given an array of integersbull The Insertion Sort algorithm views the array as
having a sorted side and an unsorted sidebull The sorted side starts with just the first
element which is not necessarily the smallest element
bull The sorted side grows by taking the front element from the unsorted side
6
7
Example 2
8
Example 3
9
Example 4
10
The Selection Sort Algorithm
bull Start by finding the smallest elementbull Swap the smallest entry with the first elementbull Part of the array is sortedbull Find the smallest element in the unsorted sidebull Swap with the front of the unsorted sidebull The size of the sorted side is increased by one
elementbull Continue until the unsorted side has just one
number Why
11
The Selection Sort Algorithm(cont)
12
Example 1
13
Example 2
14
Quick sort Algorithm
bull Given an array of n elements (eg integers)bull 1048708 If array only contains one element returnbull 1048708 Else
bull pick one element to use as pivotbull Partition elements into two sub-arraysndashElements less than or equal to pivotndashElements greater than pivot
bull Quick sort two sub-arraysbull Return results
15
Pick
bull 6 5 3 1 8 7 2 4bull 6 5 3 1 8 7 2 4bull 2 5 3 1 8 7 6 4bull 2 1 3 5 8 7 6 4bull 1 2 3 5 8 7 6 4bull 1 2 3 4 8 7 6 5bull 1 2 3 4 5 6 7 8
16
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
3
Introductionbull Common problem sort a list of values starting
from lowest to highestndash Telephone directoryndash Words of dictionary in alphabetical orderndash Students names listed alphabetically
bull Choose a criteria which is used to order databull Given a list of records that have keys we use
these keys to define an ordering of the items in the list
4
Example 1
5
The Insertion Sort Algorithm
bull Given an array of integersbull The Insertion Sort algorithm views the array as
having a sorted side and an unsorted sidebull The sorted side starts with just the first
element which is not necessarily the smallest element
bull The sorted side grows by taking the front element from the unsorted side
6
7
Example 2
8
Example 3
9
Example 4
10
The Selection Sort Algorithm
bull Start by finding the smallest elementbull Swap the smallest entry with the first elementbull Part of the array is sortedbull Find the smallest element in the unsorted sidebull Swap with the front of the unsorted sidebull The size of the sorted side is increased by one
elementbull Continue until the unsorted side has just one
number Why
11
The Selection Sort Algorithm(cont)
12
Example 1
13
Example 2
14
Quick sort Algorithm
bull Given an array of n elements (eg integers)bull 1048708 If array only contains one element returnbull 1048708 Else
bull pick one element to use as pivotbull Partition elements into two sub-arraysndashElements less than or equal to pivotndashElements greater than pivot
bull Quick sort two sub-arraysbull Return results
15
Pick
bull 6 5 3 1 8 7 2 4bull 6 5 3 1 8 7 2 4bull 2 5 3 1 8 7 6 4bull 2 1 3 5 8 7 6 4bull 1 2 3 5 8 7 6 4bull 1 2 3 4 8 7 6 5bull 1 2 3 4 5 6 7 8
16
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
4
Example 1
5
The Insertion Sort Algorithm
bull Given an array of integersbull The Insertion Sort algorithm views the array as
having a sorted side and an unsorted sidebull The sorted side starts with just the first
element which is not necessarily the smallest element
bull The sorted side grows by taking the front element from the unsorted side
6
7
Example 2
8
Example 3
9
Example 4
10
The Selection Sort Algorithm
bull Start by finding the smallest elementbull Swap the smallest entry with the first elementbull Part of the array is sortedbull Find the smallest element in the unsorted sidebull Swap with the front of the unsorted sidebull The size of the sorted side is increased by one
elementbull Continue until the unsorted side has just one
number Why
11
The Selection Sort Algorithm(cont)
12
Example 1
13
Example 2
14
Quick sort Algorithm
bull Given an array of n elements (eg integers)bull 1048708 If array only contains one element returnbull 1048708 Else
bull pick one element to use as pivotbull Partition elements into two sub-arraysndashElements less than or equal to pivotndashElements greater than pivot
bull Quick sort two sub-arraysbull Return results
15
Pick
bull 6 5 3 1 8 7 2 4bull 6 5 3 1 8 7 2 4bull 2 5 3 1 8 7 6 4bull 2 1 3 5 8 7 6 4bull 1 2 3 5 8 7 6 4bull 1 2 3 4 8 7 6 5bull 1 2 3 4 5 6 7 8
16
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
5
The Insertion Sort Algorithm
bull Given an array of integersbull The Insertion Sort algorithm views the array as
having a sorted side and an unsorted sidebull The sorted side starts with just the first
element which is not necessarily the smallest element
bull The sorted side grows by taking the front element from the unsorted side
6
7
Example 2
8
Example 3
9
Example 4
10
The Selection Sort Algorithm
bull Start by finding the smallest elementbull Swap the smallest entry with the first elementbull Part of the array is sortedbull Find the smallest element in the unsorted sidebull Swap with the front of the unsorted sidebull The size of the sorted side is increased by one
elementbull Continue until the unsorted side has just one
number Why
11
The Selection Sort Algorithm(cont)
12
Example 1
13
Example 2
14
Quick sort Algorithm
bull Given an array of n elements (eg integers)bull 1048708 If array only contains one element returnbull 1048708 Else
bull pick one element to use as pivotbull Partition elements into two sub-arraysndashElements less than or equal to pivotndashElements greater than pivot
bull Quick sort two sub-arraysbull Return results
15
Pick
bull 6 5 3 1 8 7 2 4bull 6 5 3 1 8 7 2 4bull 2 5 3 1 8 7 6 4bull 2 1 3 5 8 7 6 4bull 1 2 3 5 8 7 6 4bull 1 2 3 4 8 7 6 5bull 1 2 3 4 5 6 7 8
16
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
6
7
Example 2
8
Example 3
9
Example 4
10
The Selection Sort Algorithm
bull Start by finding the smallest elementbull Swap the smallest entry with the first elementbull Part of the array is sortedbull Find the smallest element in the unsorted sidebull Swap with the front of the unsorted sidebull The size of the sorted side is increased by one
elementbull Continue until the unsorted side has just one
number Why
11
The Selection Sort Algorithm(cont)
12
Example 1
13
Example 2
14
Quick sort Algorithm
bull Given an array of n elements (eg integers)bull 1048708 If array only contains one element returnbull 1048708 Else
bull pick one element to use as pivotbull Partition elements into two sub-arraysndashElements less than or equal to pivotndashElements greater than pivot
bull Quick sort two sub-arraysbull Return results
15
Pick
bull 6 5 3 1 8 7 2 4bull 6 5 3 1 8 7 2 4bull 2 5 3 1 8 7 6 4bull 2 1 3 5 8 7 6 4bull 1 2 3 5 8 7 6 4bull 1 2 3 4 8 7 6 5bull 1 2 3 4 5 6 7 8
16
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
7
Example 2
8
Example 3
9
Example 4
10
The Selection Sort Algorithm
bull Start by finding the smallest elementbull Swap the smallest entry with the first elementbull Part of the array is sortedbull Find the smallest element in the unsorted sidebull Swap with the front of the unsorted sidebull The size of the sorted side is increased by one
elementbull Continue until the unsorted side has just one
number Why
11
The Selection Sort Algorithm(cont)
12
Example 1
13
Example 2
14
Quick sort Algorithm
bull Given an array of n elements (eg integers)bull 1048708 If array only contains one element returnbull 1048708 Else
bull pick one element to use as pivotbull Partition elements into two sub-arraysndashElements less than or equal to pivotndashElements greater than pivot
bull Quick sort two sub-arraysbull Return results
15
Pick
bull 6 5 3 1 8 7 2 4bull 6 5 3 1 8 7 2 4bull 2 5 3 1 8 7 6 4bull 2 1 3 5 8 7 6 4bull 1 2 3 5 8 7 6 4bull 1 2 3 4 8 7 6 5bull 1 2 3 4 5 6 7 8
16
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
8
Example 3
9
Example 4
10
The Selection Sort Algorithm
bull Start by finding the smallest elementbull Swap the smallest entry with the first elementbull Part of the array is sortedbull Find the smallest element in the unsorted sidebull Swap with the front of the unsorted sidebull The size of the sorted side is increased by one
elementbull Continue until the unsorted side has just one
number Why
11
The Selection Sort Algorithm(cont)
12
Example 1
13
Example 2
14
Quick sort Algorithm
bull Given an array of n elements (eg integers)bull 1048708 If array only contains one element returnbull 1048708 Else
bull pick one element to use as pivotbull Partition elements into two sub-arraysndashElements less than or equal to pivotndashElements greater than pivot
bull Quick sort two sub-arraysbull Return results
15
Pick
bull 6 5 3 1 8 7 2 4bull 6 5 3 1 8 7 2 4bull 2 5 3 1 8 7 6 4bull 2 1 3 5 8 7 6 4bull 1 2 3 5 8 7 6 4bull 1 2 3 4 8 7 6 5bull 1 2 3 4 5 6 7 8
16
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
9
Example 4
10
The Selection Sort Algorithm
bull Start by finding the smallest elementbull Swap the smallest entry with the first elementbull Part of the array is sortedbull Find the smallest element in the unsorted sidebull Swap with the front of the unsorted sidebull The size of the sorted side is increased by one
elementbull Continue until the unsorted side has just one
number Why
11
The Selection Sort Algorithm(cont)
12
Example 1
13
Example 2
14
Quick sort Algorithm
bull Given an array of n elements (eg integers)bull 1048708 If array only contains one element returnbull 1048708 Else
bull pick one element to use as pivotbull Partition elements into two sub-arraysndashElements less than or equal to pivotndashElements greater than pivot
bull Quick sort two sub-arraysbull Return results
15
Pick
bull 6 5 3 1 8 7 2 4bull 6 5 3 1 8 7 2 4bull 2 5 3 1 8 7 6 4bull 2 1 3 5 8 7 6 4bull 1 2 3 5 8 7 6 4bull 1 2 3 4 8 7 6 5bull 1 2 3 4 5 6 7 8
16
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
10
The Selection Sort Algorithm
bull Start by finding the smallest elementbull Swap the smallest entry with the first elementbull Part of the array is sortedbull Find the smallest element in the unsorted sidebull Swap with the front of the unsorted sidebull The size of the sorted side is increased by one
elementbull Continue until the unsorted side has just one
number Why
11
The Selection Sort Algorithm(cont)
12
Example 1
13
Example 2
14
Quick sort Algorithm
bull Given an array of n elements (eg integers)bull 1048708 If array only contains one element returnbull 1048708 Else
bull pick one element to use as pivotbull Partition elements into two sub-arraysndashElements less than or equal to pivotndashElements greater than pivot
bull Quick sort two sub-arraysbull Return results
15
Pick
bull 6 5 3 1 8 7 2 4bull 6 5 3 1 8 7 2 4bull 2 5 3 1 8 7 6 4bull 2 1 3 5 8 7 6 4bull 1 2 3 5 8 7 6 4bull 1 2 3 4 8 7 6 5bull 1 2 3 4 5 6 7 8
16
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
11
The Selection Sort Algorithm(cont)
12
Example 1
13
Example 2
14
Quick sort Algorithm
bull Given an array of n elements (eg integers)bull 1048708 If array only contains one element returnbull 1048708 Else
bull pick one element to use as pivotbull Partition elements into two sub-arraysndashElements less than or equal to pivotndashElements greater than pivot
bull Quick sort two sub-arraysbull Return results
15
Pick
bull 6 5 3 1 8 7 2 4bull 6 5 3 1 8 7 2 4bull 2 5 3 1 8 7 6 4bull 2 1 3 5 8 7 6 4bull 1 2 3 5 8 7 6 4bull 1 2 3 4 8 7 6 5bull 1 2 3 4 5 6 7 8
16
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
12
Example 1
13
Example 2
14
Quick sort Algorithm
bull Given an array of n elements (eg integers)bull 1048708 If array only contains one element returnbull 1048708 Else
bull pick one element to use as pivotbull Partition elements into two sub-arraysndashElements less than or equal to pivotndashElements greater than pivot
bull Quick sort two sub-arraysbull Return results
15
Pick
bull 6 5 3 1 8 7 2 4bull 6 5 3 1 8 7 2 4bull 2 5 3 1 8 7 6 4bull 2 1 3 5 8 7 6 4bull 1 2 3 5 8 7 6 4bull 1 2 3 4 8 7 6 5bull 1 2 3 4 5 6 7 8
16
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
13
Example 2
14
Quick sort Algorithm
bull Given an array of n elements (eg integers)bull 1048708 If array only contains one element returnbull 1048708 Else
bull pick one element to use as pivotbull Partition elements into two sub-arraysndashElements less than or equal to pivotndashElements greater than pivot
bull Quick sort two sub-arraysbull Return results
15
Pick
bull 6 5 3 1 8 7 2 4bull 6 5 3 1 8 7 2 4bull 2 5 3 1 8 7 6 4bull 2 1 3 5 8 7 6 4bull 1 2 3 5 8 7 6 4bull 1 2 3 4 8 7 6 5bull 1 2 3 4 5 6 7 8
16
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
14
Quick sort Algorithm
bull Given an array of n elements (eg integers)bull 1048708 If array only contains one element returnbull 1048708 Else
bull pick one element to use as pivotbull Partition elements into two sub-arraysndashElements less than or equal to pivotndashElements greater than pivot
bull Quick sort two sub-arraysbull Return results
15
Pick
bull 6 5 3 1 8 7 2 4bull 6 5 3 1 8 7 2 4bull 2 5 3 1 8 7 6 4bull 2 1 3 5 8 7 6 4bull 1 2 3 5 8 7 6 4bull 1 2 3 4 8 7 6 5bull 1 2 3 4 5 6 7 8
16
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
15
Pick
bull 6 5 3 1 8 7 2 4bull 6 5 3 1 8 7 2 4bull 2 5 3 1 8 7 6 4bull 2 1 3 5 8 7 6 4bull 1 2 3 5 8 7 6 4bull 1 2 3 4 8 7 6 5bull 1 2 3 4 5 6 7 8
16
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
16
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
17
Total comparisons
bull Assume that keys are random uniformly distributedndash Best case running time O(n log2n)ndash Worst case running time O(n2)
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
18
Quick sort Worst Case
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
Partitioning Array
Given a pivot partition the elements of the array such that the resulting array consists of
1 One sub-array that contains elements gt= pivot 2 Another sub-array that contains elements lt pivot
The sub-arrays are stored in the original data array
Partitioning loops through swapping elements belowabove pivot
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
40 20 10 80 60 50 7 30 100pivot_index = 0
[0] [1] [2] [3] [4] [5] [6] [7] [8]
too_big_index too_small_index
Example 1
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
40 20 10 80 60 50 7 30 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
40 20 10 30 60 50 7 80 100pivot_index = 0
too_big_index too_small_index
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 1
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
40 20 10 30 7 50 60 80 100pivot_index = 0
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
1 While data[too_big_index] lt= data[pivot]++too_big_index
2 While data[too_small_index] gt data[pivot]--too_small_index
3 If too_big_index lt too_small_indexswap data[too_big_index] and data[too_small_index]
4 While too_small_index gt too_big_index go to 15 Swap data[too_small_index] and data[pivot_index]
7 20 10 30 40 50 60 80 100pivot_index = 4
too_big_index too_small_index
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
Partition Result
7 20 10 30 40 50 60 80 100
lt= data[pivot] gt data[pivot]
[0] [1] [2] [3] [4] [5] [6] [7] [8]
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
Recursion Quicksort Sub-arrays
7 20 10 30 40 50 60 80 100
[0] [1] [2] [3] [4] [5] [6] [7] [8]
lt= data[pivot] gt data[pivot]
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
47
Merge Sort
bull Problem Given n elements sort elementsbull into non-decreasing orderndash Apply divide-and-conquer to sorting problembull If n=1 terminate (every one-element list is already
sorted)bull If ngt1 partition elements into two sub-arrays sort
each combine into a single sorted array
bull How do we partition
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
Example 1
bull Partition into lists of size n2
[10 4 6 3]
[10 4 6 3 8 2 5 7]
[8 2 5 7]
[10 4] [6 3] [8 2] [5 7]
[4] [10] [3][6] [2][8] [5][7]
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
Example Contrsquod
bull Merge
[3 4 6 10]
[2 3 4 5 6 7 8 10 ]
[2 5 7 8]
[4 10] [3 6] [2 8] [5 7]
[4] [10] [3][6] [2][8] [5][7]
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-
50
Example 2
6 5 3 1 8 7 2 46 5 3 1 8 7 2 4
6 5 3 1 8 7 2 45 6 1 3 7 8 2 4
1 3 5 6 2 4 7 81 2 3 4 5 6 7 81 2 3 4 5 6 7 8
- Sorting
- Objectives
- Introduction
- Example 1
- The Insertion Sort Algorithm
- Slide 6
- Slide 7
- Slide 8
- Slide 9
- The Selection Sort Algorithm
- The Selection Sort Algorithm (cont)
- Example 1 (2)
- Example 2
- Quick sort Algorithm
- Pick
- Slide 16
- Total comparisons
- Quick sort Worst Case
- Partitioning Array
- Example 1
- Slide 21
- Slide 22
- Slide 23
- Slide 24
- Slide 25
- Slide 26
- Slide 27
- Slide 28
- Slide 29
- Slide 30
- Slide 31
- Slide 32
- Slide 33
- Slide 34
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Slide 39
- Slide 40
- Slide 41
- Slide 42
- Slide 43
- Slide 44
- Partition Result
- Recursion Quicksort Sub-arrays
- Merge Sort
- Example 1 (3)
- Example Contrsquod
- Example 2
-