Searching/Sorting Introduction to Computing Science and Programming I.

Slides:



Advertisements
Similar presentations
CMPT 120 Algorithms Summer 2012 Instructor: Hassan Khosravi.
Advertisements

MATH 224 – Discrete Mathematics
Fundamentals of Python: From First Programs Through Data Structures
HST 952 Computing for Biomedical Scientists Lecture 9.
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
CMPS1371 Introduction to Computing for Engineers SORTING.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Recursion Chapter 11 Chapter 11.
Searching and Sorting I 1 Searching and Sorting 1.
Recursion Introduction to Computing Science and Programming I.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Complexity (Running Time)
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
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.
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
1 Searching and Sorting Linear Search Binary Search.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching 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.
 2006 Pearson Education, Inc. All rights reserved 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. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
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.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
CSC 211 Data Structures Lecture 13
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: ec2620m.htm Office: Tel 3049.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
Sorting.
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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 10 Lists 1.
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.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
CS 367 Introduction to Data Structures Lecture 11.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
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.
16 Searching and Sorting.
Searching and Sorting Algorithms
Searching.
Week 9 - Monday CS 113.
Searching Given a collection and an element (key) to find… Output
Lecture 14 Searching and Sorting Richard Gesick.
Chapter 10 Lists.
Teach A level Computing: Algorithms and Data Structures
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Algorithm design and Analysis
CSc 110, Spring 2017 Lecture 39: searching.
Chapter 8 Search and Sort
Chapter 10 Lists.
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Lecture 11 Searching and Sorting Richard Gesick.
MSIS 655 Advanced Business Applications Programming
Linear Search Binary Search Tree
24 Searching and Sorting.
Searching.
Searching.
Presentation transcript:

Searching/Sorting Introduction to Computing Science and Programming I

Searching Searching is the general problem of finding a specific item within a collection of items. Searching is the general problem of finding a specific item within a collection of items. We are only going to look at searching through a list of items. There are other data structures that may require different strategies We are only going to look at searching through a list of items. There are other data structures that may require different strategies

Searching Linear search: This is the simplest as most obvious way to search. Look through a list one item at a time until the item is found or the end of the list is reached. Linear search: This is the simplest as most obvious way to search. Look through a list one item at a time until the item is found or the end of the list is reached.

Searching def search(lst, val): """Find the first occurrence of val in lst. Return its index or -1 if not there. >>> search([0, 10, 20, 30, 40], 30) 3 >>> search([0, 10, 20, 30, 40], 25) """ for i in range(len(lst)): if lst[i]==val: # we only care about the first match, # so if we’ve found one, return it. return i # if we get this far, there is no val in lst. return -1

Searching What is the worst-case running time of linear search? What is the worst-case running time of linear search? If there are n items in the list then the algorithm may have to check each one so it has a running time of n. If there are n items in the list then the algorithm may have to check each one so it has a running time of n. This isn’t bad unless you need to do a lot of searching. In this case there’s a better algorithm that can be used, but needs the list to be sorted. This isn’t bad unless you need to do a lot of searching. In this case there’s a better algorithm that can be used, but needs the list to be sorted.

Searching If you think about looking up numbers in a phone book or words in a dictionary with millions of entries you can see how much having sorted information can speed up search. If you think about looking up numbers in a phone book or words in a dictionary with millions of entries you can see how much having sorted information can speed up search. To take advantage of a sorted list we can us a Binary Search algorithm. To take advantage of a sorted list we can us a Binary Search algorithm.

Searching Binary Search follows the same strategy that we used for the fast version of the guessing game. Binary Search follows the same strategy that we used for the fast version of the guessing game. Since the list is sorted, if we look at one item, we know that every item after it is larger, and every item before it is smaller. Since the list is sorted, if we look at one item, we know that every item after it is larger, and every item before it is smaller. So if we look at the item in the middle of the list we can know which half of the list the item we’re looking for would be in. So if we look at the item in the middle of the list we can know which half of the list the item we’re looking for would be in.

Searching def binary_search(lst, val): # keep track of the first and last possible positions. first = 0 last = len(lst)-1 while first <= last: mid = (first+last)/2 if lst[mid] == val: # found it return mid elif lst[mid] < val: # too small, only look at the right half first = mid+1 else: # lst[mid] > val # too large, only look at the left half last = mid-1 # if we get this far, there is no val in lst. return -1

Searching Due to this strategy of halving the possibilities with each try, the algorithm has a running time of log 2 n. Due to this strategy of halving the possibilities with each try, the algorithm has a running time of log 2 n. This is much faster than linear search, but to use binary search you must maintain a sorted list. Every time you insert an item it can take up to n steps. Therefore if you insert a lot of items compared to how many times you search, it may not be worthwhile. This is much faster than linear search, but to use binary search you must maintain a sorted list. Every time you insert an item it can take up to n steps. Therefore if you insert a lot of items compared to how many times you search, it may not be worthwhile. However, if you search more often than you insert items, binary search provides a significant speedup. However, if you search more often than you insert items, binary search provides a significant speedup.

Sorting There are an array of sorting algorithms that have been created, we’re going to look at a couple of the simpler ones to start. There are an array of sorting algorithms that have been created, we’re going to look at a couple of the simpler ones to start. In terms of running time, the best general sorting algorithms including the one used by Python’s list.sort() are n log(n). Those algorithms are more complex than what we’ll see today. In terms of running time, the best general sorting algorithms including the one used by Python’s list.sort() are n log(n). Those algorithms are more complex than what we’ll see today.

Sorting Selection Sort Selection Sort The basic strategy is to look through your list of items and find the one that goes first. Set it aside and look through the remaining items for the smallest of those. Add this to the first and repeat the process until all the items are sorted. The basic strategy is to look through your list of items and find the one that goes first. Set it aside and look through the remaining items for the smallest of those. Add this to the first and repeat the process until all the items are sorted. ‘Select’ the item that will go next each time you repeat ‘Select’ the item that will go next each time you repeat

Sorting In each iteration of the process we will swap the smallest element with the element whose position it will take. In each iteration of the process we will swap the smallest element with the element whose position it will take. 6, 34, 12, 7, 3, 26, 19 Smallest: 3 Swap: 3 and 6 6, 34, 12, 7, 3, 26, 19 Smallest: 3 Swap: 3 and 6 3, 34, 12, 7, 6, 26, 19 Smallest: 6 Swap: 6 and 34 3, 34, 12, 7, 6, 26, 19 Smallest: 6 Swap: 6 and 34 3, 6 12, 7, 34, 26, 19 Smallest: 7 Swap: 7 and 12 3, 6 12, 7, 34, 26, 19 Smallest: 7 Swap: 7 and 12 3, 6, 7 12, 34, 26, 19 Smallest: 12 Swap: 12 and 12 3, 6, 7 12, 34, 26, 19 Smallest: 12 Swap: 12 and 12 3, 6, 7, 12 34, 26, 19 Smallest: 19 Swap: 19 and 34 3, 6, 7, 12 34, 26, 19 Smallest: 19 Swap: 19 and 34 3, 6, 7, 12, 19 26, 34 Smallest: 26 Swap: 26 and 26 3, 6, 7, 12, 19 26, 34 Smallest: 26 Swap: 26 and 26 3, 6, 7, 12, 19, 26, 34 3, 6, 7, 12, 19, 26, 34 The last item will already be in the correct location The last item will already be in the correct location

Sorting Selection Sort Pseudocode Selection Sort Pseudocode for every element e from the list, for every element f from e to the end of the list, if f < smallest, set smallest to f swap smallest and e

Sorting def selection_sort(lst): ""“ Sort lst in-place using selection sort """ for pos in range(len(lst)): # get the next smallest in lst[pos] # find the next smallest small = lst[pos] # smallest value seen so far smallpos = pos # position of small in lst for i in range(pos+1, len(lst)): # check each value, searching for one # that’s smaller than the current smallest. if lst[i] < small: small = lst[i] smallpos = i # swap it into lst[pos] lst[pos], lst[smallpos] = lst[smallpos], lst[pos]

Sorting Running time of selection sort Running time of selection sort If you look at how many times the inner loop repeats for each position it takes n steps for the first, n-1 for the second, and so forth down to 1. So we have n + n-1 + n-2… steps which as we’ve seen before works out to n 2 /2 – n/2. If you look at how many times the inner loop repeats for each position it takes n steps for the first, n-1 for the second, and so forth down to 1. So we have n + n-1 + n-2… steps which as we’ve seen before works out to n 2 /2 – n/2. The running time is n 2 The running time is n 2

Sorting Insertion Sort Insertion Sort The basic idea is to keep a sorted group of elements and repeatedly insert the next element into the correct spot in the sorted group. The basic idea is to keep a sorted group of elements and repeatedly insert the next element into the correct spot in the sorted group. This is similar to how people often sort index cards or files, inserting one at a time into those already sorted. This is similar to how people often sort index cards or files, inserting one at a time into those already sorted.

Sorting At the start you consider the first element to be your sorted list At the start you consider the first element to be your sorted list 6, 34, 12, 7, 3, 26, 19 Insert 34 after 6 6, 34, 12, 7, 3, 26, 19 Insert 34 after 6 6, 34, 12, 7, 3, 26, 19 Insert 12 after 6 6, 34, 12, 7, 3, 26, 19 Insert 12 after 6 6, 12, 34, 7, 3, 26, 19 Insert 7 after 6 6, 12, 34, 7, 3, 26, 19 Insert 7 after 6 6, 7, 12, 34, 3, 26, 19 Insert 3 before 6 6, 7, 12, 34, 3, 26, 19 Insert 3 before 6 3, 6, 7, 12, 34, 26, 19 Insert 26 after 12 3, 6, 7, 12, 34, 26, 19 Insert 26 after 12 3, 6, 7, 12, 26, 34, 19 Insert 19 after 12 3, 6, 7, 12, 26, 34, 19 Insert 19 after 12 3, 6, 7, 12, 19, 26, 34 3, 6, 7, 12, 19, 26, 34

Sorting We won’t fully analyze it, but insertion sort turns out to have the same running time as selection sort, n 2 We won’t fully analyze it, but insertion sort turns out to have the same running time as selection sort, n 2 Algorithms with running time n 2 are fine with small amounts of data, but faster algorithms are used otherwise. Algorithms with running time n 2 are fine with small amounts of data, but faster algorithms are used otherwise.