SlideShare ist ein Scribd-Unternehmen logo
1 von 30
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 Edition 2
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 Edition 3
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 Edition 4
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 Edition 5
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 Edition 6
Bubble Sort (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 7
Bubble Sort (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 8
Bubble Sort (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 9
Bubble Sort (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 10
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 Edition 11
Insertion Sort
• Sorts the list by moving each element to its
proper place
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 12
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 Edition 13
Insertion Sort (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 14
Insertion Sort (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 15
Insertion Sort (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 16
Insertion Sort (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 17
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 Edition 18
Insertion Sort (cont’d.)
• List of length n
– About (n2
+ 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 Edition 19
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 Edition 20
Binary Search (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 21
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 Edition 22
Performance of Binary Search
(cont’d.)
• For a sorted list of length n:
– Maximum number comparisons is 2log2n + 2
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 23
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 Edition 24
vector type (class) (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 25
vector type (class) (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 26
vector type (class) (cont’d.)
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 27
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<int> intList = {13, 75, 28, 35};
–Not supported by all C++11 compilers
C++ Programming: From Problem Analysis to Program Design, Seventh Edition 28
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 Edition 29
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 Edition 30

Weitere ähnliche Inhalte

Was ist angesagt?

Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06Terry Yoast
 
9781285852744 ppt ch15
9781285852744 ppt ch159781285852744 ppt ch15
9781285852744 ppt ch15Terry Yoast
 
9781285852744 ppt ch13
9781285852744 ppt ch139781285852744 ppt ch13
9781285852744 ppt ch13Terry Yoast
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02Terry Yoast
 
9781285852744 ppt ch14
9781285852744 ppt ch149781285852744 ppt ch14
9781285852744 ppt ch14Terry Yoast
 
Matlab - Introduction and Basics
Matlab - Introduction and BasicsMatlab - Introduction and Basics
Matlab - Introduction and BasicsTechsparks
 
Chapter 2 Basics of MATLAB
Chapter 2 Basics of MATLABChapter 2 Basics of MATLAB
Chapter 2 Basics of MATLABPranoti Doke
 
Programming Logic and Design: Working with Data
Programming Logic and Design: Working with DataProgramming Logic and Design: Working with Data
Programming Logic and Design: Working with DataNicole Ryan
 
Can programming be liberated from the von neumann style?
Can programming be liberated from the von neumann style?Can programming be liberated from the von neumann style?
Can programming be liberated from the von neumann style?Oriol LĂłpez Massaguer
 
Functional Scala
Functional ScalaFunctional Scala
Functional ScalaStan Lea
 
Practical Malware Analysis: Ch 6: Recognizing C Code Constructs in Assembly
Practical Malware Analysis: Ch 6: Recognizing C Code Constructs in AssemblyPractical Malware Analysis: Ch 6: Recognizing C Code Constructs in Assembly
Practical Malware Analysis: Ch 6: Recognizing C Code Constructs in AssemblySam Bowne
 
Lex Tool
Lex ToolLex Tool
Lex ToolRajan Shah
 
CIS110 Computer Programming Design Chapter (6)
CIS110 Computer Programming Design Chapter  (6)CIS110 Computer Programming Design Chapter  (6)
CIS110 Computer Programming Design Chapter (6)Dr. Ahmed Al Zaidy
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manualSami Said
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: ModularityNicole Ryan
 

Was ist angesagt? (19)

Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06
 
9781285852744 ppt ch15
9781285852744 ppt ch159781285852744 ppt ch15
9781285852744 ppt ch15
 
9781285852744 ppt ch13
9781285852744 ppt ch139781285852744 ppt ch13
9781285852744 ppt ch13
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
9781285852744 ppt ch14
9781285852744 ppt ch149781285852744 ppt ch14
9781285852744 ppt ch14
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
 
Matlab - Introduction and Basics
Matlab - Introduction and BasicsMatlab - Introduction and Basics
Matlab - Introduction and Basics
 
Chapter 2 Basics of MATLAB
Chapter 2 Basics of MATLABChapter 2 Basics of MATLAB
Chapter 2 Basics of MATLAB
 
Programming Logic and Design: Working with Data
Programming Logic and Design: Working with DataProgramming Logic and Design: Working with Data
Programming Logic and Design: Working with Data
 
Lex & yacc
Lex & yaccLex & yacc
Lex & yacc
 
Can programming be liberated from the von neumann style?
Can programming be liberated from the von neumann style?Can programming be liberated from the von neumann style?
Can programming be liberated from the von neumann style?
 
Functional Scala
Functional ScalaFunctional Scala
Functional Scala
 
Yacc
YaccYacc
Yacc
 
Practical Malware Analysis: Ch 6: Recognizing C Code Constructs in Assembly
Practical Malware Analysis: Ch 6: Recognizing C Code Constructs in AssemblyPractical Malware Analysis: Ch 6: Recognizing C Code Constructs in Assembly
Practical Malware Analysis: Ch 6: Recognizing C Code Constructs in Assembly
 
Lex Tool
Lex ToolLex Tool
Lex Tool
 
CIS110 Computer Programming Design Chapter (6)
CIS110 Computer Programming Design Chapter  (6)CIS110 Computer Programming Design Chapter  (6)
CIS110 Computer Programming Design Chapter (6)
 
LEX & YACC
LEX & YACCLEX & YACC
LEX & YACC
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: Modularity
 

Andere mochten auch

9781111530532 ppt ch14
9781111530532 ppt ch149781111530532 ppt ch14
9781111530532 ppt ch14Terry Yoast
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsAbdullah Al-hazmy
 
Data Structures- Part8 stacks and queues
Data Structures- Part8 stacks and queuesData Structures- Part8 stacks and queues
Data Structures- Part8 stacks and queuesAbdullah Al-hazmy
 
AWS Innovate: AWS Container Management using Amazon EC2 Container Service an...
AWS Innovate:  AWS Container Management using Amazon EC2 Container Service an...AWS Innovate:  AWS Container Management using Amazon EC2 Container Service an...
AWS Innovate: AWS Container Management using Amazon EC2 Container Service an...Amazon Web Services Korea
 
Data Structures- Part9 trees simplified
Data Structures- Part9 trees simplifiedData Structures- Part9 trees simplified
Data Structures- Part9 trees simplifiedAbdullah Al-hazmy
 
Applying Design Patterns in Practice
Applying Design Patterns in PracticeApplying Design Patterns in Practice
Applying Design Patterns in PracticeGanesh Samarthyam
 
AWS ECS Quick Introduction
AWS ECS Quick IntroductionAWS ECS Quick Introduction
AWS ECS Quick IntroductionVinothini Raju
 
Lecture 2a arrays
Lecture 2a arraysLecture 2a arrays
Lecture 2a arraysVictor Palmar
 
Web Database
Web DatabaseWeb Database
Web Databaseidroos7
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8Garth Gilmour
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked listsAbdullah Al-hazmy
 
Java 8 Support at the JVM Level
Java 8 Support at the JVM LevelJava 8 Support at the JVM Level
Java 8 Support at the JVM LevelNikita Lipsky
 
Advanced Java - Praticals
Advanced Java - PraticalsAdvanced Java - Praticals
Advanced Java - PraticalsFahad Shaikh
 
Building Digital Transaction Systems in the new Banking World
Building Digital Transaction Systems in the new Banking WorldBuilding Digital Transaction Systems in the new Banking World
Building Digital Transaction Systems in the new Banking WorldRamit Surana
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for javamaheshm1206
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol setIram Ramrajkar
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIModern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIGanesh Samarthyam
 
Running Microservices on Amazon ECS - AWS April 2016 Webinar Series
Running Microservices on Amazon ECS - AWS April 2016 Webinar SeriesRunning Microservices on Amazon ECS - AWS April 2016 Webinar Series
Running Microservices on Amazon ECS - AWS April 2016 Webinar SeriesAmazon Web Services
 

Andere mochten auch (20)

9781111530532 ppt ch14
9781111530532 ppt ch149781111530532 ppt ch14
9781111530532 ppt ch14
 
Chap01
Chap01Chap01
Chap01
 
Data Structures- Part2 analysis tools
Data Structures- Part2 analysis toolsData Structures- Part2 analysis tools
Data Structures- Part2 analysis tools
 
Data Structures- Part8 stacks and queues
Data Structures- Part8 stacks and queuesData Structures- Part8 stacks and queues
Data Structures- Part8 stacks and queues
 
AWS Innovate: AWS Container Management using Amazon EC2 Container Service an...
AWS Innovate:  AWS Container Management using Amazon EC2 Container Service an...AWS Innovate:  AWS Container Management using Amazon EC2 Container Service an...
AWS Innovate: AWS Container Management using Amazon EC2 Container Service an...
 
Data Structures- Part9 trees simplified
Data Structures- Part9 trees simplifiedData Structures- Part9 trees simplified
Data Structures- Part9 trees simplified
 
Applying Design Patterns in Practice
Applying Design Patterns in PracticeApplying Design Patterns in Practice
Applying Design Patterns in Practice
 
AWS ECS Quick Introduction
AWS ECS Quick IntroductionAWS ECS Quick Introduction
AWS ECS Quick Introduction
 
Lecture 2a arrays
Lecture 2a arraysLecture 2a arrays
Lecture 2a arrays
 
Web Database
Web DatabaseWeb Database
Web Database
 
10 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 810 Sets of Best Practices for Java 8
10 Sets of Best Practices for Java 8
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Java 8 Support at the JVM Level
Java 8 Support at the JVM LevelJava 8 Support at the JVM Level
Java 8 Support at the JVM Level
 
Advanced Java - Praticals
Advanced Java - PraticalsAdvanced Java - Praticals
Advanced Java - Praticals
 
Building Digital Transaction Systems in the new Banking World
Building Digital Transaction Systems in the new Banking WorldBuilding Digital Transaction Systems in the new Banking World
Building Digital Transaction Systems in the new Banking World
 
Coding standards for java
Coding standards for javaCoding standards for java
Coding standards for java
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
 
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time APIModern Programming in Java 8 - Lambdas, Streams and Date Time API
Modern Programming in Java 8 - Lambdas, Streams and Date Time API
 
Running Microservices on Amazon ECS - AWS April 2016 Webinar Series
Running Microservices on Amazon ECS - AWS April 2016 Webinar SeriesRunning Microservices on Amazon ECS - AWS April 2016 Webinar Series
Running Microservices on Amazon ECS - AWS April 2016 Webinar Series
 

Ă„hnlich wie 9781285852744 ppt ch16

9781423902096_PPT_ch09.ppt
9781423902096_PPT_ch09.ppt9781423902096_PPT_ch09.ppt
9781423902096_PPT_ch09.pptLokeshK66
 
9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.pptLokeshK66
 
PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresPPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresmidtushar
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10Terry Yoast
 
Computer Programming
Computer ProgrammingComputer Programming
Computer ProgrammingBurhan Fakhar
 
C++.ppt
C++.pptC++.ppt
C++.pptshweta210
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.pptLokeshK66
 
UNEC__1683196273.pptx
UNEC__1683196273.pptxUNEC__1683196273.pptx
UNEC__1683196273.pptxhuseynmusayev2
 
Lecture 1 (bce-7)
Lecture   1 (bce-7)Lecture   1 (bce-7)
Lecture 1 (bce-7)farazahmad005
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5ahmed22dg
 
9780324782011_PPT_ch09.ppt
9780324782011_PPT_ch09.ppt9780324782011_PPT_ch09.ppt
9780324782011_PPT_ch09.pptValhallaExcalibur
 
Unit 1 linked list
Unit 1 linked listUnit 1 linked list
Unit 1 linked listLavanyaJ28
 
a581a6a2cb5778045788f0b1d7da1c0236f.pptx
a581a6a2cb5778045788f0b1d7da1c0236f.pptxa581a6a2cb5778045788f0b1d7da1c0236f.pptx
a581a6a2cb5778045788f0b1d7da1c0236f.pptxchristinamary2620
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - RecursionAdan Hubahib
 

Ă„hnlich wie 9781285852744 ppt ch16 (20)

9781423902096_PPT_ch09.ppt
9781423902096_PPT_ch09.ppt9781423902096_PPT_ch09.ppt
9781423902096_PPT_ch09.ppt
 
ch9.ppt
ch9.pptch9.ppt
ch9.ppt
 
9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt
 
PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresPPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structures
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
 
C++.ppt
C++.pptC++.ppt
C++.ppt
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt
 
Algorithms.
Algorithms. Algorithms.
Algorithms.
 
UNEC__1683196273.pptx
UNEC__1683196273.pptxUNEC__1683196273.pptx
UNEC__1683196273.pptx
 
Lecture 1 (bce-7)
Lecture   1 (bce-7)Lecture   1 (bce-7)
Lecture 1 (bce-7)
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERINGCOMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
 
9780324782011_PPT_ch09.ppt
9780324782011_PPT_ch09.ppt9780324782011_PPT_ch09.ppt
9780324782011_PPT_ch09.ppt
 
Unit 1 linked list
Unit 1 linked listUnit 1 linked list
Unit 1 linked list
 
a581a6a2cb5778045788f0b1d7da1c0236f.pptx
a581a6a2cb5778045788f0b1d7da1c0236f.pptxa581a6a2cb5778045788f0b1d7da1c0236f.pptx
a581a6a2cb5778045788f0b1d7da1c0236f.pptx
 
Lec5
Lec5Lec5
Lec5
 
RECURSION.pptx
RECURSION.pptxRECURSION.pptx
RECURSION.pptx
 
Lec1.pptx
Lec1.pptxLec1.pptx
Lec1.pptx
 
Chapter 13 - Recursion
Chapter 13 - RecursionChapter 13 - Recursion
Chapter 13 - Recursion
 

Mehr von Terry Yoast

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12Terry Yoast
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11Terry Yoast
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10Terry Yoast
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09Terry Yoast
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08Terry Yoast
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07Terry Yoast
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06Terry Yoast
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05Terry Yoast
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04Terry Yoast
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03Terry Yoast
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02Terry Yoast
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01Terry Yoast
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13Terry Yoast
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18Terry Yoast
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17Terry Yoast
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16Terry Yoast
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15Terry Yoast
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14Terry Yoast
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12Terry Yoast
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11Terry Yoast
 

Mehr von Terry Yoast (20)

9781305078444 ppt ch12
9781305078444 ppt ch129781305078444 ppt ch12
9781305078444 ppt ch12
 
9781305078444 ppt ch11
9781305078444 ppt ch119781305078444 ppt ch11
9781305078444 ppt ch11
 
9781305078444 ppt ch10
9781305078444 ppt ch109781305078444 ppt ch10
9781305078444 ppt ch10
 
9781305078444 ppt ch09
9781305078444 ppt ch099781305078444 ppt ch09
9781305078444 ppt ch09
 
9781305078444 ppt ch08
9781305078444 ppt ch089781305078444 ppt ch08
9781305078444 ppt ch08
 
9781305078444 ppt ch07
9781305078444 ppt ch079781305078444 ppt ch07
9781305078444 ppt ch07
 
9781305078444 ppt ch06
9781305078444 ppt ch069781305078444 ppt ch06
9781305078444 ppt ch06
 
9781305078444 ppt ch05
9781305078444 ppt ch059781305078444 ppt ch05
9781305078444 ppt ch05
 
9781305078444 ppt ch04
9781305078444 ppt ch049781305078444 ppt ch04
9781305078444 ppt ch04
 
9781305078444 ppt ch03
9781305078444 ppt ch039781305078444 ppt ch03
9781305078444 ppt ch03
 
9781305078444 ppt ch02
9781305078444 ppt ch029781305078444 ppt ch02
9781305078444 ppt ch02
 
9781305078444 ppt ch01
9781305078444 ppt ch019781305078444 ppt ch01
9781305078444 ppt ch01
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
9781337102087 ppt ch18
9781337102087 ppt ch189781337102087 ppt ch18
9781337102087 ppt ch18
 
9781337102087 ppt ch17
9781337102087 ppt ch179781337102087 ppt ch17
9781337102087 ppt ch17
 
9781337102087 ppt ch16
9781337102087 ppt ch169781337102087 ppt ch16
9781337102087 ppt ch16
 
9781337102087 ppt ch15
9781337102087 ppt ch159781337102087 ppt ch15
9781337102087 ppt ch15
 
9781337102087 ppt ch14
9781337102087 ppt ch149781337102087 ppt ch14
9781337102087 ppt ch14
 
9781337102087 ppt ch12
9781337102087 ppt ch129781337102087 ppt ch12
9781337102087 ppt ch12
 
9781337102087 ppt ch11
9781337102087 ppt ch119781337102087 ppt ch11
9781337102087 ppt ch11
 

KĂĽrzlich hochgeladen

APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

KĂĽrzlich hochgeladen (20)

APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

9781285852744 ppt ch16

  • 2. 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 Edition 2
  • 3. 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 Edition 3
  • 4. 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 Edition 4
  • 5. 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 Edition 5
  • 6. 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 Edition 6
  • 7. Bubble Sort (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition 7
  • 8. Bubble Sort (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition 8
  • 9. Bubble Sort (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition 9
  • 10. Bubble Sort (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition 10
  • 11. 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 Edition 11
  • 12. Insertion Sort • Sorts the list by moving each element to its proper place C++ Programming: From Problem Analysis to Program Design, Seventh Edition 12
  • 13. 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 Edition 13
  • 14. Insertion Sort (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition 14
  • 15. Insertion Sort (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition 15
  • 16. Insertion Sort (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition 16
  • 17. Insertion Sort (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition 17
  • 18. 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 Edition 18
  • 19. Insertion Sort (cont’d.) • List of length n – About (n2 + 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 Edition 19
  • 20. 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 Edition 20
  • 21. Binary Search (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition 21
  • 22. 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 Edition 22
  • 23. Performance of Binary Search (cont’d.) • For a sorted list of length n: – Maximum number comparisons is 2log2n + 2 C++ Programming: From Problem Analysis to Program Design, Seventh Edition 23
  • 24. 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 Edition 24
  • 25. vector type (class) (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition 25
  • 26. vector type (class) (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition 26
  • 27. vector type (class) (cont’d.) C++ Programming: From Problem Analysis to Program Design, Seventh Edition 27
  • 28. 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<int> intList = {13, 75, 28, 35}; –Not supported by all C++11 compilers C++ Programming: From Problem Analysis to Program Design, Seventh Edition 28
  • 29. 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 Edition 29
  • 30. 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 Edition 30