Outline lecture Revise arrays Entering into an array

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

2. Getting Started Heejin Park College of Information and Communications Hanyang University.
Last week lecture Loops (lets look at for loop again) Dry Runs
Zabin Visram Room CS115 CS126 Searching
DT266/2 Information Systems COBOL Revision. Chapters 1 & 2 Hutty & Spence Divisions of a Cobol Program Identification Division Program-ID. Environment.
Chapter 7: Arrays In this chapter, you will learn about
Recursion.
Briana B. Morrison Adapted from William Collins
College of Information Technology & Design
CS101: Introduction to Computer programming
Standard Algorithms Find the highest number. ! Your name and today’s date ! Find the maximum Dim numbers(20) As Integer.
MATH 224 – Discrete Mathematics
CSE Lecture 3 – Algorithms I
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
CS0007: Introduction to Computer Programming Array Algorithms.
HST 952 Computing for Biomedical Scientists Lecture 9.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Chapter 19: Searching and Sorting Algorithms
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Programming with Collections Collections in Java Using Arrays Week 9.
Introduction to Computers and Programming Lecture 8: More Loops New York University.
Data Structures Using Java1 Chapter 8 Search Algorithms.
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
Hash Tables1 Part E Hash Tables  
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Elementary Data Structures and Algorithms
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Data Structures Using Java1 Chapter 8 Search Algorithms.
A Level Computing#BristolMet Session Objectives U2#S6 MUST identify different data types used in programming aka variable types SHOULD describe each data.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
 Data is always stored in a logical way so that it can be accessed efficiently. Ex:A telephone directory  The way data is stored is called the structure.
Chapter 19: Searching and Sorting Algorithms
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1)
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Week 7. Lecture 2 Functions, Arrays, PHP&MySQL. Function with More than one argument and a return statement For a function to return a value, the return.
Chapter 7 Array processing. Objectives To introduce arrays and the uses of arrays To develop pseudocode algorithms for common operations on arrays To.
Arrays.
Lecture 4 on Data Structure Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Searching : Linear search Searching refers to the operation of finding.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
Array (continue).
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 6: Arrays: Lists and Tables
CSC 211 Data Structures Lecture 13
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Data Structure Introduction.
Data Structures Types of Data Structure Data Structure Operations Examples Choosing Data Structures Data Structures in Alice.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
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.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
IS2802 Introduction to Multimedia Applications for Business Lecture 4: JavaScript, Loops, and Conditional Statements Rob Gleasure
CHAPTER EIGHT ARRAYS © Prepared By: Razif Razali1.
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.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Applied Discrete Mathematics Week 2: Functions and Sequences
Outline lecture Revise arrays Entering into an array
Data Structures I (CPCS-204)
Chapter 7 Arrays.
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Revision of C++.
Presentation transcript:

Outline lecture Revise arrays Entering into an array Totalling values in an array Sequential search

What is an Array We are interested in one-dimensional arrays An array has a fixed number of elements and all elements are of the same type Each box has an index which in java and C++ starts with 0 Here is an array which holds the ages of 10 people 0 1 2 3 4 5 6 7 8 9 32 34 56 32 12 67 21 34 21 45

Lets look at this array in detail What do you notice about the array ? 0 1 2 3 4 5 6 7 8 9 32 34 56 32 12 67 21 34 21 45 Lets look at this array in detail What do you notice about the array ? Is the data organised in any particular way?

Examples of Arrays Draw an array of 20 elements which contains student marks – what type will it be ? Draw an array of 15 elements which contains student grades ranging form A-E – what type will it be?

Entering data into an array When you enter data into an array we use a loop What type of loop do you think we will use? Hint – we know the number of elements there are in the array Use a counter to keep track of how many elements are entered into the array

Entering data into an array Read Algorithm Loop : R = 1 to 10 Enter A( R) Loop end The number of elements in array is 10 The counter of loop (R ) allows the computer to increase the element number by 1 each time a piece of data is entered into a memory location The computer can find a particular element in the array by using the reference number index represented by R R = loop counter A(R ) = element R in the A array R 10 1 Enter A(R ) R B

Accumulating the elements of array Calc You may need to sum the elements of an array Initialise the sum variable which contains the total to zero The instruction Sum = Sum + A(R ) tells the computer to add the valve of element A(R ) to the old valve of the sum (Sum) and to store the result into sum memory location (Sum) Algorithm Loop : R = 1 to 10 sum = Sum + A( R) Loop end The number of elements in array is 10 The counter of loop (R ) allows the computer to increase the element number by 1 each time a piece of data is entered into a memory location Sum = Sum of the elements of A A(R ) = element R in the A array R 5 1 Sum = Sum + A(R ) R B

Why Search ? Everyday life -We always Looking for something – builder yellow pages, universities, hairdressers Computers can search World wide web – Spreadsheet – Databases – Large records – 1000s takes time - many comparison slow system – user wont wait long time

Search Algorithms Different types – Sequential Search, Binary Search Discuss - search algorithms - analyze them Analysis of the algorithms enables programmers to decide which algorithm to use for a specific application

Some observations – Key each item in a data set - special member that uniquely identifies the item in the data set For e.g University - a data set which contains student records – student ID uniquely identifies each student This unique member of the item is called Key of the item

Some observations - Key Where can Keys of the item in a data set be used in ? When searching the data set - for a particular item, what do we do ? compare the key of the item for which we are searching - with the keys of the items in the data set – e.g if we looking particular student id in a list of student id exam results

Analysis of Algorithms In addition to describing the algorithms – analyse them What does analysis of algorithms involve ? key comparisons Moreover – the number of key comparisons refers to - the number of times the key of the item (in search & sort algorithms) is compared with the keys of the items in the list

Target ? ?

Sequential search (linear search) Problem :- we want to search for a given element in a list. Do we know where the target element occurs?.

Sequential search (linear search) We can have no guarantees about the order of elements in the list if (for example) insertions have been under a user’s control. Search starts at the first element in the list and continues until either the item is found in the list - or entire list is searched

A simple search method is as follows: p193 Algorithm R = 1 While Array(R ) <> Target R = R + 1 WhileEnd If R > N Then Print “Element Not Found” Else Print Array (R ) A simple search method is as follows: p193

Flowchart for Sequential Search P193 While Target Array(R ) and R<=N R= R+1 If R > N F T Print Element Not Found Print Array(R )

Flowchart for Sequential Search using for loop J 10 1 If target not found J= J+1 If Target found F T Print Element Not Found Print Array(R )

Task :- in pairs Draw an array with 10 integer values Populate the array with 10 elements 1 5 6 3 7 8 9 5 2 3 Write an algorithm using the sequential search technique to find the target 6 in the array Draw a flowchart

Demonstration http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/LSearch.html The more comparisons he longer it takes – time!

Sequential search (linear search) If the search item is found, its index (that is its location in array) is returned. If search is unsuccessful, -1 is returned Note the sequential search does not require the list elements to be in any particular order

Sequential Search Analysis : Task – in pairs For each iteration in the loop, the search item is compared with an element in the list,and a few other statements are executed. Loop terminates when search item is found in list Therefore the execution of the other statement in loop is directly related to the outcome of the key comparisons

Sequential Search Analysis When analysing a search algorithm, - count the number of key comparisons –why ? because this number gives us the most useful information, this criterion for counting the number of key comparisons can be applied equally well to other search algorithms

Task :- in pairs – how many comparisons ? An iterative sequential search of an array that {a) finds its target; (b) does not find its target (a) A search for 8 (b) A search for 6 Look at 9 Look at 9 9 5 8 4 7 9 5 8 4 7 8 <> 9 , so continue searching … 6 <> 9 , so continue searching … Look at 5 Look at 5 9 5 8 4 7 9 5 8 4 7

Sequential Search Analysis Suppose the search item is in the list Then number of key comparisons depends on where in the list the search item is located. If search item is first element of L – make one key comparison – best case Worst case search item is the last item – algorithm makes n comparisons

Sequential Search Analysis If the search item Target , is the first element in list, how many one comparisons are needed? if target is second, how many one comparisons are needed? So if the target is the kth element in the list k comparisons are made