Download presentation
Presentation is loading. Please wait.
Published byAshley Dennis Modified over 9 years ago
1
Comp 245 Data Structures Analysis of Algorithms
2
Purpose A professional programmer must be able to determine how efficient his code is. Efficiency, as we will analyze, will be determined by the approximate number of instructions needed to carry out the algorithm.
3
Example Assume you are asked to write an algorithm to calculate the sum of the following arithmetic series: 1 + 2 + 3 + 4 + … + (N-1) + N One method would be to write a loop that would continuously add the next number in the series to an accumulator until you reached the n th number. Another way would be to use Gauss’ formula: (N * (N+1))/2
4
Code Comparison Loop Solution int sum = 0; for (int i = 1; i <= N; i++) sum = sum + i; Calculating the sum will take approximately N instructions. This solution is considered linear. Formula Solution (Gaussian solution) int sum = (N * (N+1))/2; Calculating the sum will take ONE instruction regardless of N. This solution is considered constant.
5
Order of Magnitude Assume the following polynomial function: 3x 3 + 2x 2 – x + 5 This polynomial is classified as a third degree polynomial. This is considered the order of magnitude. The 3x 3 term dominates the equation as x gets large. The instruction efficiency of an algorithm will be a polynomial and classified according to it’s order of magnitude.
6
Main Classifications of Algorithms Big O Notation ConstantO(1)Gauss LogarithmicO(log 2 N)Binary Search LinearO(N)Seq Search Linear LogarithmicO(Nlog 2 N)Quicksort QuadraticO(N 2 )Bubblesort
7
Algorithms We Will Analyze Search Routines Unordered Sequential Search Ordered Sequential Search Binary Search Hashing Sort Routines Bubble Sort Selection Sort QuickSort
8
Search Method of Analysis Search algorithms are dominated by comparison operations: (value == target) Three types of cases to analyze: Best Case – finding target on first comparison Average Case – how many comparisons on average to find the target Worst Case – finding target on last access or target does not exist
9
Sort Method of Analysis Sort algorithms are dominated by comparison and swap operations. Three types of cases to analyze: Best Case – data is already sorted Average Case – data is randomly arranged Worst Case – data is in reverse order
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.