Presentation is loading. Please wait.

Presentation is loading. Please wait.

Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.

Similar presentations


Presentation on theme: "Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the."— Presentation transcript:

1 Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the performance or complexity of an algorithm. – Time takes to execute – space required in memory for the algorithm. Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount of space an algorithm uses

2 O(N) n = number of elements O(N) describes an algorithm whose performance will grow linearly and in direct proportion to the size of the input data set. public static int linearSearch(int []nums, int target) { for(int index = 0; index < nums.length; index++) { if(target == nums[index]) return index; } return -1; } One for loop to search through the array. The size of the array will effect the time it takes and space required.

3 O(N 2 ) O(N 2 ) represents an algorithm whose performance is directly proportional to the square of the size of the input data set. It requires passing over the elements twice as with nested iteration (2 for loops) public static int[] selectionSort(int[]num) { int min, temp; for(int index = 0; index <2; index++) { min = index; for(int scan = index+1; scan < num.length; scan++) if(num[scan] < num[min]) min = scan; //swap the values temp = num[min]; num[min] = num[index]; num[index] = temp; } return num; }

4 Selection and Insertion Sort Both the insertion sort and the selection sort algorithms have efficiencies on the order of n 2 where n is the number of values in the array being sorted. Time efficiency O(2 n ) means that as the size of the input increases, the running time increases exponentially

5 Insertion & Selection Summary – Both have O(n2) – Selection sort is usually easier to understand. – Selection sort makes fewer swaps. – Insertion sort may be a good choice if you are continually adding values to a list. – Binary search is more efficient than a linear search. – Binary search must be sorted first.

6 Linear Search public static int linearSearch(int []nums, int target) { for(int index = 0; index < nums.length; index++) { if(target == nums[index]) return index; } return -1; }

7 Binary search The speed of a binary search comes from the elimination of half of the data set each time. If each arrow below represents one binary search process, only ten steps are required to search a list of 1,024 numbers: 1024  512  256  128  64  32  16  8  4  2  1 2 10 = 1024 so it takes 10 divisions

8 Efficiency The log 2 1024 is 10. In a worst-case scenario, if the size of the list doubled to 2,048, only one more step would be required using a binary search. The efficiency of a binary search is illustrated in this comparison of the number of entries in a list and the number of binary divisions required

9 Efficiency The order of a binary search is O(log2 N ). Number of EntriesNumber of Binary Divisions 1,02410 2 10 2,04811 2 11 4,09612 2 12 …… 32,76815 2 15 …… 1,048,57620 2 20 Nlog 2 N The natural logarithm has the constant e (≈ 2.718) as its base.natural logarithmconstant e The logarithm of a number is the exponent by which another fixed value, the base, must be raised to produce that number. Binary logarithm uses base 2.

10 Summary Big Oh is the used to determine the time it takes for the algorithm to complete O(n) is the quickest. Requires one loop through O(n2) Selection & Insertion sort. Requires 2 passes through array 2 for statements O(log2 N ) Binary search uses base 2. Doubling the number of elements results in one more division


Download ppt "Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the."

Similar presentations


Ads by Google