Chapter 9: Searching, Sorting, and Algorithm Analysis

Slides:



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

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.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
An Introduction to Sorting Chapter 8. 2 Chapter Contents Selection Sort Iterative Selection Sort Recursive Selection Sort The Efficiency of Selection.
Chapter 3: Sorting and Searching Algorithms 3.2 Simple Sort: O(n 2 )
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.
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.
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.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
Computer Science 101 Introduction to Sorting. Sorting One of the most common activities of a computer is sorting data Arrange data into numerical or alphabetical.
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 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.
1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here.
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Basic Sorting Algorithms Dr. Yingwu Zhu. Sorting Problem Consider list x 1, x 2, x 3, … x n Goal: arrange the elements of the list in order Ascending.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Searching Arrays Linear search Binary search small arrays
Sort Algorithm.
Searching and Sorting Arrays
Sorting Dr. Yingwu Zhu.
Chapter 9: Sorting and Searching Arrays
Alternate Version of STARTING OUT WITH C++ 4th Edition
CS212: Data Structures and Algorithms
Searching and Sorting Arrays
Data Structures I (CPCS-204)
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Chapter 9: Searching, Sorting, and Algorithm Analysis
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.
Sorting Chapter 10.
10.3 Bubble Sort Chapter 10 - Sorting.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Linear and Binary Search
Algorithm Efficiency and Sorting
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Introduction to Search Algorithms
Bubble, Selection & Insertion sort
Selection Sort Sorted Unsorted Swap
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Sorting … and Insertion Sort.
C++ Plus Data Structures
Searching and Sorting 1-D Arrays
Search,Sort,Recursion.
Sorting Dr. Yingwu Zhu.
Searching and Sorting Arrays
Review of Searching and Sorting Algorithms
Searching and Sorting Arrays
Analysis of Algorithms
Introduction to Sorting Algorithms
CSE 373 Sorting 2: Selection, Insertion, Shell Sort
CHAPTER 9 SORTING & SEARCHING.
Module 8 – Searching & Sorting Algorithms
Sorting Algorithms.
Presentation transcript:

Chapter 9: Searching, Sorting, and Algorithm Analysis Starting Out with C++ Early Objects Ninth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda

Topics 9.3 Introduction to Sorting Algorithms 9.4 Sorting an Array of Objects 9.5 Sorting Vectors

9.3 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here Bubble sort Selection sort Insertion sort

Selection Sort Algorithm Locate the smallest element in the array and exchange it with the element in position 0. Locate the next smallest element in the array and exchange it with element in position 1. Continue until all of the elements are in order. See pr9-05.cpp

Selection Sort Example Array numlist contains The smallest element is 2. Exchange 2 with the element at subscript 0. 11 2 29 3 Now in order 2 11 29 3

Selection Sort Example, Continued 2 11 29 3 The next smallest element is 3. Exchange 3 with the element at subscript 1. The next smallest element is 11. Exchange 11 with the element at subscript 2. 2 3 29 11 2 3 11 29

//Select sort puts elements in a in ascending order, by Chapter 9 slide 1212 //Select sort puts elements in a in ascending order, by //repeatedly finding minimum element and swapping it with //first element in “unsorted part” of array void selectSort (ArrayType a; int n) { int mindex; for (int i = 0; i < n-1; i++) mindex = i; //find index of min element in unsorted subarray for (int j = i+1; j < n-1; j++) if (a[j] < a[mindex]) mindex = j; } //swap 1st unsorted element with minimum element swap (a[mindex], a[i]);

Analysis of Sort Efficiency or Time Complexity Chapter 9 slide 1313 Analysis of Sort Notice: N elements  N-1 passes In EACH iteration, found smallest element in unsorted part of list and put it in its correct place on each pass, 1 element put in its proper place Efficiency or Time Complexity 1ST Pass: N-1 comparisons 2nd Pass: N-2 comparisons … (N-1)st Pass: 1 comparison (N-1) + (N-2) + …+ 1 = N (N-1) = N2-N = O (N2) 2 2

Selection Sort Tradeoffs Chapter 9 slide 1414 Selection Sort Tradeoffs Benefit Easy to understand Uses FindMax or FindMin algorithm from CMPS1044 Disadvantage There are better ways to sort, esp. if you know something about your data

9.4 Sorting an Array of Objects As with searching, arrays to be sorted can contain objects or structures The key field determines how the structures or objects will be ordered When exchanging the contents of array elements, entire structures or objects must be exchanged, not just the key fields in the structures or objects See pr9-06.cpp

Insertion Sort Good when data is random, great when data is already somewhat ordered…. Algorithm: Consider first element is in sorted part of list for the next element in unsorted part of list put it into its proper place relative to the sorted part of the list Chapter 9 slide 16

Example 20 20 15 15 15 20 18 18 18 8 8 3 9 9 9 initial start end 1st pass 15 15 15 20 18 18 18 20 20 8 8 3 9 9 9 start end 2nd pass sorted unsorted Chapter 9 slide 17

Example 8 8 8 8 8 15 15 15 9 9 18 18 9 15 15 20 9 18 18 18 9 20 20 20 20 start end 4th pass sorted unsorted 15 15 15 8 18 18 8 15 20 8 18 18 8 20 20 20 9 9 9 9 start end 3rd pass Chapter 9 slide 18

void insertSort (ArrayType a; int n) { bool found; int j; //Insert sort puts elements in ascending order by repeatedly //putting 1st element in unsorted subarray into proper position of sorted array void insertSort (ArrayType a; int n) { bool found; int j; //put each element in unsorted subarray in proper position for (int i=1; i < n; i++) //find proper place for a[i] relative to a[0]..a[i] found = false; j = i; while ((j > i) && !found) //swap and decrement j if a[j] < a[j-1] swap (a[j], a[j-1]); j= j-1; } else found = true; } //end while } //end for } //end insertSort

Best Case Analysis- Data Ordered Pass # comparisons 1 1 2 1 3 1 … N-1 1 1 + 1 + .. + 1+ 1 = (N-1) * 1 = N-1 = O(N) Chapter 9 slide 20

Worst Case Analysis Pass # comparisons 1 1 2 2 3 3 … N-1 N-1 1 + 2 + .. + (N-2) + (N-1) = O (N2) Chapter 9 slide 21

Efficiency Sorting is a COMMON operation to ignore efficiency of sort is to ignore efficiency of program Level of efficiency measured by # of comparisons (dominant operation) # of comparisons is a function of n Chapter 9 slide 22

9.5 Sorting Vectors Sorting algorithms can be applied to vectors as well as to arrays You need slight modifications to functions to use vector arguments vector <type> & used in prototype There is no need to indicate the vector size, as functions can use the vector’s size member function See pr9-07.cpp

Other Resources on Sorting Sorting Algorithms Animations HTML5 Canvas Demo: Sorting Algorithms

Chapter 9: Searching, Sorting, and Algorithm Analysis Starting Out with C++ Early Objects Ninth Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda