Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.

Similar presentations


Presentation on theme: "© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction."— Presentation transcript:

1 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction to Algorithms Bret Ford © 2005, Prentice Hall

2 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Selection Sort Proceed through an array position by position, starting with index 0. At the current position, select the element from the remaining elements that is next in order and copy it into the current position. Proceed through an array position by position, starting with index 0. At the current position, select the element from the remaining elements that is next in order and copy it into the current position.

3 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Selection Sort Example Step 1 The ordering begins by locating the smallest animal, the fish, in the first position. The operation occurs by having the owl that currently occupies the first position exchange places with the fish. The effect is to place the smallest animal at the front of the list. The ordering begins by locating the smallest animal, the fish, in the first position. The operation occurs by having the owl that currently occupies the first position exchange places with the fish. The effect is to place the smallest animal at the front of the list.

4 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Selection Sort Example Step 2 The tail of the list, starting at the second position is unordered. The process continues by identifying the smallest animal among the owl, dragon, and dog and arranging it in the second position. The selection is the owl that is already in its proper position and so we can move to the next step. The tail of the list, starting at the second position is unordered. The process continues by identifying the smallest animal among the owl, dragon, and dog and arranging it in the second position. The selection is the owl that is already in its proper position and so we can move to the next step.

5 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Selection Sort Example Step 3 With the fish and owl in position, only the last two animals must be ordered. We identify the dog as the next-smallest animal and have it exchange positions with the dragon. After this step, the tail of the list has only one item left, which must be the largest animal. The entire list is ordered. With the fish and owl in position, only the last two animals must be ordered. We identify the dog as the next-smallest animal and have it exchange positions with the dragon. After this step, the tail of the list has only one item left, which must be the largest animal. The entire list is ordered.

6 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Selection Sort in Action

7 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. selectionSort() public static void selectionSort(int[] arr) { // index of smallest element in the sublist int smallIndex; int pass, j, n = arr.length; int temp; // pass has the range 0 to n-2 for (pass = 0; pass < n-1; pass++) { // scan the sublist starting at index pass smallIndex = pass;

8 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. selectionSort() (concluded) // j traverses the sublist // arr[pass+1] to arr[n-1] for (j = pass+1; j < n; j++) // if smaller element found, assign // smallIndex to that position if (arr[j] < arr[smallIndex]) smallIndex = j; // swap the next smallest // element into arr[pass] temp = arr[pass]; arr[pass] = arr[smallIndex]; arr[smallIndex] = temp; }

9 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Selection Sort Example // declare an integer array int[] arr = {66, 20, 33, 55, 53, 57, 69, 11, 67, 70}; // call selectionSort() to order the array Arrays.selectionSort(arr); System.out.print("Sorted: "); for (int i=0; i < arr.length; i++) System.out.print(arr[i] + " "); Output: Sorted: 11 20 33 53 55 57 66 67 69 70

10 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Sublists in an Array A sublist of an array is a sequence of elements whose range of indices begin at index first and continue up to, but not including last. Indicate a sublist using the notation [first, last). A sublist of an array is a sequence of elements whose range of indices begin at index first and continue up to, but not including last. Indicate a sublist using the notation [first, last).

11 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Sequential Search Begin with a target value and an index range [first, last). Scan the sublist item by item, looking for the first occurrence of a match with an item named target. Return the index of the match or -1 if target is not in the sublist. Begin with a target value and an index range [first, last). Scan the sublist item by item, looking for the first occurrence of a match with an item named target. Return the index of the match or -1 if target is not in the sublist.

12 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. The seqSearch() Method

13 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. seqSearch() Implementation public static int seqSearch(int[] arr, int first, int last, int target) { // scan indices in the range first // <= i < last; return the index // indicating the position if a match occurs; // otherwise return -1 for (int i = first; i < last; i++) if (arr[i] == target) return i; // no return yet if match is // not found; return -1 return -1; }

14 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Binary Search The binary search exploits the fact that a list is ordered. This allows large sections of the list to be removed from the search. The binary search exploits the fact that a list is ordered. This allows large sections of the list to be removed from the search. Select the midpoint of the current sublist [first,last). Select the midpoint of the current sublist [first,last). If target matches midpoint, the search is successful. If target matches midpoint, the search is successful. If the target is less than the current midpoint value, look in the lower sublist by selecting its midpoint; otherwise, look in the upper sublist by selecting its midpoint. If the target is less than the current midpoint value, look in the lower sublist by selecting its midpoint; otherwise, look in the upper sublist by selecting its midpoint. Continue until locating the target or the sublist reduces to size 0. Continue until locating the target or the sublist reduces to size 0.

15 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Binary Search Case 1 A match occurs. The search is complete, and mid is the index that locates target. A match occurs. The search is complete, and mid is the index that locates target.

16 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Binary Search Case 2 The value target is less than midValue, and the search must continue in the lower sublist. The index range for this sublist is [first, mid). Reposition the index last to the end of the sublist ( last = mid ). The value target is less than midValue, and the search must continue in the lower sublist. The index range for this sublist is [first, mid). Reposition the index last to the end of the sublist ( last = mid ).

17 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Binary Search Case 3 The value target is greater than midValue, and the search must continue in the upper sublist. The index range for this sublist is [mid+1,last), because the sublist begins immediately to the right of mid. Reposition the index first to the front of the sublist (first = mid+1). The value target is greater than midValue, and the search must continue in the upper sublist. The index range for this sublist is [mid+1,last), because the sublist begins immediately to the right of mid. Reposition the index first to the front of the sublist (first = mid+1).

18 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Binary Search Termination The binary search terminates when a match is found or when the sublist to be searched is empty. An empty sublist occurs when first >= last. The binary search terminates when a match is found or when the sublist to be searched is empty. An empty sublist occurs when first >= last.

19 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Binary Search Success Example Step 1 target = 23

20 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Binary Search Success Example Step 2 target = 23

21 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Binary Search Success Example Step 3 target = 23

22 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Binary Search Failure Example Step 1 target = 4

23 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Binary Search Failure Example Step 2 target = 4

24 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Binary Search Failure Example Step 3 target = 4

25 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Binary Search Failure Example Step 4 Index range [2,2). first ≥ last, so the search terminates unsuccessfully. The return value is -1.

26 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. binSearch() Method public static int binSearch(int arr[], int first, int last, int target) { // index of the midpoint int mid; // value that is assigned arr[mid] int midValue; // test for nonempty sublist while (first < last) { mid = (first+last)/2; midValue = arr[mid];

27 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. binSearch() Method (concluded) if (target == midValue) // have a match return mid; // determine which sublist to search else if (target < midValue) // search lower sublist; reset last last = mid; else // search upper sublist; reset first first = mid+1; } // target not found return -1; }

28 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. System/Memory Efficiency System efficiency measures how well an algorithm runs on a particular machine. System efficiency measures how well an algorithm runs on a particular machine. Memory efficiency is a measure of the amount of memory an algorithm uses. If an algorithm uses too much memory, it can be too slow or may not execute at all on a particular system. Memory efficiency is a measure of the amount of memory an algorithm uses. If an algorithm uses too much memory, it can be too slow or may not execute at all on a particular system.

29 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Running Time Analysis Machine independent measures of efficiency involve counting the number of comparisons, assignment statements, etc. Machine independent measures of efficiency involve counting the number of comparisons, assignment statements, etc. To analyze the complexity of an algorithm, identify the dominant operation and measure the number of times it occurs. To analyze the complexity of an algorithm, identify the dominant operation and measure the number of times it occurs.

30 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Running Time: min() Method Count the number of comparisons, T(n), required to locate the minimum of the elements in an n-element array. T(n) = n-1 Count the number of comparisons, T(n), required to locate the minimum of the elements in an n-element array. T(n) = n-1

31 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Running Time: Selection Sort Count the number of comparisons in the n-1 passes of the algorithm. T(n) = (n-1) + (n-2) +... + 2 + 1 T(n) = n(n-1)/2 = n 2 /2 - n/2 Count the number of comparisons in the n-1 passes of the algorithm. T(n) = (n-1) + (n-2) +... + 2 + 1 T(n) = n(n-1)/2 = n 2 /2 - n/2

32 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Running Time: Sequential Search Best case: Locating the target at index 0. T(n) = 1 Best case: Locating the target at index 0. T(n) = 1 Worst caset: Locating the target at index n-1 or finding no match. T(n) = n Worst caset: Locating the target at index n-1 or finding no match. T(n) = n Average case: Average of the number of comparisons to locate a target at each position. T(n)=(1+2+3...+n)/n=n(n+1)/2 (1/n) = (n+1)/2 Average case: Average of the number of comparisons to locate a target at each position. T(n)=(1+2+3...+n)/n=n(n+1)/2 (1/n) = (n+1)/2

33 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Running Time: Binary Search Best case: Target found at first midpoint. T(n) = 1 Best case: Target found at first midpoint. T(n) = 1 Worst case: Length of sublists decrease by a factor of two at each iteration. T(n) = (int)log 2 n + 1 Worst case: Length of sublists decrease by a factor of two at each iteration. T(n) = (int)log 2 n + 1 Average case: A sophisticated analysis shows that the average number of iterations is one less than the number in the worst case. Average case: A sophisticated analysis shows that the average number of iterations is one less than the number in the worst case.

34 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Big-O Notation To get an approximate measure for T(n), define a notation that involves the dominant term of T(n). O( ) is called the Big-O measure for the algorithm. To get an approximate measure for T(n), define a notation that involves the dominant term of T(n). O( ) is called the Big-O measure for the algorithm. Selection sort is O(n 2 ). Selection sort is O(n 2 ). The average case for sequential search is O(n). The average case for sequential search is O(n). The worst case for binary search is O(log 2 n). The worst case for binary search is O(log 2 n). If T(n) = 8n 3 +5n 2 -11n+1, then T(n) is O(n 3 ). If T(n) = 8n 3 +5n 2 -11n+1, then T(n) is O(n 3 ).

35 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Common Orders of Magnitude Constant time: Algorithm is O(1) when its running time is independent of the number of items. Constant time: Algorithm is O(1) when its running time is independent of the number of items. Examples: Find the minimum of an ordered n-element array. Examples: Find the minimum of an ordered n-element array.

36 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Common Orders of Magnitude (continued) Linear: Algorithm is O(n) when its running time is proportional to n, the size of the list. If n doubles, number of operations doubles. Linear: Algorithm is O(n) when its running time is proportional to n, the size of the list. If n doubles, number of operations doubles. Example: Find the minimum of an unordered n-element array. Example: Find the minimum of an unordered n-element array.

37 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Common Orders of Magnitude (continued) Quadratic: An algorithm is quadratic if its running time is O(n 2 ). If n doubles, the number of operations increases by a factor of 4. Quadratic: An algorithm is quadratic if its running time is O(n 2 ). If n doubles, the number of operations increases by a factor of 4. Example: Selection sort. Example: Selection sort. Cubic: An algorithm is cubic if its running time is O(n 3 ). Doubling n increases the number of operations by a factor of 8. Cubic: An algorithm is cubic if its running time is O(n 3 ). Doubling n increases the number of operations by a factor of 8. Example: The number of products in matrix multiplication is cubic. Example: The number of products in matrix multiplication is cubic.

38 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Common Orders of Magnitude (continued) Logarithmic: An algorithm is logarithmic if its running time is O(log 2 ) or O(n log 2 ). Occurs when the algorithm repeatedly subdivides the data into sublists whose sizes are 1/2, 1/4, 1/8,... of the original size n. Logarithmic: An algorithm is logarithmic if its running time is O(log 2 ) or O(n log 2 ). Occurs when the algorithm repeatedly subdivides the data into sublists whose sizes are 1/2, 1/4, 1/8,... of the original size n. Example: Binary search is O(log 2 ), and quicksort is O(n log 2 ). Example: Binary search is O(log 2 ), and quicksort is O(n log 2 ).

39 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Common Orders of Magnitude (concluded) Exponential: An algorithm is exponential if its running time is O(a n ). These algorithms deal with problems that require searching through a large number of potential solutions before finding an answer. Exponential: An algorithm is exponential if its running time is O(a n ). These algorithms deal with problems that require searching through a large number of potential solutions before finding an answer. Example: The traveling salesperson problem. Example: The traveling salesperson problem.

40 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Comparison of Graphs

41 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Orders of Magnitude for Selected Values of n

42 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. The Timing Class

43 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Timing Class Example Timing sortTimer = new Timing(); double timeInSec; // flank the process with calls to start() and stop() sortTimer.start();// start timing Sort.selectionSort(arr);// the sorting process timeInSec = sortTimer.stop();// get time in seconds

44 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Program 4.1 import java.util.Random; import java.text.DecimalFormat; import ds.util.Arrays; import ds.time.Timing; public class Program4_1 { public static void main(String[] args) { final int ARRAY_SIZE = 100000, TARGET_SIZE = 50000; // arrays for the search int[] listSeq = new int[ARRAY_SIZE], listBin = new int[ARRAY_SIZE], targetList = new int[TARGET_SIZE]; int i;

45 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Program 4.1 (continued) // use Timing object t to compute // times for each process Timing t = new Timing(); double seqTime, binTime; // random number object Random rnd = new Random(); // format real numbers with // three decimal places DecimalFormat fmt = new DecimalFormat("#.000");

46 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Program 4.1 (continued) // initialize the arrays with // random numbers in the // range 0 to 999,999 for (i = 0; i < ARRAY_SIZE; i++) listSeq[i] = listBin[i] = rnd.nextInt(1000000); // initialize targetList with // random numbers in the // same range 0 to 999,999 for (i=0;i < TARGET_SIZE; i++) targetList[i] = rnd.nextInt(1000000); // time the sequential search // with elements from listSeq t.start(); for (i = 0; i < TARGET_SIZE; i++) Arrays.seqSearch(listSeq,0,ARRAY_SIZE, targetList[i]);

47 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Program 4.1 (concluded) binTime = t.stop(); System.out.println( "Binary Search takes " + fmt.format(binTime) + " seconds."); System.out.println( "Ratio of sequential to" + "binary search time is " + fmt.format(seqTime/binTime)); } /* Run: Sequential Search takes 16.094 seconds. Binary Search takes.016 seconds. Ratio of sequential to binary search time is 1005.875 */


Download ppt "© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction."

Similar presentations


Ads by Google