M4_Ch18HomeworkQuestions(1)

.docx

School

Harvard University *

*We aren’t endorsed by this school

Course

161

Subject

Computer Science

Date

Dec 6, 2023

Type

docx

Pages

2

Uploaded by HighnessBatMaster978

Report
CSC161 Chapter 18 Homework Questions 1. Consider the following list: 5, 12, 25, 32, 38, 46, 58, 62, 85, 90, 97, 105, 110 Using the binary search as described in this chapter, how many comparisons are required to find whether the following items are in the list? Show the values of first, last, and middle and the number of comparisons after each iteration of the loop. a. 32 7 total comparisons 5,110,58 2 comparisons 5,46,25 2 comparisons 32,46,38 2 comparisons 32,32,32 1 comparison(because the value is equal) b. 20 8 total comparisons 5,110,58 2c 5,46,25 2c 5,12,5 2c 12,12,12 2c Not found because first>last c. 105 5 total comparisons 5,110,58 2c 62,110,90 2c 97,110,105 1c d. 60 6 total comparisons 5,110,58 2c 62,110,90 2c 62,85,62 2c Not found because first>last 2. Sort the following list using the bubble sort algorithm as discussed in this chapter. Show the list after each iteration of the outer for loop. 46, 58, 16, 25, 83, 98, 8, 70, 5, 62 46,16,25,58,83,8,70,5,62,98 16,25,46,58,8,70,5,62,83,98 16,25,46,8,58,5,62,70,83,98 16,25,8,46,5,58,62,70,83,98 16,8,25,5,46,58,62,70,83,98 8,16,5,25,46,58,62,70,83,98 8,5,16,25,46,58,62,70,83,98 5,8,16,25,46,58,62,70,83,98 5,8,16,25,46,58,62,70,83,98
CSC161 3. Using Assume the following list of keys: 36, 55, 89, 95, 65, 75, 13, 62, 86, 9, 23, 74, 2, 100, 98 This list is to be sorted using the quick sort algorithm as discussed in this chapter. Use pivot as the middle element of the list. a. Give the resulting list after one call to the function partition. 36,55,13,9,23,2,62,89,95,65,75,86,74,100,98 b. What is the size of the list that the function partition partitioned? 15 c. What are the sizes of the two sublists created by the function partition? 6 below and 8 above 4. Suppose that the list of keys is as given in Exercise 3. Use the quick sort algorithm, as discussed in this chapter, to determine the number of times the function partition is called to completely sort the list. 6 5. Suppose that the elements of a list are in descending order and they need to be put in ascending order. a. Write a C++ function that takes as input an array of items in descending order and the number of elements in the array. b. The function rear- ranges the element of the array in ascending order. c. Your function must not incorporate any sorting algorithms, that is, no item comparisons should take place. void reverseArray(int arr[], int numElem) { int start=0; int end=numElem-1; while (start < end) { int temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help