structural programming course content and data structures
play

Structural Programming Course Content and Data Structures - PDF document

Structural Programming Course Content and Data Structures Introduction Vectors Objects Testing/Debugging Winter 2000 Methods Arrays Tracing Programs Searching CMPUT 102: Searching Object State


  1. Structural Programming Course Content and Data Structures • Introduction • Vectors • Objects • Testing/Debugging Winter 2000 • Methods • Arrays • Tracing Programs • Searching CMPUT 102: Searching • Object State • Files I/O • Sharing resources • Sorting Dr. Osmar R. Zaïane • Selection • Inheritance • Repetition • Recursion University of Alberta  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 1  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 2 2 Objectives of Lecture 21 Outline of Lecture 21 Searching Searching • Review the simple array examples • Introduce two techniques for searching for an • Sequential search approach element in a collection; • Complexity of sequential search • Learn sequential search algorithm; • Binary search approach • Learn the binary search algorithm for ordered • Complexity of binary search collections. • Compare sequential search and • Learn how to evaluate the complexity of an binary search algorithm and compare between algorithms.  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 3  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 4 Array Example Array Example2 markArray markArray // Find the largest element in an array of ints // Find the index of the largest element in an index 50 0 array of ints 50 0 0 index 1 index 1 37 37 int markArray[] = {50, 37, 71, 99, 63}; int markArray[] = {50, 37, 71, 99, 63}; index 1 index 2 1 2 71 71 int index; int index; index 2 2 3 3 99 index 99 int max; int indexOfMax; 3 index 4 3 4 63 index 63 index = 0; index = 0; 4 4 index=5 index = 5 max = markArray[index]; indexOfMax = 0; for ( index = 1; index < markArray.length; index++) for (index = 1; index < markArray.length; index++) max indexOfMax if ( markArray[index] > markArray[indexOfMax]) if ( markArray[index] > max) 50 50 3 99 71 indexOfMax = index; 2 3 3 0 0 max = markArray[index]; System.out.println(indexOfMax); System.out.println(max);  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 5 Structural Programming and Data Structures University of Alberta 6 1

  2. Outline of Lecture 21 The Search Problem • Review the simple array examples • Given a container, find the index of a particular • Sequential search approach element, called the key. • Complexity of sequential search • Technique applies for vectors, arrays, files, etc. • Applications: information retrieval, database • Binary search approach querying, etc. • Complexity of binary search 0 1 2 3 4 5 6 7 8 9 • Compare sequential search and 30 25 50 10 95 75 30 70 55 60 80 binary search Element Collection sought for  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 7  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 8 Sequential Search Code Compare all elements of the collection until we find the key. Sequential Search /* a sequential search code (first tentative) */ public static int sequential_search( int data[], int key ) { • Compare the key to each element in turn, boolean found = false; int index = 0; until the correct element is found, and return its index. while ( !found) { if ( key == data[index] ) 0 1 2 3 4 5 6 7 8 9 found = true; else 25 50 10 95 75 30 70 55 60 80 index = index + 1; } 30 30 30 30 30 30 return index; }  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 9  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 10 Search Algorithm Element not found INPUT: data: array of int; key: int; • We must take into account that the key we OUTPUT: index : an int such that are searching for may not be in the array. data[index] == key if key is in data, or -1 if key is not stored in data. • In this case we must return a special index, Method : say -1. 1. index = 0; found=false; 2. While ( not found and index < data.length ) -1 0 1 2 3 4 5 6 7 8 9 check similarity data[index] and key 25 50 10 95 75 30 70 55 60 80 index = index + 1 3. if not found then index = -1; 35 35 35 35 35 35 35 35 35 35 35  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 11 Structural Programming and Data Structures University of Alberta 12 2

  3. /* a sequential search method */ public static int sequential_search( int data[], int key ) { Outline of Lecture 21 boolean found = false; int index = 0; • Review the simple array examples while ( !found && index < data.length ) { • Sequential search approach if ( key == data[index] ) found = true; • Complexity of sequential search else • Binary search approach index = index + 1; Revised Sequential } • Complexity of binary search Search Code • Compare sequential search and if (!found) index = -1; return index; binary search }  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 13  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 14 Complexity Analysis Worst, Best and Average cases • How efficient is this algorithm? • In fact, we usually have multiple • In general if we have an algorithm that does expressions: something with n objects, we want to express the time efficiency of the algorithm as a function of n . – the worst case complexity, – the best case complexity • Such an expression is called the time complexity – the average case complexity. of the algorithm. • In the case of search, we can count the number of comparison operations between the key and the elements.  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 15  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 16 Complexity of Sequential Search Outline of Lecture 21 • How many comparison operations are required for a sequential search of an n-element container? • Review the simple array examples • In the worst case � n. • Sequential search approach • In the best case � 1. • Complexity of sequential search • In the average case: + + + + + + • Binary search approach 1 2 3 ... ( 1 ) ( 1 ) n n n n = = 2 2 n n • Complexity of binary search • In this case, we say the complexity of Search is in • Compare sequential search and the order of n , denoted as O(n) . binary search • Can we improve this algorithm?  Dr. Osmar R. Zaïane, 2000  Dr. Osmar R. Zaïane, 2000 Structural Programming and Data Structures University of Alberta 17 Structural Programming and Data Structures University of Alberta 18 3

Recommend


More recommend