3.5 merge sort

39
Mergesort Mergesort (divide-and-conquer) Divide array into two halves. A L G O R I T H M S divide A L G O R I T H M S

Transcript of 3.5 merge sort

Mergesort

Mergesort (divide-and-conquer) Divide array into two halves.

A L G O R I T H M S

divideA L G O R I T H M S

Mergesort

Mergesort (divide-and-conquer) Divide array into two halves. Recursively sort each half.

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

Mergesort

Mergesort (divide-and-conquer) Divide array 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

auxiliary array

smallest smallest

A G 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

auxiliary array

smallest smallest

A G L O R H I M S T

A

MergingMerge. Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

G

auxiliary array

smallest smallest

A G L O R H I M S T

A G

MergingMerge. Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

H

auxiliary array

smallest smallest

A G 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

auxiliary array

smallest smallest

A G 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

auxiliary array

smallest smallest

A G 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

auxiliary array

smallest smallest

A G 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

auxiliary array

smallest smallest

A G 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

auxiliary array

first halfexhausted smallest

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

auxiliary array

first halfexhausted smallest

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

Do by yourself

10,3,54,23,25,5,75,1 10,5,7,6,1,4,8,3,2,9

What is radix?

The radix 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?

RADIX SORT

RadixSort

•Radix = “The base of a number system”

•Idea: Bucket Sort on each digit, bottom up.

17

What is a Radix?

What is a Radix?

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

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

EXAMPLE

INPUT Pass 1 Pass 2 Pass 3 Pass 4

3746 5743 5743 2653 1999

5743 2653 3746 4657 2653

4657 3746 2653 5743 3746

2653 4657 4657 3746 4657

1999 1999 1999 1999 5743

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 ];

exp *= 10;

}

}

Radix Sort

A[0], A[1], …, A[N-1] are stringsVery 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

The Magic of RadixSort

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

Radix Sort 29

35 53 55 33 52 32 25

Q[0] Q[1] Q[2] Q[3] Q[4] Q[5] Q[6] Q[7] Q[8] Q[9]

53 33 35 55 25

52 32 53 33 35 55 25

52 32

Q[0] Q[1] Q[2] Q[3] Q[4] Q[5] Q[6] Q[7] Q[8] Q[9]

32 33 3525 52 53 55

25 32 33 35 52 53 55

A=

A=

A=

Running time of Radixsort

N items, d digit keys of max value M How many passes? How much work per pass?

Total time?

30

Running time of Radixsort

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

Your Turn

1) 45834,62837,56323,92634,78332,55111

2) 478, 537, 9, 721, 3, 38, 123, 67

3) 126, 328, 636, 341, 416, 131, 328

                                                                                             

How many squares can 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?

There are 11 squares 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.

GUIDED READING

ASSESSMENT1._____ sort is also known as bin sort.

a.       merge sort

b.      radix sort

c.       bubble sort

d.      selection sort

CONTD..

2.Digital sort is an example of _________ sort.

a.       merge sort

b.      bubble sort

c.       selection  sort

d.      radix sort

CONTD..

3.Least significant digit in radix sort processes the

digit from _______.

a.    right to left

b.   left to right

c.    top to bottom

d.   bottom to up