Chapter 16: Searching, Sorting, and the vector Type.

Slides:



Advertisements
Similar presentations
Chapter 10: Applications of Arrays and the class vector
Advertisements

C++ Programming:. From Problem Analysis
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
Visual C++ Programming: Concepts and Projects
CPS120: Introduction to Computer Science Searching and Sorting.
Chapter 19: Searching and Sorting Algorithms
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.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
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 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Searching Arrays Linear search Binary search small arrays
Sorting Chapter 10. Chapter 10: Sorting2 Chapter Objectives To learn how to use the standard sorting methods in the Java API To learn how to implement.
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 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
CSC220 Data Structure Winter
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Chapter 8 Arrays and Strings
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Searching and Sorting Algorithms.
Chapter 10 Applications of Arrays and Strings. Chapter Objectives Learn how to implement the sequential search algorithm Explore how to sort an array.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Applications of Arrays (Searching and Sorting) and Strings
Chapter 19: Searching and Sorting Algorithms
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
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.
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
CSC 211 Data Structures Lecture 13
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Chapter 14: Searching and Sorting
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 12 Binary Search and QuickSort Fundamentals of Java.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 10: Applications of Arrays (Searching and Sorting) and the vector Type.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 2.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
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.
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.
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.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
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.
Searching Topics Sequential Search Binary Search.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
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.
Java Programming: Guided Learning with Early Objects Chapter 8 Applications of Arrays (Sorting and Searching) and Strings.
Searching Arrays Linear search Binary search small arrays
Chapter 16: Searching, Sorting, and the vector Type
Chapter 9: Sorting and Searching Arrays
Searching and Sorting Algorithms
Linear and Binary Search
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Data Structures Using C++ 2E
Applications of Arrays
Presentation transcript:

Chapter 16: Searching, Sorting, and the vector Type

Objectives In this chapter, you will: – Learn about list processing and how to search a list using sequential search – Explore how to sort an array using the bubble sort and insertion sort algorithms – Learn how to implement the binary search algorithm – Become familiar with the vector type C++ Programming: From Problem Analysis to Program Design, Seventh Edition2

List Processing List: a collection of values of the same type Array is a convenient place to store a list Basic list operations: – Search the list for a given item – Sort the list – Insert an item in the list – Delete an item from the list – Print the list C++ Programming: From Problem Analysis to Program Design, Seventh Edition3

Searching Sequential search algorithm: – Not very efficient for large lists – On average, number of key comparisons is equal to half the size of the list – Does not assume that the list is sorted If the list is sorted, the search algorithm can be improved C++ Programming: From Problem Analysis to Program Design, Seventh Edition4

Bubble Sort list[0]...list[n - 1] – List of n elements, indexed 0 to n – 1 – Example: a list of five elements (Figure 16-1) C++ Programming: From Problem Analysis to Program Design, Seventh Edition5

Bubble Sort (cont’d.) Series of n - 1 iterations – Successive elements list[index] and list[index + 1] of list are compared – If list[index] > list[index + 1] Swap list[index] and list[index + 1] – Smaller elements move toward the top (beginning of the list) – Larger elements move toward the bottom (end of the list) C++ Programming: From Problem Analysis to Program Design, Seventh Edition6

Bubble Sort (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition7

Bubble Sort (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition8

Bubble Sort (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition9

Bubble Sort (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition10

Bubble Sort (cont’d.) List of length n – Exactly n(n - 1) / 2 key comparisons – On average n(n - 1) / 4 item assignments If n = 1000 – 500,000 key comparisons and 250,000 item assignments Can improve performance if we stop the sort when no swapping occurs in an iteration C++ Programming: From Problem Analysis to Program Design, Seventh Edition11

Insertion Sort Sorts the list by moving each element to its proper place C++ Programming: From Problem Analysis to Program Design, Seventh Edition12

Insertion Sort (cont’d.) Consider the element list[4] – First element of unsorted list – list[4] < list[3] Move list[4] to proper location at list[2] C++ Programming: From Problem Analysis to Program Design, Seventh Edition13

Insertion Sort (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition14

Insertion Sort (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition15

Insertion Sort (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition16

Insertion Sort (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition17

Insertion Sort (cont’d.) During the sorting phase, the array is divided into two sublists: sorted and unsorted – Sorted sublist elements are sorted – Elements in the unsorted sublist are to be moved into their proper places in the sorted sublist, one at a time C++ Programming: From Problem Analysis to Program Design, Seventh Edition18

Insertion Sort (cont’d.) List of length n – About (n 2 + 3n – 4) / 4 key comparisons – About n(n – 1) / 4 item assignments If n = 1000 – 250,000 key comparisons – 250,000 item assignments C++ Programming: From Problem Analysis to Program Design, Seventh Edition19

Binary Search Much faster than a sequential search List must be sorted “Divide and conquer” Compare search item with middle element – If less than middle: search only upper half of list – If more than middle: search only lower half of list C++ Programming: From Problem Analysis to Program Design, Seventh Edition20

Binary Search (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition21

Performance of Binary Search If L is a sorted list of size 1024 – Every iteration of the while loop cuts the size of the search list by half – At most, 11 iterations to determine whether x is in L – Binary search will make 22 comparisons at most If L has1,048,576 elements – Binary search makes 42 item comparisons at most C++ Programming: From Problem Analysis to Program Design, Seventh Edition22

Performance of Binary Search (cont’d.) For a sorted list of length n: – Maximum number comparisons is 2log 2 n + 2 C++ Programming: From Problem Analysis to Program Design, Seventh Edition23

vector type (class) Only a fixed number of elements can be stored in an array Inserting and removing elements causes shifting of remaining elements vector type implements a list – vector container – vector – vector object – object C++ Programming: From Problem Analysis to Program Design, Seventh Edition24

vector type (class) (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition25

vector type (class) (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition26

vector type (class) (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition27

Vectors and Range-Based for loops Can use range-based for loop in C++11 Standard to process vector elements for (auto p : list) //for all p in list cout << p << " "; cout << endl; Can initialize a vector object vector intList = {13, 75, 28, 35}; – Not supported by all C++11 compilers C++ Programming: From Problem Analysis to Program Design, Seventh Edition28

Summary List – Set of elements of the same type Sequential search – Searches each element until item is found Sorting algorithms – Bubble sort – Insertion sort C++ Programming: From Problem Analysis to Program Design, Seventh Edition29

Summary (cont’d.) Binary search – Much faster than sequential search – Requires that the list is sorted vector type – Implements a list – Can increase/decrease in size during program execution – Must specify the type of object the vector stores C++ Programming: From Problem Analysis to Program Design, Seventh Edition30