CNSP - Lecture #8 Chapter 9
BY PROF. RAFAEL ORTA
Disclosure: this presentation includes slides that were provided by the textbook publisher and Dr. Hnatyshin, some are adaptation, and some are an exact replica.
7.12 Structures
8.1 Arrays Hold Multiple Values
8.2 Accessing Array Elements
8.3 Inputting and Displaying Array Contents
8.4 Array Initialization
8.5 The Range-Based for loop
8.6 Processing Array Contents
8.7 Using Parallel Arrays
Last class we covered�
8.9 Arrays as Function Arguments
8.10 Two-Dimensional Arrays
8.11 Arrays with Three or More Dimensions
8.12 Introduction to the STL vector
8.13 Arrays of Objects
Last class we covered�
Topics
9.1 Introduction to Search Algorithms
9.2 Searching an Array of Objects
9.3 Introduction to Sorting Algorithms
9.4 Sorting an Array of Objects
9.5 Sorting and Searching Vectors
9.6 Introduction to Analysis of Algorithms
9.1 Introduction to Search Algorithms
Linear Search Algorithm
Set found to false
Set position to –1
Set index to 0
While index < number of celts and found is false
If list [index] is equal to search value
found = true
position = index
End If
Add 1 to index
End While
Return position
Linear Search Example
search examines 17, 23, 5, 11, 2, 29, and 3
Linear Search Tradeoffs
Binary Search Algorithm
Binary search requires that the array is in order.
Binary Search Example
search examines 11, 3, 5, then stops
Binary Search Tradeoffs
9.2 Searching an Array of Objects
9.3 Introduction to Sorting Algorithms
Bubble Sort Algorithm
Bubble Sort Example 1 of 3
Array numlist3 contains
Compare values 17 and 23. They are in order, so no exchange is needed.
Compare values 23 and 5. Exchange them.
Compare values 23 and 11. Exchange them.
This is the end of the first pass of sorting using Bubble Sort.
Bubble Sort Example 2 of 3
The second pass starts with the array from pass one
Compare values 17 and 5. Exchange them.
Compare 17 and 11. Exchange them.
Compare 17 and 23. No exchange is needed.
This is the end of the second pass.
Bubble Sort Example 3 of 3
The third pass starts with the array from pass two
Compare values 5 and 11. No exchange is needed.
Compare values 11 and 17. No exchange is needed.
Compare values 17 and 23. No exchange is needed.
It ends after N-1 passes.
Bubble Sort Tradeoffs
An Improved Bubble Sort
NOTE: The classic Bubble Sort doesn’t care if the array is already sorted — it always runs n−1 full passes no matter what.
Selection Sort Algorithm
Selection Sort Example 1 of 2
Array numlist contains
The smallest element is 2. Exchange 2 with the element at subscript 0. The element in position 0 is now in order
Selection Sort Example 2 of 2
The next smallest element is 3. Exchange 3 with the element at subscript 1. The element in position 1 is now in order.
The next smallest element is 11. Exchange 11 with the element at subscript 2.
Sorting Considerations
9.4 Sorting an Array of Objects
9.5 Sorting and Searching Vectors
9.6 Introduction to Analysis of Algorithms
Analysis of Algorithms: Terminology 1 of 3
Analysis of Algorithms: Terminology 2 of 3
Complexity Example
Analysis:
Lines 1 and 2 execute once.
The test in line 3 executes n times.
The test in line 4 executes n times.
The assignment in line 6 executes at most n times.
Due to lines 3 and 4, the algorithm requires execution time proportional to n.
Find the largest value in array A of size n
Analysis of Algorithms: Terminology 3 of 3
Asymptotic Notation