Download presentation
Presentation is loading. Please wait.
Published byCollin Osborne Modified over 5 years ago
1
Data Structures for Java William H. Ford William R. Topp
Chapter 4 Introduction to Algorithms Bret Ford © 2005, Prentice Hall © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
2
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. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
3
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. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
4
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. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
5
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. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
6
Selection Sort in Action
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
7
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; © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
8
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; } © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
9
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: © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
10
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). © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
11
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. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
12
The seqSearch() Method
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
13
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; } © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
14
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. Select the midpoint of the current sublist [first,last). 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. Continue until locating the target or the sublist reduces to size 0. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
15
Binary Search Case 1 A match occurs. The search is complete, and mid is the index that locates target. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
16
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). © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
17
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). © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
18
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. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
19
Binary Search Success Example Step 1
target = 23 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
20
Binary Search Success Example Step 2
target = 23 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
21
Binary Search Success Example Step 3
target = 23 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
22
Binary Search Failure Example Step 1
target = 4 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
23
Binary Search Failure Example Step 2
target = 4 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
24
Binary Search Failure Example Step 3
target = 4 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
25
Binary Search Failure Example Step 4
Index range [2,2). first ≥ last, so the search terminates unsuccessfully. The return value is -1. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
26
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]; © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
27
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; © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
28
System/Memory Efficiency
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. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
29
Running Time Analysis 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. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
30
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 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
31
Running Time: Selection Sort
Count the number of comparisons in the n-1 passes of the algorithm. T(n) = (n-1) + (n-2) T(n) = n(n-1)/2 = n2/2 - n/2 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
32
Running Time: Sequential Search
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 Average case: Average of the number of comparisons to locate a target at each position. T(n)=( n)/n=n(n+1)/2 (1/n) = (n+1)/2 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
33
Running Time: Binary Search
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)log2n + 1 Average case: A sophisticated analysis shows that the average number of iterations is one less than the number in the worst case. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
34
Big-O Notation To get an approximate measure for T(n), define a notation that involves the dominant term of T(n). O(<term>) is called the Big-O measure for the algorithm. Selection sort is O(n2). The average case for sequential search is O(n). The worst case for binary search is O(log2n). If T(n) = 8n3+5n2-11n+1, then T(n) is O(n3). © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
35
Common Orders of Magnitude
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. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
36
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. Example: Find the minimum of an unordered n-element array. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
37
Common Orders of Magnitude (continued)
Quadratic: An algorithm is quadratic if its running time is O(n2). If n doubles, the number of operations increases by a factor of 4. Example: Selection sort. Cubic: An algorithm is cubic if its running time is O(n3). Doubling n increases the number of operations by a factor of 8. Example: The number of products in matrix multiplication is cubic. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
38
Common Orders of Magnitude (continued)
Logarithmic: An algorithm is logarithmic if its running time is O(log2) or O(n log2). 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(log2), and quicksort is O(n log2). © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
39
Common Orders of Magnitude (concluded)
Exponential: An algorithm is exponential if its running time is O(an). These algorithms deal with problems that require searching through a large number of potential solutions before finding an answer. Example: The traveling salesperson problem. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
40
Comparison of Graphs © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
41
Orders of Magnitude for Selected Values of n
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
42
The Timing Class © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
43
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 © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
44
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 = , 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; © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
45
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"); © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
46
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( ); // initialize targetList with // same range 0 to 999,999 for (i=0;i < TARGET_SIZE; i++) targetList[i] = rnd.nextInt( ); // 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]); © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
47
Program 4.1 (concluded) binTime = t.stop(); System.out.println(
"Binary Search takes " + fmt.format(binTime) + " seconds."); "Ratio of sequential to" + "binary search time is " + fmt.format(seqTime/binTime)); } /* Run: Sequential Search takes seconds. Binary Search takes .016 seconds. Ratio of sequential to binary search time is */ © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.