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.

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

CSE Lecture 3 – Algorithms I
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Searching and Sorting Algorithms Based on D. S
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.
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.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
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.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
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.
Chapter 8 ARRAYS Continued
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
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.
Applications of Arrays (Searching and Sorting) and Strings
Lecture 12. Searching Algorithms and its analysis 1.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
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. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
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]
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
Chapter 14: Searching and Sorting
Sorting List is rearranged into sorted order How is the sorted order determined? – The ItemType is responsible for determining the key to be used in comparison.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
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.
3 – SIMPLE SORTING ALGORITHMS
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Sorting Algorithms: Selection, Insertion and Bubble.
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.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Elementary Sorting 30 January Simple Sort // List is an array of size == n for (i = 1; i < n; i++) for (j = i+1; j List[j])
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Arrays
Chapter 9: Sorting and Searching Arrays
Searching and Sorting Algorithms
Lecture 14 Searching and Sorting Richard Gesick.
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.
Linear and Binary Search
Introduction to Search Algorithms
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Topic 24 sorting and searching arrays
Searching and Sorting Arrays
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

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 used elements at the top Sequential or Linear Search

2 public static int linearSearch(const int array[], int key) { boolean found = false; int i = 0; while (i < array.length && !found) { if (array[i] == key) found = true; else i++; } if (found) return i; else return -1; }

3 BINARY SEARCH requires sorted list – (Ex: phone books, dictionaries). keep dividing list in half, compare against middle argument eliminates one half of the elements after each comparison. efficient

4 Simple Binary Search Algorithm: locates the middle element and compares it with the search key, till a match is found or no elements are left for comparison. The array: How to search for x: 1. Compare with the middle element, m 2. If x = m, done 3. If x > m, continue searching in the right half 4. If x < m, continue the search in the left half

5 public static int binSearch( ItemType list[], ItemType item, { int first = 0; // lower bound on list int last = list.length - 1; // upper bound on list int middle; // middle index found = FALSE; while (last >= first && !found) { middle = (first + last)/2; if (item < list[middle] last = middle - 1; else if (item > list[middle]) first = middle + 1; else found = true; } if (found) return middle; else return -1; }

6 Average Number of Iterations

7 Linear vs Binary Search: n elements - list[n] Linear search –requires between 1 and n comparisons/array accesses Exa: in a worst case scenario, searching an array of 1024 elements with take 1024 comparisons Binary search –requires between 1 and log(n) comparisons/array accesses Exa: in a worst case scenario, searching an array of 1024 elements with take only 10 comparisons

8 Selection sort One of many sorting algorithms In-place comparison sort inefficient on large lists noted for its simplicity It works as follows: Find the minimum value in the list Swap it with the value in the first position Repeat the steps above for remainder of the list (starting at the second position) Effectively, we divide the list into two parts: the sublist of items already sorted, which we build up from left to right and is found at the beginning, and the sublist of items remaining to be sorted, occupying the remainder of the array

9 Sorting public static void selSort(ItemType list[]) { int passCount; int placeCount; int minIndex; for (passCount = 0; passCount < list.length - 1; passCount++) { minIndex = passCount; // find the index of the smallest component in list for (placecount = passCount + 1; placeCount < length; placeCount++) if (list[placeCount] < list[minIndex]) minIndex = placeCount; swap(list[minIndex],list[passCount]); }

10 As an example, begin with the following array. The selection sort marks the first element (7). It then goes through the remaining data to find the smallest number (1). It swaps with the first element (7) and the smallest element (1) which is placed in its correct position. It then marks the second element (4) and looks through the remaining data for the next smallest number (2). These two numbers are then swapped. Marking the third element (7) and looking through the remaining data for the next smallest number (4). These two numbers are now swapped. Lastly it marks the fourth element (9) and looks through the remaining data for the next smallest number (7). These two numbers are then swapped. If we were not finished at this point this sort would continue until n-1 passes have been made.