Computers and Technology
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)

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 08:00, razielcornils04
What is the algorithm for building a binary tree program
Answers: 2
image
Computers and Technology, 23.06.2019 22:30, reaganphelps3
What would be the address of the cell, which is at the intersection of the second row and the third column in a worksheet?
Answers: 1
image
Computers and Technology, 24.06.2019 02:30, journeyhile5
How to apply the fly in effect to objects on a slide
Answers: 1
image
Computers and Technology, 25.06.2019 04:00, edjiejwi
Plz there is a problem with my account. i no longer have ! ( hand, ambitious, virtuoso etc.) it's like getting brainliest means nothing. how do i get ranks back?
Answers: 1
Do you know the correct answer?
Given code that reads user IDs (until -1), complete the quicksort() and partition() functions to sor...

Questions in other subjects:

Konu
Chemistry, 28.02.2020 21:15