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 20:30
To display data in a certain manner like alphabetical order is called
Answers: 1
question
Computers and Technology, 24.06.2019 02:10
Consider the usual algorithm to convert an infix expression to a postfix expression. suppose that you have read 10 input characters during a conversion and that the stack now contains these symbols: (5 points) | | | + | | ( | bottom |_*_| now, suppose that you read and process the 11th symbol of the input. draw the stack for the case where the 11th symbol is
Answers: 2
question
Computers and Technology, 24.06.2019 05:00
Who is most likely be your target audience if you create a slide presentation that had yellow background and purple text
Answers: 2
question
Computers and Technology, 24.06.2019 08:30
Aconsumer would pay an extra they used the rent to own program to buy the computer, rather than using cash. for all of the items, is the cheapest option over the life of the contract. the most expensive overall option is to use purchase the item.
Answers: 2
You know the right answer?
Given code that reads user IDs (until -1), complete the quicksort() and partition() functions to sor...
Questions
question
Mathematics, 26.02.2021 01:00
question
Mathematics, 26.02.2021 01:00
question
Spanish, 26.02.2021 01:00
question
Mathematics, 26.02.2021 01:00