Big Oh Algorithms are compared to each other
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
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 linear. Search(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.
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[] selection. Sort(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; }
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
Insertion & Selection • Summary – Both have O(n 2) – 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.
Linear Search public static int linear. Search(int []nums, int target) { for(int index = 0; index < nums. length; index++) { if(target == nums[index]) return index; } return -1; }
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 210 = 1024 so it takes 10 divisions
Efficiency • The log 21024 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
Efficiency • The order of a binary search is O(log 2 N). Number of Entries 1, 024 Number of Binary Divisions 10 2, 048 11 211 4, 096 12 212 … 32, 768 … 15 … 1, 048, 576 N 215 … 20 220 log 2 N The natural logarithm has the constant e (≈ 2. 718) as its base. 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.
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(n 2) Selection & Insertion sort. Requires 2 passes through array 2 for statements O(log 2 N) Binary search uses base 2. Doubling the number of elements results in one more division
- Slides: 10