Download - 10 merge sort

Transcript

Merge Sort

Merge Sort

The Merge Sort Algorithm is based on a simple operation known as merging:

combining two ordered arrays to make one larger ordered array.

To sort an array:

1. divide it into two halves

2. sort the two halves (recursively)

3. and then merge the results.

Merge sort guarantees to sort an array of N items in time proportional to N log N, no matter what the input.

Merge Sort (cont.)

• Recursion:

Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem.

Most computer programming languages support recursion by allowing a function to call itself within the program text.

In mathematics, recursion is frequently used. The most common example is the factorial:

For example, 5! = 5(4)(3)(2)(1), or5! = 5(4!)

Merge Sort (cont.)

• Recursion (cont.):

In other words,

Or

This definition says that 0! is 1, while the factorial of any other number is that number times the factorial of one less than that number.

! ( 1)!n n n

1 if 0!

( 1)! otherwise

nn

n n

Merge Sort (cont.)

• Recursion (cont.):

Our definition is recursive. Consider 4!

– 4! = 4(4-1)! = 4(3!)

– What is 3!? We apply the definition again4! = 4(3!) = 4[3(3-1)!] = 4(3)(2!)

– And so on…4! = 4(3!) = 4(3)(2!) = 4(3)(2)(1!) = 4(3)(2)(1)(0!) = 4(3)(2)(1)(1) = 24

Merge Sort (cont.)

• Recursion (cont.):All good recursive definitions have these two key characteristics:

– There are one or more base cases for which no recursion is applied.– All chains of recursion eventually end up at one of the base cases.

The simplest way for these two conditions to occur is for each recursion to act on a smaller version of the original problem. A very small version of the original problem that can be solved without recursion becomes the base case.

Merge Sort (cont.)

• Merge Sort Algorithm:

• 1. Merge Sort (Overview):

1. Split array into two halves

2. Sort the left half (recursively)

3. Sort the right half (recursively)

4. Merge the two sorted halves

Merge Sort (cont.)

• Merge Sort Algorithm (cont.):

• 2. Merging two halves:

1. Access the first item from both halves

2. While neither half is finished

1. Compare the current items of both

2. Copy smaller current item to the output

3. Access next item from that input half

3. Copy any remaining from first half to output

4. Copy any remaining from second half to output

Merge Sort (cont.)

• Merging:

The key to Merge Sort is merging two sorted lists into one, such that if you have two lists X (x1x2

…xm) and Y(y1y2…yn) the resulting list is

Z(z1z2…zm+n)

Example:

L1 = { 3 8 9 } L2 = { 1 5 7 }

merge(L1, L2) = { 1 3 5 7 8 9 }

Merge Sort (cont.)

• Merging (cont.):

3 10 23 54 1 5 25 75X: Y:

Result:

Merge Sort (cont.)

• Merging (cont.):

3 10 23 54 5 25 75

1

X: Y:

Result:

Merge Sort (cont.)

• Merging (cont.):

10 23 54 5 25 75

1 3

X: Y:

Result:

Merge Sort (cont.)

• Merging (cont.):

10 23 54 25 75

1 3 5

X: Y:

Result:

Merge Sort (cont.)

• Merging (cont.):

23 54 25 75

1 3 5 10

X: Y:

Result:

Merge Sort (cont.)

• Merging (cont.):

54 25 75

1 3 5 10 23

X: Y:

Result:

Merge Sort (cont.)

• Merging (cont.):

54 75

1 3 5 10 23 25

X: Y:

Result:

Merge Sort (cont.)

• Merging (cont.):

75

1 3 5 10 23 25 54

X: Y:

Result:

Merge Sort (cont.)

• Merging (cont.):

1 3 5 10 23 25 54 75

X: Y:

Result:

Merge Sort (cont.)

• Merging a two lists of one element each is the same as sorting them.

• Merge sort divides up an unsorted list until the above condition is met and then sorts the divided parts back together in pairs.

• Specifically this can be done by recursively dividing the unsorted list in half, merge sorting the left side then the right side and then merging the right and left back together.

Merge Sort (cont.)

• Merge Sort Example:

99 6 86 15 58 35 86 4 0

Merge Sort (cont.)

• Merge Sort Example:

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

Merge Sort (cont.)

• Merge Sort Example:

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

86 1599 6 58 35 86 4 0

Merge Sort (cont.)

• Merge Sort Example:

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

86 1599 6 58 35 86 4 0

99 6 86 15 58 35 86 4 0

Merge Sort (cont.)

• Merge Sort Example:

99 6 86 15 58 35 86 4 0

99 6 86 15 58 35 86 4 0

86 1599 6 58 35 86 4 0

99 6 86 15 58 35 86 4 0

4 0

Merge Sort (cont.)

• Merge Sort Example:

99 6 86 15 58 35 86 0 4

4 0Merge

Merge Sort (cont.)

• Merge Sort Example:

99 6 86 15 58 35 86 0 4

Merge

15 866 99 35 58 0 4 86

Merge Sort (cont.)

• Merge Sort Example:

Merge

15 866 99 58 35 0 4 86

6 15 86 99 0 4 35 58 86

Merge Sort (cont.)

• Merge Sort Example:

Merge

6 15 86 99 0 4 35 58 86

0 4 6 15 35 58 86 86 99

Merge Sort (cont.)

• Merge Sort Example:

0 4 6 15 35 58 86 86 99

Merge Sort (cont.)