
Jessie M.
asked 02/09/21bubble sort python
Information to solve question:
Use "a" for loop to calculate the sum of squares of values in this list: 1, 7, 8, 6, 11. (The answer should be 271)
Create a recursive function to do the same calculation a in the previous question. (The function input will be the list. Each recursion, you are going to send a sub-list with one less item from the list).
(question to answer)
(1) Create a function to perform bubble sorting (in python language) as descried in this tutorial: https://www.geeksforgeeks.org/bubble-sort/ . The algorithm should stop when there are no swaps in the last iteration
1 Expert Answer

Patrick B. answered 02/09/21
Math and computer tutor/teacher
#-------------------------------------------------
#sum of squares recursively
def SumOfSquares(x):
if len(x)>0:
return x[0]*x[0]+SumOfSquares(x[1:])
else:
return 0
#-------------------------------------
#BubbleSort
def BubbleSort(array,N):
while (int(N)>0):
for j in range(int(N-1)):
if float(array[j]) >float(array[j+1]):
temp = array[j+1]
array[j+1]=array[j]
array[j]=temp
N=int(N)-1
return(array)
x=[1,7,8,6,11]
sumOfSquares = SumOfSquares(x)
print(sumOfSquares)
array=[10,5,3,7,9,2,6,8,1,4]
print(BubbleSort(array,10))
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
source code uploaded to RESOURCES section if you want it02/09/21