SlideShare a Scribd company logo
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

More Related Content

What's hot

Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06
Terry Yoast
 
9781285852744 ppt ch15
9781285852744 ppt ch159781285852744 ppt ch15
9781285852744 ppt ch15
Terry Yoast
 
9781285852744 ppt ch13
9781285852744 ppt ch139781285852744 ppt ch13
9781285852744 ppt ch13
Terry Yoast
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
Terry Yoast
 
9781285852744 ppt ch14
9781285852744 ppt ch149781285852744 ppt ch14
9781285852744 ppt ch14
Terry Yoast
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
MLG College of Learning, Inc
 
Matlab - Introduction and Basics
Matlab - Introduction and BasicsMatlab - Introduction and Basics
Matlab - Introduction and Basics
Techsparks
 
Chapter 2 Basics of MATLAB
Chapter 2 Basics of MATLABChapter 2 Basics of MATLAB
Chapter 2 Basics of MATLAB
Pranoti 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 Data
Nicole Ryan
 
Lex & yacc
Lex & yaccLex & yacc
Lex & yacc
Taha Malampatti
 
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 Scala
Stan Lea
 
Yacc
YaccYacc
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
Sam Bowne
 
Lex Tool
Lex ToolLex Tool
Lex Tool
Rajan 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 & YACC
LEX & YACCLEX & YACC
LEX & YACC
Mahbubur Rahman
 
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: Modularity
Nicole Ryan
 

What's hot (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
 

Viewers also liked

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 tools
Abdullah Al-hazmy
 
Data Structures- Part8 stacks and queues
Data Structures- Part8 stacks and queuesData Structures- Part8 stacks and queues
Data Structures- Part8 stacks and queues
Abdullah 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 simplified
Abdullah Al-hazmy
 
Applying Design Patterns in Practice
Applying Design Patterns in PracticeApplying Design Patterns in Practice
Applying Design Patterns in Practice
Ganesh Samarthyam
 
AWS ECS Quick Introduction
AWS ECS Quick IntroductionAWS ECS Quick Introduction
AWS ECS Quick Introduction
Vinothini Raju
 
Web Database
Web DatabaseWeb Database
Web Database
idroos7
 
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
Garth Gilmour
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
Abdullah 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 Level
Nikita Lipsky
 
Advanced Java - Praticals
Advanced Java - PraticalsAdvanced Java - Praticals
Advanced Java - Praticals
Fahad 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 World
Ramit 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 recursion
Abdullah Al-hazmy
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
Iram 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 API
Ganesh 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 Series
Amazon Web Services
 

Viewers also liked (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
 

Similar to 9781285852744 ppt ch16

9781423902096_PPT_ch09.ppt
9781423902096_PPT_ch09.ppt9781423902096_PPT_ch09.ppt
9781423902096_PPT_ch09.ppt
LokeshK66
 
ch9.ppt
ch9.pptch9.ppt
9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt
LokeshK66
 
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
midtushar
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
Terry Yoast
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
Burhan Fakhar
 
PPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.pptPPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.ppt
masadjie
 
C++.ppt
C++.pptC++.ppt
C++.ppt
shweta210
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt
LokeshK66
 
Algorithms.
Algorithms. Algorithms.
UNEC__1683196273.pptx
UNEC__1683196273.pptxUNEC__1683196273.pptx
UNEC__1683196273.pptx
huseynmusayev2
 
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
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
9780324782011_PPT_ch09.ppt
9780324782011_PPT_ch09.ppt9780324782011_PPT_ch09.ppt
9780324782011_PPT_ch09.ppt
ValhallaExcalibur
 
Unit 1 linked list
Unit 1 linked listUnit 1 linked list
Unit 1 linked list
LavanyaJ28
 
a581a6a2cb5778045788f0b1d7da1c0236f.pptx
a581a6a2cb5778045788f0b1d7da1c0236f.pptxa581a6a2cb5778045788f0b1d7da1c0236f.pptx
a581a6a2cb5778045788f0b1d7da1c0236f.pptx
christinamary2620
 
RECURSION.pptx
RECURSION.pptxRECURSION.pptx
RECURSION.pptx
PrestonKhumbula1
 

Similar to 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
 
PPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.pptPPT slide_chapter 02 Basic element of C++.ppt
PPT slide_chapter 02 Basic element of C++.ppt
 
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
 

More from Terry Yoast

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

More from 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
 

Recently uploaded

"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 

Recently uploaded (20)

"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 

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