A_Survey_of_Common_Running_Times

A_Survey_of_Common_Running_Times - L in e ar T im e : O (n...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
2.4 A Survey of Common Running Times 14 Linear Time: O(n) Linear time. Running time is proportional to input size. Computing the maximum. Compute maximum of n numbers a 1 , …, a n . max ( a 1 for i = 2 to n { if (a i > max) max ( a i } 15 Linear Time: O(n) Merge. Combine two sorted lists A = a 1 ,a 2 ,…,a n with B = b 1 ,b 2 ,…,b n into sorted whole. Claim. Merging two lists of size n takes O(n) time. Pf. After each comparison, the length of output list increases by 1. i = 1, j = 1 while (both lists are nonempty) { if (a i # b j ) append a i to output list and increment i else (a i # b j ) append b j to output list and increment j } append remainder of nonempty list to output list 16 O(n log n) Time O(n log n) time. Arises in divide-and-conquer algorithms. Sorting. Mergesort and heapsort are sorting algorithms that perform O(n log n) comparisons. Largest empty interval. Given n time-stamps x 1 , …, x n on which copies of a file arrive at a server, what is largest interval of time when no
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 2

A_Survey_of_Common_Running_Times - L in e ar T im e : O (n...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online