HST 952 Computing for Biomedical Scientists Lecture 9.

Slides:



Advertisements
Similar presentations
Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
Advertisements

Back to Sorting – More efficient sorting algorithms.
Introduction to Computer Science Theory
Garfield AP Computer Science
HST 952 Computing for Biomedical Scientists Lecture 10.
CS4413 Divide-and-Conquer
CMPS1371 Introduction to Computing for Engineers SORTING.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Sorting.
Sorting Chapter 9.
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.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 20: Sorting.
Searching/Sorting Introduction to Computing Science and Programming I.
Searching Arrays Linear search Binary search small arrays
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
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.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
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. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
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.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
ALGORITHMS.
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.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
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.
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.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Data Structures Arrays and Lists Part 2 More List Operations.
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.
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.
Using recursion for Searching and Sorting
16 Searching and Sorting.
Introduction to Search Algorithms
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Quicksort and Mergesort
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.
MSIS 655 Advanced Business Applications Programming
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
24 Searching and Sorting.
Algorithm Efficiency and Sorting
Presentation transcript:

HST 952 Computing for Biomedical Scientists Lecture 9

Outline Searching and sorting Programming examples

Searching and sorting To search a collection of numbers, characters, strings or any other type that can be ordered –if the collection is not sorted, we may have to examine each member of the collection in turn until we find the item we are seeking (a linear or sequential search strategy) –if the collection is sorted we may be able to speed up or otherwise improve the efficiency of our search by taking advantage of this fact

Searching and sorting We will look briefly at some searching methods –linear/sequential search –binary search Since the process of searching a collection of numbers, etc. can be improved if we know that the items in the collection are already sorted we will look at some sorting methods: –selection sort –merge sort We will use arrays and vectors to illustrate the searching and sorting methods

Linear/sequential search If we have a completely filled array –start at the beginning of the array and proceed in sequence until either the value is found or the end of the array is reached –or, start at the end of the array and proceed backwards in sequence until the value being sought is found or the beginning of the array is reached If the array is only partially filled, –keep track of the number of valid entries in the array –forward search stops when the last meaningful value has been checked

Linear/sequential search Linear search is not the most efficient search method However, it works and is easy to program public int itemInArray(char item, char[] arr, int validEntries) { int index = -1; for (int i = 0; i < validEntries; i++) { if (item == arr[i]) index = i; } return index; }

Linear/sequential search In the worst possible case (when the element we are seeking is in the last position in the array or is not in the array at all), the total number of array item comparisons performed in a linear search equals the total length of the array being searched For an array with 2048 elements this means 2048 comparisons in the worst case

Binary search More efficient search method than linear search Works for arrays/collections that are already sorted (is essentially the strategy that humans use for searching a phone book or dictionary) –this strategy is often called a divide-and-conquer strategy Strategy for searching for a name in one section of a phone book is the same as initial strategy for searching for the name in the entire phone book This implies that we can solve the binary search problem using recursion

Binary search Search: middle page = (first page + last page)/2 Open the phone book to middle page; If (name is on middle page) then done; //this is the base case else if (name is alphabetically before middle page) last page = middle page //redefine search area to front half Search //recursive call with reduced number of pages else //name must be after middle page first page = middle page //redefine search area to back half Search //recursive call with reduced number of pages

Binary search In the worst possible case (e.g., when the element we are seeking is not in the array at all), the total number of array item comparisons performed in a binary search is significantly less than for linear search For an array with 2048 elements a binary search would perform 11 comparisons in the worst case (compared to 2048 for linear search!)

Binary search Programming example

Selection sort To sort an array, A of integers in non-decreasing order: search the array for the smallest number and record its index swap (interchange) the smallest number with the first element of the array –the sorted part of the array is now the first element: A[0] –the unsorted part of the array is the remaining elements: A[1]- A[length-1] search A[1]- A[length-1] for the next smallest element and record that element's index swap the next smallest element with the second element of the array repeat the search and swap until all elements have been placed –each iteration of the search/swap process increases the length of the sorted part of the array by one, and reduces the unsorted part of the array by one

Selection sort One of the easiest sorting methods to understand and code However it is not the most efficient –in the worst case (we are sorting in non- decreasing/increasing order: e.g., but the array is already sorted in decreasing order: e.g., ) for an array of length 1000, we perform roughly 1,000,000 comparisons –we will look at merge sort next which is more efficient than selection sort

Selection sort Board example Programming example

Merge sort Uses a divide-and-conquer strategy for sorting The idea is to sort a set of n numbers, etc. by splitting them into two sets of roughly equal size (n/2) We then sort each of the half-sized sets of numbers separately To complete the sorting of the original set of n numbers, we merge the two sorted, half-sized sets Note that we can apply the same split-and-merge approach when we sort the two half-sized sets –this means we can use a recursive approach for merge sorting

Merge sort Merging means that we produce from the two sorted sets a single sorted set containing all the elements in the two sets and no others We can use two vectors to hold these two sets A simple algorithm for merging the two sorted sets is: –compare the first elements of the two vectors holding the two sets of numbers we wish to merge –store the smaller of the two into a new vector that will hold the combined set of numbers (can break ties arbitrarily) –remove the stored element from its original vector so that this vector has a new first element –repeat this process until there are no elements left in the two original vectors

Merge sort More efficient than selection sort: –In the worst case we want to merge-sort two sets that are already sorted in decreasing order: e.g., and into one set with elements in non-decreasing/ increasing order: i.e., –For a vector/array of length 1000, we would need to perform roughly 10,000 comparisons (as opposed to 1,000,000 for selection sort)

Merge sort Board example Programming example

Other sorting algorithms There are other sorting algorithms that we won’t examine in detail: Quicksort Insertion sort Heapsort Bubblesort Bogosort (only ever used to illustrate the worst/ most inefficient method possible for sorting)

Summary Linear search is most appropriate for searching through a collection of unsorted items It is not very efficient, but is easy to program Binary search is a more efficient search method than linear search It works for arrays/collections that are already sorted (is essentially the strategy that humans use for searching a phone book or dictionary) –this strategy is often called a divide-and-conquer strategy Strategy for searching for a name in one section of a phone book is the same as initial strategy for searching for the name in the entire phone book This implies that we can solve the binary search problem using recursion

Summary Selection sort is one of the easiest sorting methods to understand and code Interchanges smallest number in array with first location in array It is not the most efficient sorting method Merge sort uses a divide-and-conquer strategy for sorting More efficient than selection sort