180 likes | 262 Vues
Compare linear and binary search algorithms in Java for efficiency based on operations, processor, and multitasking. Discuss Big-O notation and code idioms for algorithm analysis using insertion sort.
E N D
CSC 205 Java Programming II Algorithm Efficiency
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
Searching Algorithms • Linear search and binary search Is meerkat in the table? IndexScan TableScan
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
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;
Binary Search • Number of comparisons • Worst case: Log2(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; }
Big-O Notation • Exact number doesn’t matter • Just concern order-of-magnitude • O(n) means that f(n) < c*n for 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)
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(n2) • for (int i=0; i<n; i++) for (int j=0; j<i; j++) O(n2) • for (int i=0; i<n; i++) for (int j=0; j<k; j++) O(n) note: k is a constant
Code Idioms & Big-O (cont’d) • S O(1) • while (n>1){ n /= 2; S } O(log n) • for (int i=0; i<n; i++) while (n>1) { n /= 2; S } O(nlog n)
Order Hierarchy O(1)<O(log n) <O(n)<O(n log n)<O(n2)<O(n3)<…<O(2n)
Determine Big-O Notation • For the following growth rate functions, give the corresponding Big-O notations • n2 + 100n • 7*log2n + n • n*log2n + n2
Insertion Sort • Snapshots of an array as the numbers 83, 24, 56, 90, and 17 are inserted:
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.
Insertion Sort • An example that uses insertion sort to sort an array of five integers: Shaded elements are not yet in sorted order.
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; }
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) { … }
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 n2/2, so the time required to sort grows rapidly as n increases.
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.