subject
Computers and Technology, 15.07.2021 16:50 kylee65

Given code that reads user IDs (until -1), complete the quicksort() and partition() functions to sort the IDs in ascending order using the Quicksort algorithm. Increment the global variable num_calls in quicksort() to keep track of how many times quicksort() is called. The given code outputs num_calls followed by the sorted IDs. MY CODE(please help me fix the error)
# Global variable
num_calls = 0
# TODO: Write the partitioning algorithm - pick the middle element as the
# pivot, compare the values using two index variables l and h (low and high),
# initialized to the left and right sides of the current elements being sorted,
# and determine if a swap is necessary
def partition(user_ids, low, high):
global num_calls
num_calls = num_calls + 1
i_index = (low-1)
pivot = user_ids[high]
for j_index in range(low , high):
if user_ids[j_index] <= pivot:
i_index = i_index+1
user_ids[i_index],user_ids[j_index] = user_ids[j_index],user_ids[i_index]
user_ids[i_index+1],user_ids[high] = user_ids[high], user_ids[i_index+1]
return (i_index+1)
# TODO: Write the quicksort algorithm that recursively sorts the low and
# high partitions. Add 1 to num_calls each time quisksort() is called
def quicksort(user_ids, i, k):
global num_calls
num_calls = num_calls + 1
if i < k:
pi = partition(user_ids, i,k)
quicksort(user_ids, i , pi-1)
quicksort(user_ids, pi+1, k)
if __name__ == "__main__":
user_ids = []
user_id = input()
while user_id != "-1":
user_ids. append(user_id)
user_id = input()
# Initial call to quicksort
quicksort(user_ids, 0, len(user_ids) - 1)
# Print number of calls to quicksort
print(num_calls)
# Print sorted user ids
for user_id in user_ids:
print(user_id)

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 21.06.2019 19:50
Python write an expression that executes the loop body as long as the user enters a non-negative number. note: if the submitted code has an infinite loop, the system will stop running the code after a few seconds and report "program end never reached." the system doesn't print the test case that caused the reported message. sample outputs with inputs: 9 5 2 -1
Answers: 3
question
Computers and Technology, 22.06.2019 10:30
This first part of the film shows the early history of motion pictures. how accurate do you think the portrayal of the early motion picture industry is? why? is historical accuracy important in films and theatre productions? explain. in the scene where don is going to the party (starting at time code 14: 51), we see a street scene as he first rides with cosmo and then with kathy. what aspects did the filmmaker include to make the scene look and feel like don, cosmo, and kathy are riding in a car on a street? think about elements such as scenery, sound, props, lighting, and so on. a "talkie" picture is shown starting around time code 21: 15. how does the audience in the film react to the "talkie"? what influence do audiences have on film and theatre performances? how do film and theatre actors influence audiences? in the musical scene with cosmo (starting at time code 27: 00), how does the actor use props? what is the result? do you think the use of props effectively fulfilled the artistic vision for this musical number? why or why not?
Answers: 1
question
Computers and Technology, 22.06.2019 19:20
Write a program that reads a file consisting of studentsโ€™ test scores in the range 0โ€“200. it should then determine the number of students having scores in each of the following ranges: 0โ€“24, 25โ€“49, 50โ€“74, 75โ€“99, 100โ€“124, 125โ€“149, 150โ€“174, and 175โ€“200. output the score ranges and the number of students. (run your program with the following input data: 76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189.)
Answers: 3
question
Computers and Technology, 23.06.2019 09:30
Which of the following tasks is an audio technician most likely to perform while working on a nature documentary? (select all that apply). eliminating potentially distracting background noise adding sound effects making sure the lighting is adequate for a particular scene changing the narration to better match the mood of the documentary
Answers: 3
You know the right answer?
Given code that reads user IDs (until -1), complete the quicksort() and partition() functions to sor...
Questions
question
Mathematics, 19.02.2021 23:30
question
Arts, 19.02.2021 23:30
question
Mathematics, 19.02.2021 23:30
question
Chemistry, 19.02.2021 23:30
question
English, 19.02.2021 23:30
question
Mathematics, 19.02.2021 23:30
question
Mathematics, 19.02.2021 23:30
question
World Languages, 19.02.2021 23:30
question
Mathematics, 19.02.2021 23:30
Questions on the website: 13722361