Chapter 7 sor ting a lgorithms
This presentation is the property of its rightful owner.
Sponsored Links
1 / 11

Chapter 7: Sor ting A lgorithms PowerPoint PPT Presentation


  • 107 Views
  • Uploaded on
  • Presentation posted in: General

Mark Allen Weiss: Data Structures and Algorithm Analysis in Java. Chapter 7: Sor ting A lgorithms. Merge Sort. Lydia Sinapova, Simpson College. Merge Sort. Basic Idea Example Analysis Animation. Idea. Two sorted arrays can be merged in linear time with N comparisons only.

Download Presentation

Chapter 7: Sor ting A lgorithms

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 7 sor ting a lgorithms

Mark Allen Weiss: Data Structures and Algorithm Analysis in Java

Chapter 7: Sorting Algorithms

Merge Sort

Lydia Sinapova, Simpson College


Merge sort

Merge Sort

  • Basic Idea

  • Example

  • Analysis

  • Animation


Chapter 7 sorting algorithms

Idea

Two sorted arrays can be merged in linear time with N comparisons only.

Given an array to be sorted,

consider separately its left half and its right half,

sort them and then merge them.


Characteristics

Characteristics

  • Recursive algorithm.

  • Runs in O(NlogN) worst-case running time.

    Where is the recursion?

  • Each half is an array that can be sorted using the same algorithm - divide into two, sort separately the left and the right halves, and then merge them.


Example

Example


Merge sort code

Merge Sort Code

void merge_sort ( int [ ] a, int left, int right)

{

if(left < right) {

int center = (left + right) / 2;

merge_sort (a,left, center);

merge_sort(a,center + 1, right);

merge(a, left, center + 1, right);

}

}


Analysis of merge sort

Analysis of Merge Sort

Assumption:N is a power of two.

For N = 1 time is constant (denoted by 1)

Otherwise:

time to mergesort N elements =

time to mergesort N/2 elements +

time to merge two arrays each N/2 el.


Recurrence relation

Recurrence Relation

Time to merge two arrays each N/2 elements is linear, i.e. O(N)

Thus we have:

(a) T(1) = 1

(b) T(N) = 2T(N/2) + N


Solving the recurrence relation

Solving the Recurrence Relation

T(N) = 2T(N/2) + N divide by N:

(1) T(N) / N = T(N/2) / (N/2) + 1

Telescoping:N is a power of two, so we can write

(2) T(N/2) / (N/2) = T(N/4) / (N/4) +1

(3) T(N/4) / (N/4) = T(N/8) / (N/8) +1

…….

T(2) / 2 = T(1) / 1 + 1


Adding the equations

Adding the Equations

The sum of the left-hand sides will be equal to the sum of the right-hand sides:

T(N) / N + T(N/2) / (N/2) + T(N/4) / (N/4) +

… + T(2)/2 =

T(N/2) / (N/2) + T(N/4) / (N/4) + ….

+ T(2) / 2 + T(1) / 1 +LogN

(LogNis the sum of 1’s in the right-hand sides)


Crossing equal terms final formula

Crossing Equal Terms, Final Formula

After crossing the equal terms, we get

 T(N)/N = T(1)/1 + LogN

T(1) is 1, hence we obtain

  T(N) = N + NlogN = (NlogN)

 Hence the complexity of the Merge Sort algorithm is(NlogN).


  • Login