0% found this document useful (0 votes)
2 views

Algorithm 2 Getting Started

The document discusses algorithm efficiency, comparing Linear Search and Binary Search, as well as Insertion Sort and Merge Sort, highlighting their respective number of comparisons. It explains the concepts of best-case, worst-case, and average-case running times, emphasizing the importance of worst-case analysis for understanding algorithm performance. Additionally, it introduces the concept of order of growth to compare the efficiency of algorithms based on their worst-case running times.

Uploaded by

doglord444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Algorithm 2 Getting Started

The document discusses algorithm efficiency, comparing Linear Search and Binary Search, as well as Insertion Sort and Merge Sort, highlighting their respective number of comparisons. It explains the concepts of best-case, worst-case, and average-case running times, emphasizing the importance of worst-case analysis for understanding algorithm performance. Additionally, it introduces the concept of order of growth to compare the efficiency of algorithms based on their worst-case running times.

Uploaded by

doglord444
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Chapter 2 Getting Started

Which algorithm is more efficient?


Example 1: Please use Linear Search and Binary Search to find 20 in the
following sorted array [ -2 4 9 15 17 19 20].
Linear Search
How many comparisons?
7
Which algorithm is more efficient?
Example 1: Please use Linear Search and Binary Search to find 20 in the
following sorted array [ -2 4 9 15 17 19 20].
Binary Search
How many comparisons?
3
Which algorithm is more efficient?
Example 2: Please use Insertion Sort and Merge Sort to sort the
following array in increasing order [ 2 9 5 4].
Insertion Sort
[ 2 9 5 4]  [ 2 9 5 4]  [2 5 9 4]  [2 4 5 9]
How many comparisons?
6
Which algorithm is more efficient?
Example 2: Please use Insertion Sort and Merge Sort to sort the
following array in increasing order [ 2 9 5 4].
Merge Sort
How many comparisons?
5
Running Time
• Suppose two algorithms perform the same task such as
search

which one is better?


• The running time of an algorithm on a particular input
is the number of primitive operations or “steps”
executed.
Running Time
• An input that results in the shortest execution time is called
the best-case input and an input that results in the longest
execution time is called the worst-case input.
• An average-case analysis attempts to determine the average
amount of time among all possible input of the same size.
•The average-case analysis is ideal, but difficult to perform,
because it is hard to determine the relative probabilities and
distributions of various input instances for many problems.
Best, Worst, and Average
Cases
• Best-case and worst-case are not representative, but worst-
case analysis is very useful.
• You can show that the algorithm will never be slower than
the worst-case.
• Worst-case analysis is easier to obtain and is thus common.
So, the analysis is generally conducted for the worst-case.
Running Time
• The running time is dependent on specific input.
T(n) is the running time of an array with n
elements

Best Case  sorted array

T(n)=an+b

Worst Case  reverse sorted array

T(n)=an2+bn+c
Running Time
• The worst-case running time of an algorithm gives us an upper
bound on the running time for any input. Knowing it provides a
guarantee that the algorithm will never take any longer. We
need not make some educated guess about the running time
and hope that it never gets much worse
Running Time
• For some algorithms, the worst case occurs fairly often. For
example, in searching a database for a particular piece of
information, the searching algorithm’s worst case will often
occur when the information is not present in the database. In
some applications, searches for absent information may be
frequent.
Running Time
• The “average case” is often roughly as bad as the worst case.
Suppose that we randomly choose n numbers and apply
insertion sort. How long does it take to determine where in
subarray A[1…,j-1] to insert element A[j] ?

• On average, half the elements in A[1…,j-1] are less than A[j] ,


and half the elements are greater. The resulting average-case
running time turns out to be a quadratic function of the input
size, just like the worst-case running time.
Order of Growth
• We can consider only the leading term of a running time
formula, since the lower-order terms are relatively insignificant
for large values of n

• A simplified abstraction: rate of growth or order of growth

e.g., T(n)=an2+bn+c  Θ(n2)

We usually consider one algorithm to be more efficient than


another if its worst case running time has a lower order of growth.
Order of Growth
• Merge Sort T(n) ?
Order of Growth
• Merge Sort
Order of Growth
• Merge Sort

You might also like