1 / 18

CSC 205 Java Programming II

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.

drake-ayers
Download Presentation

CSC 205 Java Programming II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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? IndexScan TableScan

  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: 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; }

  7. 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)

  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(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

  9. 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)

  10. Order Hierarchy O(1)<O(log n) <O(n)<O(n log n)<O(n2)<O(n3)<…<O(2n)

  11. Determine Big-O Notation • For the following growth rate functions, give the corresponding Big-O notations • n2 + 100n • 7*log2n + n • n*log2n + n2

  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 n2/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.

More Related