Question

C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential...

C++ Sorting and Searching

1. Mark the following statements as true or false.

a. A sequential search of a list assumes that the list elements are sorted in ascending

order.

b. A binary search of a list assumes that the list is sorted.

2. Consider the following list:
63 45 32 98 46 57 28 100
Using a sequential search, how many comparisons are required to determine whether the following items are in the list or not? (Recall that comparisons mean item comparisons, not index comparisons.)
a. 90 b. 57 c. 63 d. 120

3. Consider the following list: 2 10 17 45 49 55 68 85 92 98 110
Using the binary search, how many comparisons are required to determine whether the following items are in the list or not? Show the values of first, last, and middle and the number of comparisons after each iteration of the loop.

a. 15 b. 49 c. 98 d. 99

4. Sort the following list using the bubble sort algorithm. Show the list after each iteration of the outer for loop.
26, 45, 17, 65, 33, 55, 12, 18

5. Sort the following list using the selection sort algorithm. Show the list after each iteration of the outer for loop.
36, 55, 17, 35, 63, 85, 12, 48, 3, 66

6. Consider the following list: 5, 18, 21, 10, 55, 20
The first three keys are in order. To move 10 to its proper position using the insertion sort as described in this chapter, exactly how many key comparisons are executed?

7. Consider the following list: 7, 28, 31, 40, 5, 20
The first four keys are in order. To move 5 to its proper position using the insertion sort as described in this chapter, exactly how many key comparisons are executed?

8. Consider the following list: 28, 18, 21, 10, 25, 30, 12, 71, 32, 58, 15
This list is to be sorted using the insertion sort algorithm. Show the resulting list after six

passes of the sorting phase – that is, after six iterations of the for loop.

9. Perform the insertion sort algorithm using the following list of keys: 18, 8, 11, 9, 15, 20, 32, 61, 22, 48, 75, 83, 35, 3
Show the list after each iteration. Exactly how many key comparisons are executed to sort this list using insertion sort?

10. a. The performance of bubble sort can be improved if we stop the sorting process as soon as we find that in an iteration, no swapping of elements takes place. Write a function that implements bubble sort algorithm using this fact.
b. Using the algorithm that you designed in part (a), find the number of iterations that are needed to sort the list: 65, 14, 52, 43, 75, 25, 80, 90, 95.

11. Suppose that L is a sorted list of 4096 elements. What is the maximum number of comparisons made by binary search to determine whether an item is in L?

12. Suppose that the elements of a list are in descending order, and they need to be put in ascending order. Write a C++ function that takes as input an array of items in descending order and the number of elements in the array. The function must not incorporate any sorting algorithms, that is, no item comparisons should take place.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

A sequential search of a list assumes that the list elements are sorted in ascending
1. a) false
Sequential search means checking element by element only and not necessarily sorted array

b. A binary search of a list assumes that the list is sorted.
1. b) true
Concept of binary search is that it has sorted elemets and finding the element takes lesser time than that of linear search.

63 45 32 98 46 57 28 100

2. a) 90
8 comparisions
Since 90 is not there in given list, it will check all the items. So, 8.

2. b) 57
6 comparisions
57 can be found at 6th comparsion and there it will stop. So, 6

Add a comment
Know the answer?
Add Answer to:
C++ Sorting and Searching 1. Mark the following statements as true or false. a. A sequential...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 4: Sorting and Searching (55 points) a) (20 points) Scarch for the character K using...

    Problem 4: Sorting and Searching (55 points) a) (20 points) Scarch for the character K using the binary search algorithm on the following array of characters: KM 2. R For each iteration of binary search use the table below to list: (a) the left index and (b) the right index of the array that denote the region of the array that is still being searched, (c) the middle point index of the array, and (d) the number of character-to-character comparisons...

  • Recall the insertion sort algorithm as discussed in this chapter. Assume the following list of keys:...

    Recall the insertion sort algorithm as discussed in this chapter. Assume the following list of keys: 30, 20, 35, 27, 96, 82, 60, 48, 75, 5, 80 Exactly how many key comparisons are executed to sort this list using insertion sort? Show the values and the number of comparisons after each iteration for the loop.

  • Java We did in lecture, and explain your answer briefly. Problem 4: Sorting practice 14 points;...

    Java We did in lecture, and explain your answer briefly. Problem 4: Sorting practice 14 points; 2 points for each part individual-only Important: When answering these questions, make sure to apply the versions of these algorithms that were discussed in lecture. The Java code for each algorithm can be found in our Sort class. Given the following array: {14, 7, 27, 13, 24, 20, 10, 33 1. If the array were sorted using selection sort, what would the array look...

  • Puodace a char showing the number of moves required to solve the Towers of Hanoi puzle using t 30...

    Puodace a char showing the number of moves required to solve the Towers of Hanoi puzle using t 30. What is an execution frame? What is an activation record? What is contained in it? 31. Write a recursive Java method that computes the factorial of a number 2. Linear search may require up to comparisons while binary search will only require roughly comparisons 33. Sort sorts a list of values by repetitively putting a particular value into its final, sorted,...

  • Which of the following statements about sequential search is true? a. The list must be a...

    Which of the following statements about sequential search is true? a. The list must be a sorted list. b. It works only on a list of integers. c. When the search target is not in the list, every element must be tested d. The search typically starts at the middle of the list and searches on either side of the middle. e. None of the above. Consider searching for a target value of 44 in the sorted list given below...

  • When you look closely at the Insertion Sort you will notice that the insertion of the...

    When you look closely at the Insertion Sort you will notice that the insertion of the next element into the already sorted list basically does a sequential search to determine where the element should be inserted. Consider a variation of the standard Insertion Sort algorithm that does a binary search on the already sorted part of the list to determine the location where the element should be inserted. Assuming that the keys are unique, note that a standard binary search...

  • 1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching...

    1. a. Using C++, represent the following graph using adjacency matrix, and implement depth first searching (DFS) by stack (define it with class) to traverse the graph. 6 7 2 4 b. If starting with node 2, when node 7 is printed, what numbers are in the stack (for DFS)? Please draw the stack step by step to show how the numbers are pushed into and popped out of it. 2. a. Given a set of integer numbers as int...

  • //Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded....

    //Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded. However, //for these algorithms to work correctly, the data objects must //be compared using the method compareTo and equals. //In other words, the classes implementing the list objects //must implement the interface Comparable. The type parameter T //is unbounded because we would like to use these algorithms to //work on an array of objects as well as on objects of the classes //UnorderedArrayList and...

  • Benchmark Searching and Sorting Write a program that has an array of at least 20 strings...

    Benchmark Searching and Sorting Write a program that has an array of at least 20 strings that you will have your user enter. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep...

  • Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of...

    Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of data and arranging items in an ascending/descending order. you will also explore storing lists/arrays using different sorting algorithms including, the selection sort, bubble sort, and insertion sort algorithm. Comparison between the three algorithms are made based on the number of comparisons and item assignments (basic operations) each algorithms executes. Background: Ordering the elements of a list is a problem that occurs in many computer...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT