CSc 110, Spring 2017 Lecture 39: searching.

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

Search algorithm In computer science, a search algorithm is an algorithm that takes a problem as input and returns a solution to the problem, usually after.
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
CSE 143 Lecture 5 Binary search; complexity reading: slides created by Marty Stepp and Hélène Martin
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Building Java Programs Chapter 13 Searching reading: 13.3.
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.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Computer Science Searching & Sorting.
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
CSC 211 Data Structures Lecture 13
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.
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.
Binary search and complexity reading:
CSE 143 Lecture 16 Sorting reading: 13.1, slides created by Marty Stepp
Building Java Programs Chapter 13 Searching and Sorting Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 13 Lecture 13-1: binary search and complexity reading:
Building Java Programs Chapter 13 Sorting reading: 13.3, 13.4.
Searching and Sorting Searching algorithms with simple arrays
Lecture 25: Searching and Sorting
Searching and Sorting Algorithms
CSc 110, Autumn 2016 Lecture 36: searching.
CSCI 104 Sorting Algorithms
Searching Given a collection and an element (key) to find… Output
Lecture 14 Searching and Sorting Richard Gesick.
Adapted from slides by Marty Stepp and Stuart Reges
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
CSc 110, Autumn 2017 Lecture 37: searching and sorting
Teach A level Computing: Algorithms and Data Structures
slides created by Marty Stepp and Hélène Martin
Adapted from slides by Marty Stepp and Stuart Reges
Searching CSCE 121 J. Michael Moore.
Searching and Sorting Linear Search Binary Search ; Reading p
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 14: binary search and complexity reading:
CSE 143 Lecture 5 Binary search; complexity reading:
Based on slides created by Ethan Apter & Marty Stepp
Building Java Programs
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs
CSc 110, Spring 2017 Lecture 39: searching and sorting
Lecture 11 Searching and Sorting Richard Gesick.
Lecture 15: binary search reading:
CSE 154 Sorting reading: 13.3, 13.4
Adapted from slides by Marty Stepp and Stuart Reges
Search,Sort,Recursion.
slides created by Marty Stepp
slides created by Marty Stepp and Ethan Apter
slides created by Marty Stepp
slides created by Marty Stepp
slides created by Marty Stepp
CSE 143 Sorting reading: 13.3, 13.4.
Building Java Programs Chapter 13
Adapted from slides by Marty Stepp and Stuart Reges
CSE 373 Data Structures and Algorithms
slides created by Marty Stepp and Hélène Martin
Building Java Programs
Building Java Programs
CSE 373 Sorting 1: Bogo Sort, Stooge Sort, Bubble Sort
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
Stacks, Queues, ListNodes
Presentation transcript:

CSc 110, Spring 2017 Lecture 39: searching

Sequential search sequential search: Locates a target value in a list (may not be sorted) by examining each element from start to finish. Also known as linear search. How many elements will it need to examine? Example: Searching the list below for the value 42: index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value 30 56 20 68 36 -4 25 42 50 22 92 85 103 i

Sequential (linear) search sequential search: Even if the list is sorted, elements are examined in the way (one after the other). Example: Searching the list below for the value 42: index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 20 22 25 30 36 42 50 56 68 85 92 103 i

Sequential (linear) search Sequential search code: def sequential_search(my_list, value): for i in range(0, len(my_list)): if (my_list[i] == value): return i return -1 # not found Note that -1 is returned if the element is not found. index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 20 22 25 30 36 42 50 56 68 85 92 103

Sequential (linear) search For a list of size N, how many elements will be checked worst case? On average how many elements will be checked? A list of 1,000,000 elements may require 1,000,000 elements to be examined. The number of elements to check grows in proportion to the size of the list, i.e., it grows linearly.

Binary Search Binary search: a method of searching that takes advantage of sorted data. Consider a guessing game: Someone thinks of a number between 1 and 100. You must guess the number. On each round, you are told whether your number is low, high, or correct. Best strategy: use a first guess of 50 Eliminates half of the numbers immediately On each round, half the numbers are eliminated: 100 50 25 …

Binary search binary search: Locates a target value in a sorted list by successively eliminating half of the list from consideration. How many elements will it need to examine? Example: Searching the list below for the value 42: index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 20 22 25 30 36 42 50 56 68 85 92 103 Keep track of indices for a min, mid and max.

eliminate from min to mid (left half) Search for 42: Round 1. list[mid] < 42 eliminate from min to mid (left half) index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 20 22 25 30 36 42 50 56 68 85 92 103 min mid max

eliminate from mid to max (right half of what's left) Search for 42: Round 2. list[mid] > 42 eliminate from mid to max (right half of what's left) index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 20 22 25 30 36 42 50 56 68 85 92 103 min mid max

Search for 42: Round 3. list[mid] == 42 found! index 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 20 22 25 30 36 42 50 56 68 85 92 103 min mid max

Binary search runtime For a list of size N, it eliminates ½ until 1 element remains. N, N/2, N/4, N/8, ..., 4, 2, 1 How many divisions does it take? Suppose N = 1024 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 (10 divisions) 10 = log2 (1024) Suppose we double the number the number of elements. Suppose N = 2048 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 (11 divisions) 11 = log2 (2048)

Binary search runtime For a list of size N, it eliminates ½ until 1 element remains. N, N/2, N/4, N/8, ..., 4, 2, 1 How many divisions does it take? Suppose N = 1024 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 (10 divisions) Binary search examines a number of elements proportional to the number of divisions Think of it from the other direction: How many times do I have to multiply by 2 to reach N? 1, 2, 4, 8, ..., N/4, N/2, N Call this number of multiplications "x". 2x = N x = log2 N Binary search examines a number of elements proportional to log of N.

Binary search code # Returns the index of an occurrence of target in a, # or a negative number if the target is not found. # Precondition: elements of a are in sorted order def binary_search(a, target): min = 0 max = len(a) - 1 while (min <= max): mid = (min + max) // 2 if (a[mid] < target): min = mid + 1 elif (a[mid] > target): max = mid - 1 else: return mid # target found return -(min + 1) # target not found

Binary search index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 18 25 27 30 36 42 56 68 85 91 92 98 102 What do the following calls return when passed the above list? binary_search(a, 2) binary_search(a, 68) binary_search(a, 12) How many comparisons does each call do?

Comparing Binary vs. Sequential search Binary search vs Sequential search: number of items examined List size Binary search Sequential search 1 10 4 1,000 11 5,000 14 100,000 18 1,000,000 21

bisect bisect(list, value) bisect(list, value, min_index, max_index) from bisect import * # searches an entire sorted list for a given value # returns the index the value should be inserted at to maintain sorted order # Precondition: list is sorted bisect(list, value) # searches given portion of a sorted list for a given value # examines min_index (inclusive) through max_index (exclusive) bisect(list, value, min_index, max_index)

Using bisect # index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 a = {-4, 2, 7, 9, 15, 19, 25, 28, 30, 36, 42, 50, 56, 68, 85, 92} index1 = bisect(a, 42, 0, 16) # index1 is 11 index2 = bisect(a, 21, 0, 16) # index2 is 6 bisect returns the index where the value could be inserted while maintaining sorted order if the value is already in the list the next index is returned

Sorting sorting: Rearranging the values in a list into a specific order (usually into their "natural ordering"). one of the fundamental problems in computer science can be solved in many ways: there are many sorting algorithms some are faster/slower than others some use more/less memory than others some work better with specific kinds of data some can utilize multiple computers / processors, ... comparison-based sorting : determining order by comparing pairs of elements: <, >, …

Sorting algorithms bogo sort: shuffle and pray bubble sort: swap adjacent pairs that are out of order selection sort: look for the smallest element, move to front insertion sort: build an increasingly large sorted front portion merge sort: recursively divide the list in half and sort it heap sort: place the values into a sorted tree structure quick sort: recursively partition list based on a middle value other specialized sorting algorithms: bucket sort: cluster elements into smaller groups, sort them radix sort: sort integers by last digit, then 2nd to last, then ... ...

Bogo sort bogo sort: Orders a list of values by repetitively shuffling them and checking if they are sorted. name comes from the word "bogus" The algorithm: Scan the list, seeing if it is sorted. If so, stop. Else, shuffle the values in the list and repeat. This sorting algorithm (obviously) has terrible performance!

Bogo sort code # Places the elements of a into sorted order. def bogo_sort(a): while (not is_sorted(a)): shuffle(a) # Returns true if a's elements #are in sorted order. def is_sorted(a): for i in range(0, len(a) - 1): if (a[i] > a[i + 1]): return False return True