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?

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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
browneliza
jamessusan
jonessteve
smithamy
smythjune
9
browneliza
jamessusan
jonessteve
smithamy
smythjune
Transcribed Image Text:7 browneliza jamessusan jonessteve smithamy smythjune 9 browneliza jamessusan jonessteve smithamy smythjune
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
Transcribed Image 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
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY