Data Structures for Java William H. Ford William R. Topp

Slides:



Advertisements
Similar presentations
CSE Lecture 3 – Algorithms I
Advertisements

Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, Java Version, Third Edition.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
ADSA: Sorting/ Advanced Data Structures and Algorithms Objective –examine popular sorting algorithms, with an emphasis on divide and conquer.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 7 Sorting.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Algorithm Efficiency and Sorting
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Main Index Contents 11 Main Index Contents Selection Sort Selection SortSelection Sort Selection Sort (3 slides) Selection Sort Alg. Selection Sort Alg.Selection.
Analysis of Algorithm.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Sort, Search, and Running Time.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
ADSA: IntroAlgs/ Advanced Data Structures and Algorithms Objective –introduce algorithm design using basic searching and sorting, and remind.
CSC 211 Data Structures Lecture 13
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Data Structure Introduction.
ADSA: Generics/ Advanced Data Structures and Algorithms Objective –to describe basic forms of generic classes, interfaces, and methods for searching.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Introduction to Analysis of Algorithms CS342 S2004.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Searching and Sorting Searching algorithms with simple arrays
Algorithm Analysis 1.
16 Searching and Sorting.
19 Searching and Sorting.
Data Structures I (CPCS-204)
Algorithm Analysis: Big O Notation
Introduction to Search Algorithms
CSC 222: Object-Oriented Programming
Computer Science 112 Fundamentals of Programming II
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Algorithm Analysis CSE 2011 Winter September 2018.
Chapter 13: Searching and Sorting
Teach A level Computing: Algorithms and Data Structures
Searching and Sorting Linear Search Binary Search ; Reading p
Building Java Programs
Linear and Binary Search
Algorithm design and Analysis
Chapter 3: The Efficiency of Algorithms
Searching and Sorting 1-D Arrays
Chapter 3: The Efficiency of Algorithms
24 Searching and Sorting.
IST311 - CIS265/506 Cleveland State University – Prof. Victor Matos
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Podcast Ch22a Title: Array-based Binary Trees
Sorting and Searching -- Introduction
Chapter 19 Searching, Sorting and Big O
Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases.
Presentation transcript:

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.

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.

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.

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.

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.

Selection Sort in Action © 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; © 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; } © 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 © 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). © 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. © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.

The seqSearch() Method © 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; } © 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. 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.

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.

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.

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.

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.

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

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

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

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

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

Binary Search Failure Example Step 3 target = 4 © 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. © 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]; © 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; © 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. 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.

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.

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.

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 = n2/2 - n/2 © 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 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 © 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 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.

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.

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.

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.

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.

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.

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.

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

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

The Timing Class © 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 © 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; © 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"); © 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 // 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]); © 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."); "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 */ © 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.