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, 23.06.2019 04:31, hargunk329
Q13 what function does a security certificate perform? a. creates user accounts b. scrambles data c. identifies users d. creates password policies e. provides file access
Answers: 1
image
Computers and Technology, 23.06.2019 12:40, Emilyvite6251
According to the video what are some tasks petroleum engineers perform check all that apply
Answers: 2
image
Computers and Technology, 23.06.2019 22:40, azariah7
22. sata3 allows for data transfer rates of 600 mb/s. explain why you would likely not be able to copy data from one hard drive to another at anywhere close to this speed. also, what could be upgraded on the computer to achieve transfer speeds closer to 600 mb/s
Answers: 1
image
Computers and Technology, 24.06.2019 09:50, trenrain
Create a string list. 2. use console. readline() to collect values of firstname, lastname, street, city, state, zip, save them to list. 3. write a simple linq statement, call method uppercasewords() to change first letter to uppercase. 4. create a foreach statment to display the information. public static string uppercasewords(string value) { char[] array = value. tochararray(); if (array. length > = 1) { if (char. islower(array[0])) { array[0] = char. toupper(array[0]); } } for (int i = 1; i < array. length; i++) { if (array[i - 1] == ' ') { if (char. islower(array[i])) { array[i] = char. toupper(array[i]); } } } return new string(array);
Answers: 3
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
Advanced Placement (AP), 09.12.2020 08:20
Konu
Physics, 09.12.2020 08:20
Konu
Mathematics, 09.12.2020 08:20