Download presentation
Presentation is loading. Please wait.
1
CSC 205 Java Programming II Algorithm Efficiency
2
Algorithm Analysis Assume two algorithms perform the same task. Which one do you choose to use? That is, which one is better? Choose the one that is more efficient Runs faster Takes up less space
3
Searching Algorithms Linear search and binary search Is meerkat in the table? TableScan IndexScan
4
Efficiency Instead of measuring the real run- time, we compare the number of operations Available before running, even writing the program Run-time will be effected by Processor multitasking
5
Linear Search Number of comparisons Worst case:n Best case:1 int found = -1; for (int i=0; i<a.length; i++) { if (a[i] == key) { found = i; break; } return found;
6
Binary Search Number of comparisons Worst case: Log 2 (n) Best case: 1 int low = 0; int high = a.length - 1; while (low < high) { int mid = (low + high) / 2; if (a[mid] < key) low = mid + 1; else high = mid; }
7
Big-O Notation Exact number doesn’t matter Just concern order-of-magnitude O(n) means that f(n) k, where c and k are both constants The search algorithms Linear search: O(n) (big-O of n) Binary search: O(log n) (big-O of the logarithm of n)
8
Code Idioms & Big-O for (int i=0; i<n; i++)O(n) for (int i=0; i<n; i++) for (int j=0; j<n; j++) O(n 2 ) for (int i=0; i<n; i++) for (int j=0; j<i; j++) O(n 2 ) for (int i=0; i<n; i++) for (int j=0; j<k; j++) O(n) note: k is a constant
9
Code Idioms & Big-O (cont’d) SO(1) while (n>1) { n /= 2; S }O(log n) for (int i=0; i 1) { n /= 2; S } O(nlog n)
10
Order Hierarchy O(1)<O(log n) <O(n)<O(n log n)<O(n 2 )<O(n 3 )<…<O(2 n )
11
Determine Big-O Notation For the following growth rate functions, give the corresponding Big-O notations n 2 + 100n 7*log 2 n + n n*log 2 n + n 2
12
Insertion Sort Snapshots of an array as the numbers 83, 24, 56, 90, and 17 are inserted:
13
Insertion Sort The insertion sort algorithm is simpler if the element to be inserted (the one at position i) is compared with the elements at positions i – 1, i – 2, …, 0, working backward instead of forward. If a comparison indicates that the element to be inserted is smaller than the one at position j, then the element at position j can be moved down one place in the array.
14
Insertion Sort An example that uses insertion sort to sort an array of five integers: Shaded elements are not yet in sorted order.
15
Insertion Sort A Java version of insertion sort: for (int i = 1; i < a.length; i++) { int itemToInsert = a[i]; int j = i - 1; while (j >= 0 && itemToInsert < a[j]) { a[j+1] = a[j]; j--; } a[j+1] = itemToInsert; }
16
Insertion Sort If the array contains values other than numbers, the condition in the while statement will need to be changed. The while statement if a contains strings: while (j >= 0 && itemToInsert.compareTo(a[j]) < 0) { … }
17
Insertion Sort Insertion sort is easy to implement but not very efficient. In the worst case, it performs 1 + 2 + 3 + … + (n – 1) = n(n – 1)/2 comparisons, where n is the number of elements in the array. This number is approximately n 2 /2, so the time required to sort grows rapidly as n increases.
18
Insertion Sort Comparisons performed by insertion sort for various array sizes: Array Number of Size Comparisons 10 45 100 4,950 1,000 499,500 10,000 49,995,000 100,000 4,999,950,000 For small arrays, insertion sort is usually fast enough. For larger arrays, better algorithms are necessary.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.