J. Michael Moore Searching & Sorting CSCE 110. J. Michael Moore Searching with Linear Search Many times, it is necessary to search an array to find a.

Slides:



Advertisements
Similar presentations
CSE Lecture 3 – Algorithms I
Advertisements

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.
Simple Sorting Algorithms
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Analysis of Algorithm.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Chapter 14: Sorting and searching. Chapter Goals To study several sorting and searching algorithms To appreciate that algorithms for the same task can.
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.
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1)
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Searching CS 105 See Section 14.6 of Horstmann text.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
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.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
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.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Data Structure Introduction.
Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
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.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
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.
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
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.
Data Structures Arrays and Lists Part 2 More List Operations.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Searching CS 110: Data Structures and Algorithms First Semester,
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
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.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Growth of Functions & Algorithms
Lecture 14 Searching and Sorting Richard Gesick.
Recitation 13 Searching and Sorting.
CSc 110, Autumn 2017 Lecture 37: searching and sorting
Searching CSCE 121 J. Michael Moore.
CSc 110, Spring 2017 Lecture 39: searching and sorting
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting 1-D Arrays
Simple Sorting Algorithms
Module 8 – Searching & Sorting Algorithms
Simple Sorting Algorithms
Module 8 – Searching & Sorting Algorithms
Algorithms.
Module 8 – Searching & Sorting Algorithms
Sorting Algorithms.
Presentation transcript:

J. Michael Moore Searching & Sorting CSCE 110

J. Michael Moore Searching with Linear Search Many times, it is necessary to search an array to find a particular entry. To study the basics of this issue, we will look at an array of ________ integers (I.e. no __________) and search for a particular value called the ______. The answer is the ________ of the array element that holds the ______. If the ______ is not in the array, then this should be conveyed.

J. Michael Moore Linear Search A simple algorithm for linear (or sequential) search: Const MAX = 5; Type Number = Array[1..MAX] of integer; function linearSearch (A:Number, int key):integer; {returns the index to where the search value is found} begin var i:integer; linearSearch := -1; for i := 1 to MAX do if (A[i]=key) then linearSearch := i; end; _______________ running time is proportional to the __________________ in the array. O(n) If entries are ordered, faster algorithms are possible.

J. Michael Moore Sorting Many applications using arrays require that the data be sorted. A[1] < A[2] <... < A[n-1] When the data is sorted, other operations can be done ______________ (e.g., searching).

J. Michael Moore Insertion Sort Many sorting algorithms have been devised. This is a simple one called insertion sort. Think of the array as ___________ into the part that is __________ _________ and the part that is __________________. Initially, only the _________________ of the array, A[1], is sorted. The rest, A[2..n], is not.

J. Michael Moore Insertion Sort Suppose we’ve sorted A[1..i-1]. Our task is to insert the i th entry, v, into its correct place among the already sorted entries. A[1] A[i-1]A[i]A[i+1]A[n] v sortedunsorted To be ________ into __________ array

J. Michael Moore Insertion Sort Start _____________ backwards from A[i-1] down to A[0]. ________ entries to the _________ until the proper place for v is _______, and _______ v. A[0] A[i-1]A[i]A[i+1]A[n] v >v<v v ______ hereMust ________ this part unsorted

J. Michael Moore Insertion Sort More detail: At each entry j in that range, ________ v to A[j]. If A[j]> v, then v goes _______________________ A[j] ; keep shifting -- value in A[j] is moved to A[j+1]. If A[j]< v, then v goes just after A[j], so v is put in A[j+1]. If we _____________ the end of the array, then v is __________ than anything in the range, so v goes in A[0]. A[0] A[i]A[i+1]A[n-1] v >v<v sorted unsorted

J. Michael Moore Pascal Code for Insertion Sort procedure insertionSort(var A:Number); var i,j:integer; var value:integer; begin for i := 2 to MAX do {start at second element in array} begin value := A[i]; { current value to insert } j := i - 1; { between A[i-1] and A[1] } while ((j >= 1) AND (A[j] > value)) do begin A[j+1] := A[j]; { shift to the right } j := j – 1 end; A[j+1] := value {Place value} end end;

J. Michael Moore Insertion Sort The __________ running time for insertion sort is proportional to the ___________ of the _______________________ to be sorted O(___) A visualization: Note: The array used by these sites is indexed from 0 to n-1, where the array used in these slides is indexed from 1 to n.

J. Michael Moore procedure insertionSort(var A:Number); var i,j:integer; var value:integer; begin for i := 2 to MAX do {start at second element in array} begin value := A[i]; { current value to insert } j := i - 1; { between A[i-1] and A[1] } while ((j >= 1) AND (A[j] > value)) do begin A[j+1] := A[j]; { shift to the right } j := j – 1 end; A[j+1] := value {Place value} end end; Pascal Code for Insertion Sort

J. Michael Moore Modification for Practical usage procedure insert(value: integer; var A:Number; i: integer); var j: integer; begin j := i - 1; { between A[i-1] and A[1] } while ((j >= 1) AND (A[j] > value)) do begin A[j+1] := A[j]; { shift to the right } j := j – 1 end; A[j+1] := value {Place value} end;

J. Michael Moore Modification for Practical usage procedure insertionSort(var A:Number); var i,j: integer; var value: integer; begin for i := 2 to MAX do {start at second element in array} begin value := A[i]; { current value to insert } insert(value, A, i); end end;

J. Michael Moore Binary Search Recall Linear Search… We started at one end and checked each value With ordered Data we can do ____________. We don’t have to look at _______ array element. sorted Is the _______ value _______ than what we are looking for? Not here Somewhere in here! Let’s look in the ______: is it what we are looking for? YES Not here Somewhere in here! NO

J. Michael Moore Binary Search Repeat the process until we find what we are looking for or fail We continue until we find what we are looking for, or run out of places to look thus failing to find a match. sorted Is the ______ value _______ than what we are looking for? YES Not here NO Let’s look in the ______: is it what we are looking for? NO Not here  Here  Not here  Here 

J. Michael Moore Binary Search Implementation function binarySearch(A:Number; key:integer):integer; begin var low, high, mid: integer; var found:boolean; found := false; low = 1; high = MAX; while (low<=high) AND (NOT found) do begin mid = (low + high) div 2; if (A[mid] > key) then high = mid – 1 else if (A[mid] < key) then low = mid + 1 else found := true end; if found then binarySearch := mid else binarySearch := -1; end;

J. Michael Moore Binary Search Runs much _______ than linear search. The _______ running time for binary search is proportional to the ________ of the ________________ to be searched O(______) ____ is defined as ______ Linear search: O(______) Binary search: O(______)