Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Growth-rate Functions
MATH 224 – Discrete Mathematics
CSE Lecture 3 – Algorithms I
CompSci Searching & Sorting. CompSci Searching & Sorting The Plan  Searching  Sorting  Java Context.
CS0007: Introduction to Computer Programming Array Algorithms.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Cmpt-225 Algorithm Efficiency.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithm.
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
COMP s1 Computing 2 Complexity
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Week 2 CS 361: Advanced Data Structures and Algorithms
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 16: Searching, Sorting, and the vector Type.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Sorting HKOI Training Team (Advanced)
Analysis of Algorithms
Algorithm Evaluation. What’s an algorithm? a clearly specified set of simple instructions to be followed to solve a problem a way of doing something What.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
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.
Arrays Tonga Institute of Higher Education. Introduction An array is a data structure Definitions  Cell/Element – A box in which you can enter a piece.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
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.
CSCI 51 Introduction to Programming March 12, 2009.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
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.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Intro To Algorithms Searching and Sorting. Searching A common task for a computer is to find a block of data A common task for a computer is to find a.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
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.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Chapter 16: Searching, Sorting, and the vector Type.
2 - Arrays Introducing Arrays Declaring Array Variables, Creating Arrays, and Initializing Arrays Ordered and Unordered Arrays Common Operations: Insertion,
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
CMPT 438 Algorithms.
Chapter 16: Searching, Sorting, and the vector Type
Data Structures I (CPCS-204)
Modeling with Recurrence Relations
CSC 222: Object-Oriented Programming
Introduction Algorithms Order Analysis of Algorithm
Searching and Sorting 1-D Arrays
Searching CLRS, Sections 9.1 – 9.3.
Algorithmic complexity: Speed of algorithms
Algorithmic complexity: Speed of algorithms
slides created by Ethan Apter
Presentation transcript:

Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching

Tables: Arrays for storing lists of homogenous information  Simple lists (1D table)  Complex matrices (2D and more) Operations  Initialise/Build  insert  delete  modify  traverse (display, accumulate, max, min)  search/find  sort

A B C D E parallel arrays are two or more arrays in which elements with corresponding indexes are related Technique: Parallel Arrays for storing heterogeneous lists * Parallel arrays are used when related data is of different data types. ID grade

Example to demonstrate use of parallel and arrays of strings Requirements: Load prisoner data into arrays Filter data by birth place specified by user. Data source is prisoner data file. 1. Create file variables, Parallel arrays 2. initialise file variables 3. Load data into arrays 4. Ask user for a birth place 5. Go through relevant array and pick out and save only those that match user input 1.Display to screen and save to file filtered data.

Searching  Linear or sequential search  Binary search or divide and conquer! Demo 2 to illustrate performance

Performance  Linear search was slow –A bad algorithm?  Binary search was fast –A good algorithm?

Measures of performance  Speed (Time it takes to do job) –Not very helpful –Varies from machine to machine. –No reference to numbers of items processed  E.g. suppose Method A performs 2 time faster than method B. But if you increase the number of items by 50% A performs 3 times faster than B and if you decrease the number of items by 50% they both perform the same.  Need a machine independent measure that relates performance to the amount of items (N) to be processed.

Big O Estimates  An estimate that is machine independent and related the number of steps needed to complete a task to the number of items to be processed.  steps counted are usually number or comparisons (ifs). The number of exchanges (swaps) are calculated if sorting

Linear search vs. Binary search  Linear is O(N)  Binary is O(logN)

What does O(N) and O(logN) mean?  Linear search –A search of a list of N items will on average take N/2 comparisons. –i.e. on average the item will be the middle item in the list.  Convention is to set all coefficients of N to 1. i.e. coefficient of N above is ½ so big O estimate for linear search is O(N)  The time it takes to find an element is directly proportional to N.

Other Estimates  Other algorithms have different Big O estimates, e.g. O(log N), O(N 2 ).  Generally an excellent Big O estimate is O(log N) algorithms  Average performance is O(N)  very poor performance is O(N 2 ) and above

Number of items N O(log N) O(N) O(N 2 ) Number of steps

Why is binary search so much faster?  It is an algorithm that is designed for ORDERED LISTS.  It only works with an ordered list.  If your list is not ordered then you have a choice –either sort it and use binary search or –use linear search  More on binary search later

Basic lists ordered or unordered Operations  Insertion of elements to an unordered list.  Deletion of elements from an unordered list.  Insertion of elements to an ordered list.  Deletion of elements from an ordered list.

An unordered list int A[7] = {12, 4, 7, 3}; int nItems = 4; max items is 7! nItems now number of elements used index

Insertion A[nItems] = 9; nItems++; new item nItems happens to refer to next free element index index

Insertion new item index nItems now = 5

Deletion need to shuffle all subsequent items back one and decrease nItems by one item ItemIndex = 2 to be removed index

Deletion for (j = ItemIndex; j < nItems;j++) A[j] = A[j+1]; nItems--; item to be removed Loop shuffles back elements index

Deletion End result nItems now back to 4, value A[4] still 9, but will be overwritten next index

Analysis  Insertion takes 1 step irrespective of nItems –Big O estimate for insertion is therefore O(1)  Deletion. on average you will need to shuffle half the items down so this takes N/2 steps. –Big O estimate for deletion is therefore O(N). Remember set coeffs of N to 1

An Ordered list int A[7] = {4, 14, 27, 33}; int nItems = 4; index nItems no longer refers to next free element index

Insertion need to shuffle this and all subsequent items forward by one to make space, and increase nItems by one newItem = 9 must be inserted here at ItemIndex = index

Insertion newItem = 9; for (j = 0; j > nItems; j++) if (A[j] < newItem) break; ItemIndex = j; for (j = nItems; j > ItemIndex;j--) A[j] = A[j-1]; A[ItemIndex] = newItem; nItems++; End result index

Deletion need to shuffle all subsequent items back one and decrease nItems by one item ItemIndex = 2 to be removed index

Deletion for (j = ItemIndex; j < nItems;j++) A[j] = A[j+1]; nItems--; item to be removed Loop shuffles back elements index

Deletion End result index

Analysis  Need to search through half to find insertion point, takes N/2 comparisons on average.  Insertion on average you will need to shuffle half the items up by one to make space for the new item, this takes N/2 steps –Big O estimate for insertion is therefore N/2 + N/2 = O(N)  Deletion. on average you will need to shuffle half the items down so this takes N/2 steps. –Big O estimate for deletion is therefore still O(N).

Summary  For an unordered list –insertion is O(1) very very fast –deletion is O(N) quite slow  For an ordered list –insertion is O(Nquite slow. –deletion is O(N)quite slow.

Going back to our searches  A linear search needs to search on average half the items therefore its Big O estimate is O(N)  Binary search was much faster it has a big O estimate of O(log N)  What does this mean?

Binary search algorithms  Divide and conquer  Basic idea is that given an ordered list –find the mid point in the list and compare the target key with the element at this point –if the key is bigger than the mid point element then go to the mid point from the current mid point and the current end and check again. –if the key is smaller than the mid point element then go to the mid point from the current mid point and the current start and check again –Keep repeating until you find or fail to find target.

Step 1 of find target = lower=0 upper= 6 (mid = lower+upper)/2 (0 + 6)/2 = 3 Check if A[3] == 50 ….. NO then divide data since 50 > 33 only look from this point on. We have effectively removed the lower part of the list from the search index

Step 2 of find target = lower=4 upper= 6 (mid = lower+upper)/2 (4 + 6)/2 = 5 lower set to previous mid + 1, mid set to 5 Check if A[5] == 50 ….. YES found target index

Binary search took 2 steps rather than 6 steps needed by linear search

Brief analysis of binary search quite complicated!  Each step of the Binary search successively eliminates HALF the elements from the search. –Hence DIVIDE AND CONQUER  What we want is a figure for the number of steps needed for any given number of items to be searched

Table comparing maximum steps needed for binary search with average steps needed for linear search. NBinaryLinear ,000145, , ,000 1,000, ,000 You could check by hand by repeatedly dividing the range in half. How many steps does it take to zoom in on a range containing only one element?

 A list of 100 items then will take at most 7 steps to locate an element.  A linear search would take on average 50 steps (but could take 100.)

What is the maximum range (N) that can be searched in a given number of steps? StepsNEquivalent Power

 So if we need to search 100 items 6 steps only sufficient for 64 items, we need 7 steps to search entire list.  Remember we want an equation to tell us the number of steps needed for any N.

 Notice that each increment in step doubles the range that can be searched.  This range is expressed as a power series.  where N = 2 steps.  we want an equation specifying steps in terms of a range. StepsNEquivalent Power

NASTY BIT OF MATHS (DON’T PANIC!!!) JUST TAKE MY WORD FOR IT.  The reverse of a power is called a logarithm.  So to reverse equation N = 2 steps. we take logarithms.  log N = steps x Log 2  log N = steps –since log 2 = 1.  Therefore Steps = Log N –hence the Big O estimate for binary search is O(log N)  I said that there was more theory this semester!

Conclusion  There are many algorithms that adopt this divide and conquer approach. –Algorithms that do this are very fast.  The down side is that you need an ordered list in the first place.  Sorting a list can be time consuming.  Maintaining an ordered list is time consuming.  If you do not have many items to process then a simple unordered list may be preferable.  Unordered lists are easier to maintain.

Big O  Provides the most convenient way of comparing algorithms performance BestO(1) O(log N) O(N) WorstO(N 2 ) and above

Next Week SORTING  Bye Bye