1 of 35

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.

2 of 35

3 of 35

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

4 of 35

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

5 of 35

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

6 of 35

9.1 Introduction to Search Algorithms

  • Search: to locate a specific item in a list (array, vector, etc.) of information
  • Two algorithms (methods) considered here:
    • Linear search (also called sequential search)
    • Binary search

7 of 35

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

8 of 35

Linear Search Example

  • Array numlist contains

  • Searching for the value 11, linear search examines 17, 23, 5, and 11
  • Searching for the value 7, linear

search examines 17, 23, 5, 11, 2, 29, and 3

9 of 35

Linear Search Tradeoffs

  •  

10 of 35

Binary Search Algorithm

Binary search requires that the array is in order.

  1. Examine the value of the middle element
  2. If the middle element has the desired value, done. Otherwise, determine which half of the array may have the desired value. Continue working with this portion of the array.
  3. Repeat steps 1 and 2 until either the element with the desired value is found or there are no more elements to examine.

11 of 35

Binary Search Example

  • Array numlist2 contains

  • Searching for the value 11, binary search examines 11 and stops
  • Searching for the value 7, binary

search examines 11, 3, 5, then stops

12 of 35

Binary Search Tradeoffs

  •  

13 of 35

9.2 Searching an Array of Objects

  • Search algorithms are not limited to arrays of integers
  • When searching an array of objects or structures, the value being searched for is a member of an object or structure, not the entire object or structure
  • Member in object/structure: key field
  • Value used in search: search key

14 of 35

9.3 Introduction to Sorting Algorithms

  • Sort: arrange values into an order
    • Alphabetical
    • Ascending (smallest to largest) numeric
    • Descending (largest to smallest) numeric
  • Two algorithms considered here
    • Bubble sort
    • Selection sort

15 of 35

Bubble Sort Algorithm

    • Compare 1st two elements and exchange them if they are out of order.
    • Move down one element and compare 2nd and 3rd elements. Exchange if necessary. Continue until the end of the array.
    • Pass through the array again, repeating the process and exchanging as necessary.
    • Repeat until a pass is made with no exchanges.

16 of 35

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.

17 of 35

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.

18 of 35

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.

19 of 35

Bubble Sort Tradeoffs

  • Benefit
    • It is easy to understand and to implement
  • Disadvantage
    • It is inefficient due to the number of exchanges. This makes it slow for large arrays (n-1) passes.

20 of 35

An Improved Bubble Sort

  • If no exchanges occur in a pass using Bubble Sort, then the array must already be in order.
  • This can be incorporated into the Bubble Sort algorithm:
    • detect if an exchange was made in the inner for loop and set a boolean flag.
    • test the flag in the outer for loop and terminate execution if no exchanges were made.

NOTE: The classic Bubble Sort doesn’t care if the array is already sorted — it always runs n−1 full passes no matter what.

21 of 35

Selection Sort Algorithm

  1. Locate the smallest element in the array and exchange it with the element in position 0.
  2. Locate the next smallest element in the array and exchange it with element in position 1.
  3. Continue until all of the elements are in order.

22 of 35

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

23 of 35

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.

24 of 35

Sorting Considerations

  • Although it does not have Bubble Sort’s ability to terminate the sort when it detects that all elements are in order, Selection Sort is considered more efficient than Bubble Sort.
  • This is due to the fewer number of exchanges that occur in Selection Sort.

25 of 35

9.4 Sorting an Array of Objects

  • As with searching, arrays to be sorted can contain objects or structures
  • The key field determines how the structures or objects will be ordered
  • When exchanging the contents of array elements, entire structures or objects must be exchanged, not just the key fields in the structures or objects

26 of 35

9.5 Sorting and Searching Vectors

  • Sorting and searching algorithms can be applied to vectors as well as to arrays
  • You need slight modifications to the sorting functions to use vector arguments
    • vector <type> & used in prototype
    • There is no need to indicate the vector size, as functions can use the vector’s size member function

27 of 35

28 of 35

9.6 Introduction to Analysis of Algorithms

  • Given two algorithms to solve a problem, what makes one better than the other?
  • Efficiency of an algorithm is measured by
    • space (computer memory used)
    • time (how long to execute the algorithm)
  • Analysis of algorithms is a more effective way to find efficiency than by using empirical data

29 of 35

30 of 35

Analysis of Algorithms: Terminology 1 of 3

  • Computational Problem: a problem solved by an algorithm
  • Instance of the Problem: a specific problem that is solved by the algorithm
  • Size of an Instance: the amount of memory needed to hold the data for the specific problem

31 of 35

Analysis of Algorithms: Terminology 2 of 3

  • Basic step: an operation in the algorithm that executes in a constant amount of time
  • Examples of basic steps:
    • exchange the contents of two variables
    • compare two values
  • Non-example of a basic step:
    • find the largest element in an array

32 of 35

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

  1. biggest = A[0]
  2. indx = 0
  3. while (indx < n) do
  4. if (A[n] > biggest)
  5. then
  6. biggest = A[n]
  7. end if
  8. end while

33 of 35

Analysis of Algorithms: Terminology 3 of 3

  • Complexity of an algorithm: the number of basic steps required to execute the algorithm for an input of size N (N = number of input values)
  • Worst-case complexity of an algorithm (Big O): the number of basic steps for input of size N that requires the most work
  • Average case complexity function (Big Theta) : the complexity for typical, average inputs of size N
  • Best case complexity function (Big Omega) : the complexity for best inputs of size N.

34 of 35

Asymptotic Notation

35 of 35