M4_Ch18HomeworkQuestions(1)
.docx
keyboard_arrow_up
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
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
Related Questions
def eliminate_neighbours (items):
Given the sequence of integer items that are guaranteed to be some permutation of positive
integers from 1 to n where n is the length of the list, find the smallest number among those that still
remain in the list, and remove from the list both that number and whichever of its current
immediate neighbours is larger. The function should repeat this basic operation until the largest
number in the original list gets eliminated. Return the number of removal operations that were
needed to achieve this goal.
For example, given the list [5, 2, 1, 4, 6, 31, the operation would remove element 1 and its
current larger neighbour 4, resulting in the list [5, 2, 6, 3]. Applied again, that operation
would remove 2 and its current larger neighbour 6, thus reaching the goal in two steps.
items
Expected result
[1, 6, 4, 2, 5, 3]
1
[8, 3, 4, 1, 7, 2, 6, 5]
3
[8, 5, 3, 1, 7, 2, 6, 4]
4
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
5
range (1, 10001)
5000
[1000] + list(range (1, 1000))…
arrow_forward
That's enough of you!
def remove_after_kth(items, k=1):
Given a list of items, some of which may be duplicated, create and return a new list that is otherwise the same as items, but only up to k occurrences of each element are kept, and all occurrences of that element after those first k are discarded.Hint: loop through the items, maintaining a dictionary that remembers how many times you have already seen each element. Update this count as you go, and append each element to the result list only if its count is still at most equal to k.
items
k
Expected result
[42, 42, 42, 42, 42, 42, 42]
3
[42, 42, 42]
['tom', 42, 'bob', 'bob', 99, 'bob', 'tom', 'tom', 99]
2
['tom', 42, 'bob', 'bob', 99, 'tom', 99]
[1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1]
1
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5]
3
[1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 1, 5]
[42, 42, 42, 99, 99, 17]
0
[]
arrow_forward
That's enough of you! def remove_after_kth(items, k=1):
Given a list of items, some of which may be duplicated, create and return a new list that is otherwise the same as items, but only up to k occurrences of each element are kept, and all occurrences of that element after those first k are discarded.
Hint: loop through the items, maintaining a dictionary that remembers how many times you have already seen each element. Update this count as you go, and append each element to the result list only if its count is still at most equal to k.
Note the counterintuitive and yet completely legitimate edge case of k==0 that has the well defined and unambiguously correct answer of an empty list! Once again, an often missed and yet so very important part of becoming a programmer is learning to perceive zero as a number...
arrow_forward
Q2: a. Write an algorithm that searches a sorted list of n items by dividing it into three sublists of almost n/3 items.
This algorithm finds the sublist that might contain the given item and divides it into three smaller sublists of
almost equal size. The algorithm repeats this process until it finds the item or concludes that the item is not in
the list. Dry run the above algorithm to find the value 240.
A[] = {10,15,20,60,65,110,150,220,240,245,260,290,300,460,470,501}
arrow_forward
L = ['jon', 'bran', 'ghost', 'ned', 'sansa', 'arya', 'tyrion', 'drogon']
In the table below, show the contents of the list after each of the first four iterations of the for-loop in selection_sort
arrow_forward
Question in image
Please explain how to do with the answer.
Thank you.
arrow_forward
Solve Question 38, 39 showing detailly all the steps with including all explanations
Answer Should be typewritten using a computer keyboard!
arrow_forward
12. Write a for loop that sums the odd values from the
LIST_SIZE element array
list. For example, the sum for this list would be 113 (51
+ 17 + 45).
Array 11at
liat (0)1iat tal 1nt ta) liat (11iet14] 1iat (
30
12
51
17
45
62
arrow_forward
Considering the linear linked list
Assume that a linked list will be used to store the identity of the students as
Student Identity:
(St. Name
St. Id
St. CGPA
Create a for loop that the user can insert 4 students from the command window
Insert the new list by the method of insertion at the end of the list
arrow_forward
Assume you have a list with exactly 15 elements:
[31, 32, 33, 34, 35, 36, 37, 38, 39, 50, 61, 62, 63, 64, 75]
If we perform binary search for an element, list all the elements that will be found by examining two or fewer numbers from the list
Please list all the elements that will be found by examining two or fewer numbers from the list
arrow_forward
In binary search:
A colors list is searched for Tan using binary search. Colors list: ( Beige, Blue, Brown, Gray, Indigo, Magenta, Maroon, Orange, Tan, Teal )
What is the first color searched?
What is the second color searched?
arrow_forward
2) Hash Innards
Homework • Unanswered
Select all true statements from the below.
Multiple answers: Multiple answers are accepted for this question
Select one or more answers and submit. For keyboard navigation. SHOW MORE V
a
A hash function takes a key and produces an index into the hash table.
The next step in this process is often something like 'h%SIZE' so that the hash value of the key will fit within the table
b
(having SIZE elements, you see).
Common techniques involve exclusive or of bits within the key and folding different sections of bits within the key into
each other.
The best hash method for character strings is to simply add up the ASCIlI values of their individual characters.
Coming up with a perfect hash for a given set of keys can be a difficult and time-consuming task.
arrow_forward
Alert - don't use any AI platform to generate my answer and don't try to copy anything from other websites otherwise I'll reduce rating for sure and report. Python query
arrow_forward
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.
26, 45, 17, 65, 33, 55, 12, 18
arrow_forward
Binary search outperforms sequential, but how do you choose?
arrow_forward
Using the Select kth element algorithm discussed in the class to find the 8th element from the list
of 25 integers below. Show the steps up to the partition of the list into two lists L and R, and
indicate which list (L or R) you will continue according to the select kth element algorithm.
7,2, 106, 99, 120, 41, 18, 113, 112, 110, 111, 110, 110, 112, 115, 1, 3, 99, 120, 190, 3, 4, 116,
129, 800.
arrow_forward
Sort the following list using the Selection Sort algorithm . Show the list after each iteration of the outer for loop (after each complete pass through the list)
IMPORTANT: Separate each value by a comma and only one space after each comma and no space after the last value.
You will have seven iterations of the list for your answer.
38, 60, 43, 5, 70, 58, 15, 10
arrow_forward
True/False 9. The add method can be used to add an item to the end of a list.
arrow_forward
Question 8
Sort the following numbers using an "in place" version of a selection sort. This
means that you should have only one array throughout and all elements should be
present at all times. Show each "pass" of the algorithm.
34, 25, 11, 44, 21, 8, 4, 28, 16, 31
arrow_forward
Pull down your neighbours
def eliminate_neighbours(items):
Given the sequence of integer items that are guaranteed to be some permutation of positive integers from 1 to n where n is the length of the list, find the smallest number among those that still remain in the list, and remove from the list both that number and whichever of its current immediate neighbours is larger. The function should repeat this basic operation until the largest number in the original list gets eliminated. Return the number of removal operations that were needed to achieve this goal.For example, given the list [5, 2, 1, 4, 6, 3], the operation would remove element 1 and its current larger neighbour 4, resulting in the list [5, 2, 6, 3]. Applied again, that operation would remove 2 and its current larger neighbour 6, thus reaching the goal in two steps.
items
Expected result
[1, 6, 4, 2, 5, 3]
1
[8, 3, 4, 1, 7, 2, 6, 5]
3
[8, 5, 3, 1, 7, 2, 6, 4]
4
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
5
range(1,…
arrow_forward
Question2. Given the following linked list of integers named myList:
7
11
b. Write an enhanced for loop to print the elements in the list myList.
(Ctrl)-
20
arrow_forward
This code gets an error for going out of range in the last for loop. What can I do to fix this?
# Lucas Conklin# 5772707import csvimport statistics
def readCSVIntoDictionary(f_name): data = [] with open(f_name) as f: reader = csv.reader(f) for row in reader: if not data: for index in range(len(row)): data.append([]) for index in range(len(row)): data[index].append(float(row[index])) f.close() return data
features = readCSVIntoDictionary("C:\\Users\\lucas\\Downloads\\pima.csv")print(features)
def find_median_and_SD(data, feature): med = statistics.median(data[feature]) rounded_med = round(med, 4) st_dev = statistics.stdev(data[feature]) rounded_st_dev = round(st_dev, 5) return rounded_med, rounded_st_dev
for i in range(0, len(features)): (median, st_dev) = find_median_and_SD(features, i) print(f'Feature {i} Median: {median} Standard Deviation: {st_dev}')…
arrow_forward
Assignment 1:
Prompt the user for 5 pairs of numbers. The pairs consist of a player’s jersey number (0-99) and the player’s rating (1-9). Make sure to use good prompts and check the user’s input using a while loop. Do not stop the program if the input is outside the range. You need to prompt the user until they get the numbers correct. Store the pairs in a dictionary. Since the player’s jersey number is a key, you need to check for duplicates and prompt user again if the number is already in use. After all players are entered, print the roster with the jersey numbers in ascending order.
Next, print a menu for the user to be able to modify the roster. They should be able to add a new player, remove a player, update a player’s rating, output a list of players above a rating (get the cutoff from the user), output the roster, or quit.
You need to turn in a written algorithm for this project before starting to write the program.
Assignment 2:
Change assignment 1 to use functions.…
arrow_forward
DevC++ Code (paste the link to browser):https://paste.ofcode.org/K3uqRBy7z65V4exvcrShgF
Focus on the main (driver program) or int main (), just make a menu driven program on how the following program works.
Laboratory Task 8:Create a menu-driven program that will properly insert a new element to the Binary Search Tree (BST). The options of the program are the following:1. Insert Element2. Display Binary Search Tree3. Exit
Option (1) inserts a new element to its proper location in the BST. Take note of the following properties of the Binary Search Tree:The left subtree of a node contains only nodes with elements lesser than the element. The right subtree of a node contains only nodes with elements greater than the node s element.The left and right subtree each must also be a BST. There must be noduplicate nodes.Option (2) displays all the elements in the BST in proper order.Option (3) exits the program.
arrow_forward
Background: When searching for an item in a list, each item that we examine (compare) is considered to be interrogated.
If we search for John, the following names are interrogated: Harry, Larry, John (in that order). If two names tie for the middle position, choose the first of the two names for the middle.If we search this same list for John using the Sequential search we would interrogate all the names from Alice through John. We would start with Alice, move to Bob, move to Carol and so forth until we reached John.
Directions: Use the original list of names (Alice - Oliver) to answers questions 1-8.
Using a sequential search, what names are interrogated to find Carol?
Using a sequential search, what names are interrogated to determine that Sam is not in the list?
Using a binary search, what names are interrogated to find Carol?
Using a binary search, what names are interrogated to determine that Sam is not in the list?
Will a binary search or sequential search find Alice…
arrow_forward
In order to print each item in a list, create a function called Multiply2 that multiplies the list's elements by 2. For instance: mylist=[1,2,3,4] In this case, multiply2(mylist) returns the following results: 2 4 6 8 The function requires just one argument, which is the list on which the operation is to be done.
arrow_forward
Write a version of the sequential search algorithm that can be used to search a sorted list.
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Related Questions
- def eliminate_neighbours (items): Given the sequence of integer items that are guaranteed to be some permutation of positive integers from 1 to n where n is the length of the list, find the smallest number among those that still remain in the list, and remove from the list both that number and whichever of its current immediate neighbours is larger. The function should repeat this basic operation until the largest number in the original list gets eliminated. Return the number of removal operations that were needed to achieve this goal. For example, given the list [5, 2, 1, 4, 6, 31, the operation would remove element 1 and its current larger neighbour 4, resulting in the list [5, 2, 6, 3]. Applied again, that operation would remove 2 and its current larger neighbour 6, thus reaching the goal in two steps. items Expected result [1, 6, 4, 2, 5, 3] 1 [8, 3, 4, 1, 7, 2, 6, 5] 3 [8, 5, 3, 1, 7, 2, 6, 4] 4 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 5 range (1, 10001) 5000 [1000] + list(range (1, 1000))…arrow_forwardThat's enough of you! def remove_after_kth(items, k=1): Given a list of items, some of which may be duplicated, create and return a new list that is otherwise the same as items, but only up to k occurrences of each element are kept, and all occurrences of that element after those first k are discarded.Hint: loop through the items, maintaining a dictionary that remembers how many times you have already seen each element. Update this count as you go, and append each element to the result list only if its count is still at most equal to k. items k Expected result [42, 42, 42, 42, 42, 42, 42] 3 [42, 42, 42] ['tom', 42, 'bob', 'bob', 99, 'bob', 'tom', 'tom', 99] 2 ['tom', 42, 'bob', 'bob', 99, 'tom', 99] [1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1] 1 [1, 2, 3, 4, 5] [1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5] 3 [1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 1, 5] [42, 42, 42, 99, 99, 17] 0 []arrow_forwardThat's enough of you! def remove_after_kth(items, k=1): Given a list of items, some of which may be duplicated, create and return a new list that is otherwise the same as items, but only up to k occurrences of each element are kept, and all occurrences of that element after those first k are discarded. Hint: loop through the items, maintaining a dictionary that remembers how many times you have already seen each element. Update this count as you go, and append each element to the result list only if its count is still at most equal to k. Note the counterintuitive and yet completely legitimate edge case of k==0 that has the well defined and unambiguously correct answer of an empty list! Once again, an often missed and yet so very important part of becoming a programmer is learning to perceive zero as a number...arrow_forward
- Q2: a. Write an algorithm that searches a sorted list of n items by dividing it into three sublists of almost n/3 items. This algorithm finds the sublist that might contain the given item and divides it into three smaller sublists of almost equal size. The algorithm repeats this process until it finds the item or concludes that the item is not in the list. Dry run the above algorithm to find the value 240. A[] = {10,15,20,60,65,110,150,220,240,245,260,290,300,460,470,501}arrow_forwardL = ['jon', 'bran', 'ghost', 'ned', 'sansa', 'arya', 'tyrion', 'drogon'] In the table below, show the contents of the list after each of the first four iterations of the for-loop in selection_sortarrow_forwardQuestion in image Please explain how to do with the answer. Thank you.arrow_forward
- Solve Question 38, 39 showing detailly all the steps with including all explanations Answer Should be typewritten using a computer keyboard!arrow_forward12. Write a for loop that sums the odd values from the LIST_SIZE element array list. For example, the sum for this list would be 113 (51 + 17 + 45). Array 11at liat (0)1iat tal 1nt ta) liat (11iet14] 1iat ( 30 12 51 17 45 62arrow_forwardConsidering the linear linked list Assume that a linked list will be used to store the identity of the students as Student Identity: (St. Name St. Id St. CGPA Create a for loop that the user can insert 4 students from the command window Insert the new list by the method of insertion at the end of the listarrow_forward
- Assume you have a list with exactly 15 elements: [31, 32, 33, 34, 35, 36, 37, 38, 39, 50, 61, 62, 63, 64, 75] If we perform binary search for an element, list all the elements that will be found by examining two or fewer numbers from the list Please list all the elements that will be found by examining two or fewer numbers from the listarrow_forwardIn binary search: A colors list is searched for Tan using binary search. Colors list: ( Beige, Blue, Brown, Gray, Indigo, Magenta, Maroon, Orange, Tan, Teal ) What is the first color searched? What is the second color searched?arrow_forward2) Hash Innards Homework • Unanswered Select all true statements from the below. Multiple answers: Multiple answers are accepted for this question Select one or more answers and submit. For keyboard navigation. SHOW MORE V a A hash function takes a key and produces an index into the hash table. The next step in this process is often something like 'h%SIZE' so that the hash value of the key will fit within the table b (having SIZE elements, you see). Common techniques involve exclusive or of bits within the key and folding different sections of bits within the key into each other. The best hash method for character strings is to simply add up the ASCIlI values of their individual characters. Coming up with a perfect hash for a given set of keys can be a difficult and time-consuming task.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education