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.
Ex: If the input is:
kaylasimms
julia myron1994
kaylajones
-1
the output is:
7
julia
kaylajones
kaylasimms
myron1994
I am having issues getting the count correct. What should I be looking at to adjust the count?
Extracted text: 7 browneliza jamessusan jonessteve smithamy smythjune 9 browneliza jamessusan jonessteve smithamy smythjune
Extracted text: 1 # Global variable 2 num_calls = 0 3 # TODO: Write the partitioning algorithm - pick the middle element as the pivot, compare the values using two index variables 1 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 4 5 # 6 # 7 def partition (user_ids, i, k): low = (i-1) mid = (i + k) //2 pivot = user_ids[mid] user_ids[mid], user_ids[k] = user_ids[k], user_ids[mid] for j in range (i ,k): if user_ids[j] <= pivot: 8 9 10 11 12 13 14 low = low + 1 15 user_ids[low],user_ids[j] = user_ids[j],user_ids[low] user_ids[low+1],user_ids[k] = user_ids[k],user_ids[low+1] return ( low+1 ) 16 17 18 # todo: write the quicksort algorithm that recursively sorts the low and high partitions. add 1 to num_calls each time quisksort() is called 19 # 20 def quicksort(user_ids, i, k): global num_calls num_calls = num_calls + 1 if ikk: pivot_index = partition(user_ids,i,k) quicksort (user_ids, i, pivot_index-1) quicksort (user_ids, pivot_index+1, k) 21 22 23 24 25 26 27 if main ": name 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)| 28 29 30 31 32 33 34 35 36 37 # print sorted user ids for user id in user ids: print (user_id) 38 39 40 pivot:="" 8="" 9="" 10="" 11="" 12="" 13="" 14="" low="low" +="" 1="" 15="" user_ids[low],user_ids[j]="user_ids[j],user_ids[low]" user_ids[low+1],user_ids[k]="user_ids[k],user_ids[low+1]" return="" (="" low+1="" )="" 16="" 17="" 18="" #="" todo:="" write="" the="" quicksort="" algorithm="" that="" recursively="" sorts="" the="" low="" and="" high="" partitions.="" add="" 1="" to="" num_calls="" each="" time="" quisksort()="" is="" called="" 19="" #="" 20="" def="" quicksort(user_ids,="" i,="" k):="" global="" num_calls="" num_calls="num_calls" +="" 1="" if="" ikk:="" pivot_index="partition(user_ids,i,k)" quicksort="" (user_ids,="" i,="" pivot_index-1)="" quicksort="" (user_ids,="" pivot_index+1,="" k)="" 21="" 22="" 23="" 24="" 25="" 26="" 27="" if="" main="" ":="" name="" 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)|="" 28="" 29="" 30="" 31="" 32="" 33="" 34="" 35="" 36="" 37="" #="" print="" sorted="" user="" ids="" for="" user="" id="" in="" user="" ids:="" print="" (user_id)="" 38="" 39="">= pivot: 8 9 10 11 12 13 14 low = low + 1 15 user_ids[low],user_ids[j] = user_ids[j],user_ids[low] user_ids[low+1],user_ids[k] = user_ids[k],user_ids[low+1] return ( low+1 ) 16 17 18 # todo: write the quicksort algorithm that recursively sorts the low and high partitions. add 1 to num_calls each time quisksort() is called 19 # 20 def quicksort(user_ids, i, k): global num_calls num_calls = num_calls + 1 if ikk: pivot_index = partition(user_ids,i,k) quicksort (user_ids, i, pivot_index-1) quicksort (user_ids, pivot_index+1, k) 21 22 23 24 25 26 27 if main ": name 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)| 28 29 30 31 32 33 34 35 36 37 # print sorted user ids for user id in user ids: print (user_id) 38 39 40>