Topic 9B – Array Sorting and Searching

Slides:



Advertisements
Similar presentations
Topic 14 Searching and Simple Sorts "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The.
Advertisements

Introduction to Algorithms Quicksort
College of Information Technology & Design
Introduction to Computer Science Theory
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Sorting CMSC 201. Sorting In computer science, there is often more than one way to do something. Sorting is a good example of this!
Topic 24 sorting and searching arrays "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be."
Chapter 9: Searching, Sorting, and Algorithm Analysis
Visual C++ Programming: Concepts and Projects
CS0007: Introduction to Computer Programming Array Algorithms.
HST 952 Computing for Biomedical Scientists Lecture 9.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
CMPS1371 Introduction to Computing for Engineers SORTING.
CPSC 171 Introduction to Computer Science More Efficiency of Algorithms.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 11: Sorting and Searching  Searching Linear Binary  Sorting.
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
C++ Plus Data Structures
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
1 Two-Dimensional Arrays. 2 Can be visualized as consisting m rows, each of n columns Syntax: datatype arrayname [row] [ column] ; Example: int val[3]
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.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Chapter 12 Recursion, Complexity, and Searching and Sorting
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.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
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 and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
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.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
ALGORITHMS.
Chapter 9 Sorting 1. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step.
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.
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.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
CMPT 120 Topic: Sorting Algorithms – Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n -> O(n) i.e., has.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Data Structures and Algorithms Instructor: Tesfaye Guta [M.Sc.] Haramaya University.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Assignment 5 is posted. Exercise 8 is very similar to what you will be doing with assignment 5. Exam.
CPS120: Introduction to Computer Science Sorting.
Searching and Sorting Algorithms
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Algorithm design and Analysis
Standard Version of Starting Out with C++, 4th Edition
Topic 24 sorting and searching arrays
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Presentation transcript:

Topic 9B – Array Sorting and Searching

Algorithms An algorithm is a set of steps that can be followed to solve a problem. Designed and developing an algorithm is often the most difficult part of the problem-solving process. For each problem, there can be many different algorithms that correctly solve the problem. CISC105 – Topic 9B

Algorithms Some algorithms may do so faster than others that do the same thing (algorithms that solve the same problem may vary in efficiency). Some algorithms may be easier to understand and may “make more sense” than others that do the same thing (algorithms that solve the same problem may vary in complexity). CISC105 – Topic 9B

Introduction to Sorting Many of the algorithms that operate on data contained within an array require the data in that array to be sorted. This means that the data contained in the array needs to be in order, from lowest-to-highest, or vice versa. We will examine a simple technique for putting the array in lowest-to-highest order. Note that once this problem is solved, the reverse, putting the array in highest-to-lowest order, is trivial. CISC105 – Topic 9B

Introduction to Sorting There are many approaches to sorting. Some sorting algorithms are much better than others. We will examine one of the simplest sorting algorithms, a selection sort. This algorithm is intuitive (easy-to-understand), although it is not very efficient. CISC105 – Topic 9B

Selection Sort: The Basic Idea The basic idea of the selection sort is to process the array from left-to-right (index 0, index 1, etc…) At each element, we look to the right and find the lowest value. Once we find the lowest value (to the right of the current element), we swap the two elements. Then, we move onto the next element and repeat the process. CISC105 – Topic 9B

Selection Sort: The Algorithm (1) current_element = 0 (2) Find index_of_min, the index of the smallest element in the subarray, array[current_element] to array[size – 1] (3) If current_element does not equal index_of_min, swap elements at current_element and index_of_min (4) If current_element = size – 1 , stop. Else current_element++ & goto step (2) CISC105 – Topic 9B

Selection Sort : An Example 24 98 4 3 55 62 3 4 98 24 24 98 55 98 62 98 X[0] X[1] X[2] X[3] X[4] X[5] CISC105 – Topic 9B

Sorting Algorithms Keep in mind that a selection sort is simply one method of sorting an array. It is a very simple-to-understand approach. It is also not very efficient. Other sorting algorithms that are much more efficient, although much harder-to-understand, include the bubble sort and the quicksort. We will not discuss these algorithms. CISC105 – Topic 9B

Introduction to Searching Searching an array is a closely related problem to sorting an array. The simplest approach to array searching is the linear search. This searching method consists of examining the array elements from left to right. If the target is found, we save the index it was found at and stop. If not, and there are still more array elements to the right, we move to the next element and repeat. CISC105 – Topic 9B

The Linear Search Thus, we simply start at index 0 and see if the target is there. If so, we stop. If not, we move to index 1 and see if the target is there. If not, we move to index 2 and see if the target is there. etc… CISC105 – Topic 9B

Linear Search: An Example We will perform a linear search on the array X in order to find the target value of 55. 24 98 4 3 55 62 X[0] X[1] X[2] X[3] X[4] X[5] 55 is located at index 4! CISC105 – Topic 9B

Searching Algorithms The linear search is not a very efficient algorithm. Notice that this algorithm does not depend on, or require, that the array is sorted, or in any order at all. If the array is sorted, we can use a much more efficient searching algorithm, the binary search. CISC105 – Topic 9B

The Binary Search The binary search is very intuitive. (1) Begin with the entire array (2) Select the middle element. (3) If the target is less than the middle element, choose the next subarray to be the half of the array to the left (smaller than) the middle element. If the target is greater than the middle element, choose the next subarray to be the half of the array to the right (greater than) the middle element. (4) Then, repeat on the new subarray. CISC105 – Topic 9B

Binary Search: An Example We will perform a binary search on the array X in order to find the target value of 55. 3 4 24 25 55 62 98 X[0] X[1] X[2] X[3] X[4] X[5] X[6] 55 is located at index 4! CISC105 – Topic 9B

Summary Searching and sorting arrays are common problems in computer science. Both problems, like almost all problems, have multiple solutions. Solutions vary on complexity and efficiency. We have examined the selection sort, the linear search, and the binary search. CISC105 – Topic 9B