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.

Slides:



Advertisements
Similar presentations
Linear Lists – Array Representation
Advertisements

Data Structures ADT List
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Wednesday, 12/4/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/4/02  Questions?  Return Test #2  General discussion of HW #05  Introduction.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Simple Arrays COMP104 Lecture 11 / Slide 2 Arrays * An array is a collection of data elements that are of the same type (e.g., a collection of integers,characters,
Wednesday, 11/25/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/25/02  QUESTIONS??  Today:  More on sorting. Advanced sorting algorithms.  Complexity:
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Wednesday, 11/13/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/13/02  QUESTIONS??  Today:  More on searching a list; binary search  Sorting a list;
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Monday, 11/18/02, Slide #1 CS 106 Intro to CS 1 Monday, 11/18/02  QUESTIONS??  Today:  Hand back, discuss HW #4  Discussion of Lab 10  Exam #2 Friday.
Chapter 8 Arrays and Strings
CSE202: Lecture 14The Ohio State University1 Arrays.
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
Simple Arrays Programming COMP104 Lecture 12 / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g., a collection.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
Arrays.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Introduction - The Need for Data Structures Data structures organize data –This gives more efficient programs. More powerful computers encourage more complex.
Chapter 16: Searching, Sorting, and the vector Type.
Applications of Arrays (Searching and Sorting) and Strings
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
CS 2430 Day 1. Agenda Load NetBeans Introduction Syllabus Review some array operations.
DATA STRUCTURES LAB 1 TA: Nouf Al-harbi
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
 Introduction to Computer Science COMP 51 – Fall 2012 – Section 2 Structures.
CS101 Computer Programming I Chapter 4 Extra Examples.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
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.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Fundamental Programming Fundamental Programming More on Repetition.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
114 3/30/98 CSE 143 Collection ADTs [Chapter 4] /30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CS 1430: Programming in C++.
Chapter 16: Searching, Sorting, and the vector Type.
CS 1428 Final Exam Review. Exam Format 200 Total Points – 60 Points Writing Programs – 45 Points Tracing Algorithms and determining results – 20 Points.
Searching Arrays Linear search Binary search small arrays
Chapter 16: Searching, Sorting, and the vector Type
Chapter 5: Loops and Files.
CS 1430: Programming in C++.
Arrays … The Sequel Applications and Extensions
Fundamental Programming
Applications of Arrays
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)