Chapter 9: Sorting and Searching Arrays

Slides:



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

Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Chapter 9: Searching, Sorting, and Algorithm Analysis
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
Visual C++ Programming: Concepts and Projects
Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and.
Chapter 6: Arrays Java Software Solutions for AP* Computer Science
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.
Searches & Sorts V Deena Engel’s class Adapted from W. Savitch’s text An Introduction to Computers & Programming.
Algorithm Efficiency and Sorting
C++ for Engineers and Scientists Third Edition
Programming Logic and Design Fourth Edition, Comprehensive
Searching and Sorting Arrays
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
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.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Chapter 8 ARRAYS Continued
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Chapter 19: Searching and Sorting Algorithms
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
Sorting. Algorithms Sorting reorders the elements in an array or list in either ascending or descending order. Sorting reorders the elements in an array.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
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.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Searching & Sorting.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
1 CS 163 Data Structures Chapter 5 Sorting Herbert G. Mayer, PSU Status 5/23/2015.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Third Edition by Tony Gaddis.
Chapter 16: Searching, Sorting, and the vector Type.
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 and Sorting Searching algorithms with simple arrays
Sort Algorithm.
Sorting Chapter 14.
Searching and Sorting Arrays
Chapter 16: Searching, Sorting, and the vector Type
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
Chapter 9: Searching, Sorting, and Algorithm Analysis
Linear and Binary Search
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
Standard Version of Starting Out with C++, 4th Edition
Sorting … and Insertion Sort.
C++ Plus Data Structures
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
Starting Out with Programming Logic & Design
24 Searching and Sorting.
Searching and Sorting Arrays
Searching and Sorting Arrays
Sorting and Searching -- Introduction
CHAPTER 9 SORTING & SEARCHING.
Applications of Arrays
Presentation transcript:

Chapter 9: Sorting and Searching Arrays Starting Out with Programming Logic & Design Second Edition by Tony Gaddis

9.1 The Bubble Sort Algorithm Bubble sort is a simple sorting algorithm for rearranging the contents of an array Useful for alphabetical lists and numerical sorting Can be done in ascending or descending order With the Bubble Sort, array elements are compared and numbers bubble toward the end of the array (assuming your sorting in ascending order) Swapping elements must be done in order to properly sort the array

9.1 The Bubble Sort Algorithm

9.1 The Bubble Sort Algorithm Inside Program 9-1 maxElement variable holds the subscript of the last element that is to be compared to its neighbor index variable is an array subscript in one loop The outer loop iterates for each element in the array The inner loop iterates for each of the unsorted array elements The if statement does the comparison Sorting an array of strings to put information in alphabetical order can be done with a Bubble Sort

9.2 The Selection Sort Algorithm The selection sort works similar to the bubble sort, but more efficient Bubble sort moves one element at a time Selection sort performs fewer swaps because it moves items immediately to their final position

9.2 The Selection Sort Algorithm Figure 9-17 Flowchart for the selectionSort module

9.2 The Selection Sort Algorithm Inside Figure 9-17 minIndex holds the subscript of the element with the smallest value minValue holds the smallest value found The outer loop iterates for each element in the array, except the last The inner loop performs the scan

9.3 The Insertion Sort Algorithm The insertion sort algorithm sorts the first two elements, which becomes the sorted part of the array Each remaining element is then inserted into the sorted part of the array at the correct location Also more efficient than the bubble sort

9.3 The Insertion Sort Algorithm Figure 9-24 Flowchart for the insertionSort module

9.3 The Insertion Sort Algorithm Inside Figure 9-24 scan is used to scan through the array unsortedValue holds the first unsorted value The outer loop steps the index variable through each subscript, starting at 1 The inner loop moves the first element outside the sorted subset and into its proper position

The Swap Module A closer look at the swap module In most of the sorts, a swap module can be called It is the same in each sorting algorithm and only changes in the parameter list to account for the type of data passed to it //This swap module accepts two Integer arguments Module swap(Integer Ref a, Integer Ref b) Declare Integer temp //swap the values Set temp =a Set a = b Set b = temp End Module

9.4 The Binary Search Algorithm The binary search algorithm locates an item in an array by repeatedly dividing the array in half Each time it divides the array, it eliminates the half of the array that does not contain the item It’s more sequential than the selection search because each time it cuts the array in half and makes a smaller number of comparisons to find a match

9.4 The Binary Search Algorithm How it works Requires that the array is first sorted The first comparison is done with the middle element of the array to see if it is greater than or less than the number that is being searched If it’s greater than, then the number must be in the first half of the array If it’s less than, then the number must be in the second half of the array This process is continued until the match if found

9.4 The Binary Search Algorithm

9.4 The Binary Search Algorithm Inside Program 9-17 middle is used to store the calculated middle index value stores the value being searched for A loop hold the search and iterates as long as there are elements or until a match is found Nested if-then-else's perform the comparisons