Mergesort
Mergesort (divide-and-conquer)
Dividearray into two halves.
Recursively sort each half.
Merge two halves to make sorted whole.
merge
sort
A L G O R I T H M S
divideA L G O R I T H M S
A G L O R H I M S T
A G H I L M O R S T
4.
auxiliary array
smallest smallest
AG L O R H I M S T
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
A
5.
auxiliary array
smallest smallest
AG L O R H I M S T
A
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
G
6.
auxiliary array
smallest smallest
AG L O R H I M S T
A G
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
H
7.
auxiliary array
smallest smallest
AG L O R H I M S T
A G H
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
I
8.
auxiliary array
smallest smallest
AG L O R H I M S T
A G H I
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
L
9.
auxiliary array
smallest smallest
AG L O R H I M S T
A G H I L
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
M
10.
auxiliary array
smallest smallest
AG L O R H I M S T
A G H I L M
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
O
11.
auxiliary array
smallest smallest
AG L O R H I M S T
A G H I L M O
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
R
12.
auxiliary array
first half
exhaustedsmallest
A G L O R H I M S T
A G H I L M O R
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
S
13.
auxiliary array
first half
exhaustedsmallest
A G L O R H I M S T
A G H I L M O R S
Merging
Merge.
Keep track of smallest element in each sorted half.
Insert smallest of two elements into auxiliary array.
Repeat until done.
T
What is radix?
Theradix or base is the number of unique digits,
including zero, that a positional numeral system
uses to represent numbers.
recall:
What is the radix of decimal number system?
What is the radix of binary number system?
What is aRadix?
The radix or base is the number of unique
digits, including zero, that a positional
numeral
system uses to represent numbers.
Examples:
1 BINARY 2
2 DECIMAL 10
3 OCTAL 8
4 HEXADECIMAL 16
20.
RADIX SORT
• non- comparative sorting algorithm (the
keys are not compared as such)
• sorts the keys by soring individual digits of
the keys which share the same significant
position and value
RADIX SORT -PSEUDO CODE
void radixsort()
{
max = A[0], exp = 1;
for (i = 0; i < n; i++) { if (A[i] > max) max = A[i]; }
while (max / exp > 0)
{
for (i=0; i<=9; i++) bucket [ i ] =0;
for (i = 0; i < n; i++) bucket [ A[i] / exp % 10 ]++;
for (i = 1; i <=9; i++) bucket [ i ] += bucket [i - 1];
for (i = n-1; i >= 0; i--) B [--bucket[A[i] / exp % 10]] =
A[i];
for (i = 0; i < n; i++) A [ i ] = B [ i ];
27.
Radix Sort
A[0], A[1],…, A[N-1] are strings
Very common in practice
Each string is:
cd-1cd-2…c1c0,
where c0, c1, …, cd-1 ∈{0, 1, …, M-1}
M = 128
Other example: decimal numbers
27
28.
The Magic ofRadixSort
Input list:
126, 328, 636, 341, 416, 131, 328
BucketSort on lower digit:
341, 131, 126, 636, 416, 328, 328
BucketSort result on next-higher digit:
416, 126, 328, 328, 131, 636, 341
BucketSort that result on highest digit:
126, 131, 328, 328, 341, 416, 636
28
Running time ofRadixsort
N items, d digit keys of max value M
How many passes?
How much work per pass?
Total time?
30
31.
Running time ofRadixsort
N items, d digit keys of max value M
How many passes? d
How much work per pass? N + M
just in case M>N, need to account for time to empty
out buckets between passes
Total time? O( d(N+M) )
31
How many squarescan you create in this figure by connecting any 4
dots (the corners of a square must lie upon a grid dot?
TRIANGLES:
How many triangles are located in the image below?
34.
There are 11squares total; 5 small, 4 medium, and 2 large.
27 triangles. There are 16 one-cell triangles, 7 four-cell triangles, 3 nine-cell
triangles, and 1 sixteen-cell triangle.