1 CSE1301 Computer Programming Lecture 31: List Processing (Search)

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

CSE Lecture 3 – Algorithms I
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
HST 952 Computing for Biomedical Scientists Lecture 10.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4)
11.2 Complexity Analysis. Complexity Analysis As true computer scientists, we need a way to compare the efficiency of algorithms. Should we just use a.
the fourth iteration of this loop is shown here
FIT Objectives By the end of this lecture, students should: understand iteration over arrays searching and sorting in arrays the differences.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
1 CSE1301 Computer Programming: Lecture 28 List Sorting.
1 CSE1301 Computer Programming Lecture 32: List Sorting.
CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation.
Concept of Basic Time Complexity Problem size (Input size) Time complexity analysis.
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
Searching Arrays Linear search Binary search small arrays
Analysis of Algorithm.
Elementary Data Structures and Algorithms
Abstract Data Types (ADTs) Data Structures The Java Collections API
COMP s1 Computing 2 Complexity
1 MT258 Computer Programming and Problem Solving Unit 9.
Week 2 CS 361: Advanced Data Structures and Algorithms
Analysis of Algorithms
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CSC 205 Java Programming II Algorithm Efficiency.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
1 Lecture 5 Generic Types and Big O. 2 Generic Data Types A generic data type is a type for which the operations are defined but the types of the items.
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Algorithms and data structures: basic definitions An algorithm is a precise set of instructions for solving a particular task. A data structure is any.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Introduction to Analysis of Algorithms CS342 S2004.
Algorithm Analysis (Big O)
ANALYSING COSTS COMP 103. RECAP  ArrayList: add(), ensuring capacity, iterator for ArrayList TODAY  Analysing Costs 2 RECAP-TODAY.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
Searching Topics Sequential Search Binary Search.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 Analysing Costs COMP 103.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Section 1.7 Comparing Algorithms: Big-O Analysis.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
Complexity Analysis (Part I)
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Introduction to complexity
Computer Science 112 Fundamentals of Programming II
COMP 53 – Week Seven Big O Sorting.
Searching an Array: Binary Search
Teach A level Computing: Algorithms and Data Structures
Building Java Programs
Programming and Data Structure
Programming and Data Structure
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
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Presentation transcript:

1 CSE1301 Computer Programming Lecture 31: List Processing (Search)

2 Topics An array as a list Searching –Linear search –Binary search (sorted list) Efficiency of an algorithm

3 Arrays as Lists An array –stores several elements of the same type –can be thought of as a list of elements: int a[8]

4 Linear Search Problem: determine if an element is present in an array Method: –start at one end –look at each array element until the sought element is found Also called sequential search

5 int isPresent (int *arr, int val, int N) { int count; for (count=0;count<N;count++) { if (arr[count]==val) { return 1; } return 0; } isPresent (array, val, arraySize) { set count to 0 while ( not yet processed all array elements ) { if ( current array element is val ) { return true } increment count } return false } Linear Search: Algorithm and Code

6 How would you modify the program so that it returns the position of the sought item (i.e., findPosition rather than isPresent )? How would you indicate “not found”? Linear Search -- Exercise

7 What does Efficiency Mean? Algorithm: a set of instructions describing how to do a task Program: an implementation of an algorithm Complexity theory describes the time and space used by an algorithm The time and space requirements of an algorithm enable us to measure how efficient it is

8 Types of Computer Resources Time: elapsed period from start to finish of the execution of an algorithm Space (memory): amount of storage required by an algorithm Hardware: physical mechanisms required for executing an algorithm

9 How to Measure Efficiency? Use your watch? Use the computer clock? Not a good idea, because: –What if you run your program on different computers? –Your program may also wait for I/O or other resources –While running a program, a computer performs many other computations –Depends on programming/coding skill

10 Abstract Notion of Efficiency We are interested in the number of steps executed by an algorithm –step  execution of an instruction The running time of an algorithm is proportional to the number of steps executed by the algorithm Running time is given as a function of the size of the input data: “Big-O Notation”

11 Linear Search Efficiency What is the size of the input data? –The size of the array being searched is N What is the time complexity of this algorithm? –Each time through the loop we perform 2 comparisons  count<N  arr[count]==val 1 increment and 1 assignment  count++ Total: 4 operations So we execute approximately f(N)=4*N ops.

12 Big-O Notation Big-O notation is a function of the size of the input Example: –Input: N integers –Algorithm complexity: Constant O(1) Logarithmic O(log N) Linear O(N) n log(n) O(N log N) Quadratic O(N 2 ) Cubic O(N 3 ) Exponential O(2 N )

13 Calculating Complexity with the Big-O Notation Simplify and choose the highest term Examples:  2 + 3N + 10N + 3N = 3N N  O(N 2 )  40N + N 3  O(N 3 )  25  O(1)

14 Linear Search Efficiency (cont) Best case? –Wanted item is at the start of the list  1 (initialization) + 4 operations Worst case? –Wanted item is not found  1 + 4N + 2 (last increment and test)  O(N) Average case? –Average of [Wanted item is in position 1, 2, …, N ] 

15 Binary Search Can we do any better than linear search? Example: –How do you find a word in the dictionary, or a number in the phone directory? –Assume that the array is sorted and use bisection

16 Binary Search (cont) If ( value == middle element ) value is found else if ( value < middle element ) search left-half of list with the same method else search right-half of list with the same method

17 Case 1: val == a[mid] val = 10 low = 0, high = a: lowhigh Binary Search -- Example 1 mid mid = (0 + 8) / 2 = 4 10

18 Case 2: val > a[mid] val = 19 low = 0, high = 8 mid = (0 + 8) / 2 = 4 Binary Search -- Example a: mid lowhigh new low new low = mid + 1 =

19 Case 3: val < a[mid] val = 7 low = 0, high = 8 mid = (0 + 8) / 2 = 4 Binary Search -- Example a: mid lowhigh new high new high = mid - 1 =

20 val = 7 Binary Search -- Example 3 (cont) a: a: a:

21 int isPresent(int *arr, int val, int N) { int low = 0; int high = N - 1; int mid; while ( low <= high ) { mid = ( low + high )/2; if (arr[mid]==val) { return 1; } else if (arr[mid] < val) { low = mid + 1; } else { high = mid - 1; } return 0; } isPresent (array, val, arraySize) { set low to first array position set high to last array position while ( low < = high ) { set mid to half of low + high if (array element in mid is val ) { return true } else if ( middle value < val ) { set low to mid + 1 } else { set high to mid - 1 } return false } Binary Search -- Algorithm and Code

22 Binary Search: Exercise What happens if the sought value is not in the list? How would you modify the code so that it returns the position of the sought item (i.e., findPosition rather than isPresent )?

23 Binary Search Efficiency What is the size of the input data? –The size of the array being searched is N What is the time complexity of this algorithm? –Each time through the loop we perform 3 comparisons 3 arithmetic operations 2 assignments Total: 8 operations

24 Best case? –item is in the middle –5 operations  O(1) Worst case? – item is not found – 8  log 2 N operations  O(log 2 N) Average case? –O(log 2 N) Binary Search Efficiency (cont)

25 Calculating the Worst Case Complexity After 1 bisectionN/2 items After 2 bisectionsN/4 = N/2 2 items... After i bisectionsN/2 i =1 item i = log 2 N

26 Exercise Problem: How would you implement linear search or binary search over an array of structs? Method: The array must be sorted by ID, name or mark, depending on the search key

27 Exercise (cont) struct studentRec { int IDNumber; char name[NAMELEN]; float mark; }; typedef struct studentRec Student; struct classRec { int count; Student student[MAX_STUDENTS]; }; typedef struct classRec ClassType; ClassType class; Student findStudent(ClassType *class, int IDNum) {... }

28 Notes on Searching Linear search can be done on any (sorted or unsorted) list, but it is inefficient Binary search –requires a list to be sorted –is more efficient Sorting a list: Lecture 31

29 Reading Forouzan and Gilberg, Section 8.6 Knuth, Sorting and Searching, Section (ed. 2)