# Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching.

## Presentation on theme: "Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching."— Presentation transcript:

Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching  Looking ahead:  Friday: Lab 10  Next week: Review  Friday, Nov 22: Test #2  Reading: Chapter 10 (omit 10.7)  New files/handouts: ListFuncs.cpp

Monday, 11/11/02, Slide #2 Abstract Data Type List  By a list we mean a collection of data items,  All data is the same type  The data has a definite order (first, second,..., last)  Operations on list objects include  Declaring, initializing  Inputting, Outputting (Printing) the list  Inserting, deleting data items  Searching for a data item  Sorting the list

Monday, 11/11/02, Slide #3 Implementing ADT List with arrays  An array is a natural place to store the data items of a list. Objects needed include:  The name of the array (say, Items[ ])  The size of the array (say, MAXSIZE, a const)  The size of the list (say, Length <= MAXSIZE)  In HW #5 (after vacation), you’ll create a class to implement lists using arrays  For now, we’ll look at some list operations without using classes.  See file ListFuncs.cpp

Monday, 11/11/02, Slide #4 Initializing List Elements  If we know Length in advance, we can use a for-loop: for (int i = 0; i < Length; ++i) { Items[i] = 0; Items[i] = 0; //or cin >> Items[i]; //or cin >> Items[i]; //or some other way of //or some other way of //assigning values //assigning values} Index i starts at 0! Index i ends at Length-1!

Monday, 11/11/02, Slide #5 Initializing with while-loops  If Length is not known in advance, we can use a while-loop to initialize: int i = 0; int Val; while ((i > Val)) {Items[i] = Val; ++i;} Length = i; //now we know the length  WARNING: Watch out for off-by-one errors: executing the loop one too many or few times!

Monday, 11/11/02, Slide #6 Printing lists  Once a list has been initialized, we can print (or otherwise process) each element in order, using a for-loop: for (i = 0; i < Length; ++i) { cout << Items[i] << endl; cout << Items[i] << endl; // or do something else // or do something else // with Items[i] // with Items[i]}

Monday, 11/11/02, Slide #7 Linear Search Algorithm  Searching for an element (called the "key") in a list is a fundamental task, for which there are several different algorithms.  LINEAR SEARCH ALGORITHM:  We begin at one end of the list, and compare elements to the key, stopping when we either find the key, or reach the other end of the list.

Monday, 11/11/02, Slide #8 Linear Search Code; two alternatives int Index = 0; while (Index < Length && Key != List[Index]) {++Index; } //Set boolean flag for found/not found bool Found = (Index < Length); for (int Index = 0; Index < Length && Key != List[Index]; ++Index) {//do nothing!} bool Found = (Index < Length);

Monday, 11/11/02, Slide #9 Time Complexity of Linear Search  The time complexity of an algorithm is the approximate number of steps required to finish the job.  The number of times we compare the key to a list item gives us an idea of how long Linear Search can take.  At most n comparisons (where n is the length of the list);  On average, if key is in list, about n/2 comparisons.  Is there a better way???? (Yes! Later)

Download ppt "Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching."

Similar presentations