Linked Lists CSC 172 SPRING 2002 LECTURE 3 Agenda  Average case lookup – unsorted list & array  Amortized insert for array  Sorted insert  Binary.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Topic 14 Searching and Simple Sorts "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The.
MATH 224 – Discrete Mathematics
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Searching and Sorting Arrays. Searching in ordered and unordered arrays.
Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.
Time Complexity Intro to Searching CS221 – 2/20/09.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Binary Search Introduction to Trees. Binary searching & introduction to trees 2 CMPS 12B, UC Santa Cruz Last time: recursion In the last lecture, we learned.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Algorithm Efficiency and Sorting
Binary Search Example CSC 172 SPRING 2004 LECTURE 5.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
Search Lesson CS1313 Spring Search Lesson Outline 1.Searching Lesson Outline 2.How to Find a Value in an Array? 3.Linear Search 4.Linear Search.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Applications of Arrays (Searching and Sorting) and Strings
Analysis of Algorithms
Lecture 12. Searching Algorithms and its analysis 1.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
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.
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.
SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 9: Searching Data Structures.
CSC 211 Data Structures Lecture 13
Searching and Sorting Arrays. Searching in ordered and unordered arrays.
Order Statistics David Kauchak cs302 Spring 2012.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Chapter 14: Searching and Sorting
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Review Quick Sort Quick Sort Algorithm Time Complexity Examples
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
Section 1.7 Comparing Algorithms: Big-O Analysis.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Lecture No.45 Data Structures Dr. Sohail Aslam.
CSC 222: Object-Oriented Programming
Recitation 13 Searching and Sorting.
Search Lesson Outline Searching Lesson Outline
Searching an Array: Binary Search
Data Structures and Algorithms for Information Processing
Sorting by Tammy Bailey
Adapted from Pearson Education, Inc.
Algorithm design and Analysis
searching Concept: Linear search Binary search
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Searching CLRS, Sections 9.1 – 9.3.
Data Structures: Searching
Amortized Analysis and Heaps Intro
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

Linked Lists CSC 172 SPRING 2002 LECTURE 3

Agenda  Average case lookup – unsorted list & array  Amortized insert for array  Sorted insert  Binary search

Workshop sign-up Still time : Dave Feil-Seifer Ross Carmara

Average Case Analysis  Consider “lookup” on either a linked list or array  We said “n” run time  Under what conditions do we really get “n”  What do we get when the element is the first in list?  Second.. ?  Third.. ?  Middle.. ?  Last?

Lookup: Array public boolean lookup(Object o){ for (int j = 0 ; j < length;j++) if (datum[j].equals(o)) return true; return false; }

Lookup: Linked List public boolean lookup(Object o){ Object temp = head; while (temp != null){ if ((temp.data).equals(o)) return true; temp = temp.next; } return false; }

Average case  On a list of length n in how many different places can the item be?  If the list is unorganized (unsorted/random) what is the chance that the item is at any one location?  What is the probability of getting “heads” on a coin toss?  What is the probability of getting “3” rolling a die?

Average case analysis  For all the possible locations, multiply the chance of dealing with that location, times the amount of work we have to do at it. First location work = (1/n) * 1 Second location work = (1/n) * 2 Third location work = (1/n) * 3 …. But we have to add them all up

Average case analysis Expected work = (1/n)*1 + (1/n)* (1/n)*n Expected work = (1/n)*(1+2+…+n) Expected work = (1/n)*

 Prove  Then, Expected work == (1/n)*(n(n+1)/2)) == (n+1)/2 Or about n/2 So, proof is important: Thus, chapter 2 In order to calculate expected work

Amortized Analysis Amortize? to deaden? Latin : ad-, to + mortus dead Modern usage: payment by installment - sort of like a student loan, where you... never mind The idea is to spread payments out

Amortized analysis  Spread the “cost” out over all the “events”  Consider insert() on an array  We know the worst case is when we have to expand()  Cost of “n”  But, we don’t have to do this every time, right

Cost of inserting 16 items – array[2] capacity

Cost of inserting 16 items – array[2]

Cost of inserting 16 items – array[2] So, what is the average (amortized) run time? We will formalize this proof Later in the course.

Recap  Insert on a linked list?  Constant - 1  Insert on an array?  Worst case n  Average case constant – 2  Lookup on a list?  n/2  Lookup on an array  n/2  Can we do better?

What if we kept the array sorted?  How do we do it?  How much does it cost?  Imagine inserting “5” into  We have to “shift” (could you write this?)

So, suppose we kept the list sorted  Each insert now costs “n”  But what happens to lookup?

Guessing Game  I’m thinking of a number 1<=x<=64  I’ll tell you “higher” or “lower”  How many guesses do you need?  29  47 33 You know with “higher” or “lower” you can divide The remaining search space in half (i.e. log 2 (64)==6)

Binary Search  Recursively divide the array half until you find the item (if it is sorted).

Lookup on sorted array public boolean lookup(Object o) { return lookupRange(Object o,0,length-1); }

LookupRange: Binary Search public static void lookupRange(Object o, int lower,int higher){ if (lower > higher) return false; else { int mid = (low + high)/2; if (datum[mid].compareTo(o) < 0) return lookupRange(o,lower,mid); else if (dataum[mid].compareTo(o) > 0) return lookupRange(o,mid+1,higher); else return true; // o == datum[mid] }

So,  For the price of “n” instead of “1” on insertion We can go from “n/2” to log 2 (n) on lookup.  Which is “better”?  Constant insert & n lookup  n insert & log 2 (n) lookup

All very well and good  But can it help me get a date?  YES!  In order to get a date you have to look up someone’s phone number in the phone book.  You can save time by using binary search.