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...


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?




7<br>browneliza<br>jamessusan<br>jonessteve<br>smithamy<br>smythjune<br>9<br>browneliza<br>jamessusan<br>jonessteve<br>smithamy<br>smythjune<br>

Extracted text: 7 browneliza jamessusan jonessteve smithamy smythjune 9 browneliza jamessusan jonessteve smithamy smythjune
1 # Global variable<br>2 num_calls = 0<br>3 # TODO: Write the partitioning algorithm - pick the middle element as the<br>pivot, compare the values using two index variables 1 and h (low and high),<br>initialized to the left and right sides of the current elements being sorted,<br>and determine if a swap is necessary<br>4<br>5 #<br>6 #<br>7 def partition (user_ids, i, k):<br>low = (i-1)<br>mid = (i + k) //2<br>pivot = user_ids[mid]<br>user_ids[mid], user_ids[k] = user_ids[k], user_ids[mid]<br>for j in range (i ,k):<br>if user_ids[j] <= pivot:<br>8<br>9<br>10<br>11<br>12<br>13<br>14<br>low = low + 1<br>15<br>user_ids[low],user_ids[j] = user_ids[j],user_ids[low]<br>user_ids[low+1],user_ids[k] = user_ids[k],user_ids[low+1]<br>return ( loW+1 )<br>16<br>17<br>18 # TODO: Write the quicksort algorithm that recursively sorts the low and<br>high partitions. Add 1 to num_calls each time quisksort() is called<br>19 #<br>20 def quicksort(user_ids, i, k):<br>global num_calls<br>num_calls = num_calls + 1<br>if ikk:<br>pivot_index = partition(user_ids,i,k)<br>quicksort (user_ids, i, pivot_index-1)<br>quicksort (user_ids, pivot_index+1, k)<br>21<br>22<br>23<br>24<br>25<br>26<br>27 if<br>main

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="">

Jun 11, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here