COSC 1337 - Programming Fundamentals II
Chapter 9 Searching, Sorting and Algorithm Analysis
Review Exercises
I recommend that you review the chapter by doing these exercises. I will NOT grade these exercises.
Consider reviewing the Checkpoint questions from this chapter.
Answers to Checkpoint questions are in Appendix P (on the CD that came with your textbook).
Review Questions and Exercises: 1, 3, 5, 11, 15.
Answers to Review Questions and Exercises are in Appendix Q (on the CD that came with your textbook).
Other Questions
1. Write a C++ function that searches an integer array that is sorted in increasing order and returns the index of the first element that is larger than a specified search value. The search value, the list, and the size of the list should be parameters to the function.
2. Why is it more efficient to search a long list using binary search as opposed to sequential search?
3. The function shown below will sort an integer array in ascending (increasing) order. That is, after calling sort the smallest element will be at index 0, the next smallest at index 1, etc. What would you need to change to sort the array in descending (decreasing) order?
void sort( int list[], int length ) { int index, temp; bool sorted = false; while ( !sorted ) { sorted = true for ( index = 0; index < length - 1; index++ ) if ( list[index] > list[index+1] ) { temp = list[index]; list[index] = list[index+1]; list[index+1] = temp; sorted = false; } } }
Answers to Other Questions are here.
© Austin Community College 2016. The content on this page is licensed under a
Creative Commons Attribution 4.0 International License.
Last updated: July 13, 2013