SlideServe
Menu
Préférences
© SlideServe · v2026

Efficiency Analysis of Searching Algorithms in Java Programming

drake-ayers avatar
18 diapositives · 273 Views · Oct 28, 2014

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.

Efficiency Analysis of Searching Algorithms in Java Programming - slide 1
01 / 18Efficiency Analysis of Searching Algorithms in Java Programming
1 / 17

Télécharger la présentation

Efficiency Analysis of Searching Algorithms in Java Programming
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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.