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, 22.06.2019 06:00
Which of the following is a way that advancements in technology improve humans' ability to measure air quality and protect human health? a. air quality data gathered by new instruments can be used to reduce exposure to pollutants. b. new technologies enable natural and human-made pollution sources to be located and monitored. c. air quality data gathered by new instruments can be used to predict trends in pollution over time. d. new technologies allow governments to make informed decisions about the regulation of pollution. (choose more than one)
Answers: 2
question
Computers and Technology, 22.06.2019 19:10
10. when you create a pivottable, you need to specify where to find the data for the pivottable. is it true
Answers: 2
question
Computers and Technology, 23.06.2019 05:00
Jason works as an accountant in a department store. he needs to keep a daily record of all the invoices issued by the store. which file naming convention would him the most?
Answers: 2
question
Computers and Technology, 23.06.2019 11:00
In the context of the box model, what is the difference between a margin and a padding? a. a padding lies outside a box border, while a margin lies inside it. b. a padding lies inside a box border, while a margin lies outside it. c. a padding can be adjusted independently, while a margin depends on the size of its box. d. a padding depends on the size of its box, while a margin can be adjusted independently.
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, 12.01.2021 16:20
question
Mathematics, 12.01.2021 16:20
question
Chemistry, 12.01.2021 16:20
question
Spanish, 12.01.2021 16:20
question
Mathematics, 12.01.2021 16:20
Questions on the website: 13722367