Question
How to modify the following algorithms to display the total number of swap operations. The following algorithms are given: def Algorithm_1(lyst): i = 0 while
How to modify the following algorithms to display the total number of swap operations. The following algorithms are given:
def Algorithm_1(lyst): i = 0 while i < len(lyst) - 1: minIndex = i j = i + 1 while j < len(lyst): if lyst[minIndex] > lyst[j]: minIndex = j j += 1 if minIndex != i: lyst[minIndex], lyst[i] = lyst[i], lyst[minIndex] i += 1 return(lyst)
def Algorithm_2(lyst): n = len(lyst) while n > 1: i = 1 while i < n: if lyst[i] < lyst[i - 1]: lyst[i], lyst[i - 1] = lyst[i-1], lyst[i] count += 1 i += 1 n -= 1 return(lyst) # return(lyst,count)
if the user called Algorithm_1 with a list of [5, 6, 8, 3, 2, 1, 9, 4] the number of swaps would be 6, however the output would be 16 when calling Algorithm_2 with the same list.
I run the following code:
def Algorithm_1(lyst):
i = 0
swap_count=0
while i < len(lyst) - 1:
minIndex = i
j = i + 1
while j < len(lyst):
if lyst[minIndex] > lyst[j]:
minIndex = j
j += 1
if minIndex != i:
lyst[minIndex], lyst[i] = lyst[i], lyst[minIndex]
swap_count=swap_count+1
i += 1
return(lyst, swap_count)
def Algorithm_2(lyst):
n = len(lyst)
count=0
while n > 1:
i = 1
while i < n:
if lyst[i] < lyst[i - 1]:
lyst[i], lyst[i - 1] = lyst[i-1], lyst[i]
count += 1
i += 1
n -= 1
return(lyst, count) # return(lyst,count)
#calling the both Algorithms
algorithm_01=Algorithm_1([5, 6, 8, 3, 2, 1, 9, 4])
list_1=[5, 6, 8, 3, 2, 1, 9, 4]
list_1,swaps_1 = algorithm_01(list_1)
print("The number of swaps for Algorithm_1 is", swaps_1)
list_2=[5, 6, 8, 3, 2, 1, 9, 4]
list_2,swaps_2 = Algorithm_2(list_2)
print("The number of swaps for Algorithm_2 is", swaps_2)
I am getting the following error:
TypeError
Traceback (most recent call last) Input In [59], in
algorithm_01(list_1) 33 print("The number of swaps for Algorithm_1 is", swaps_1) 34 list_2=[5, 6, 8, 3, 2, 1, 9, 4]
TypeError: 'tuple' object is not callable
Step by Step Solution
There are 3 Steps involved in it
Step: 1
The error youre encountering is because youre trying to call a tuple object as if it were a function ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started