Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello i need a programming problem solved from my book in Chapter 9 programming

ID: 3567561 • Letter: H

Question

Hello i need a programming problem solved from my book in Chapter 9 programming exercise 8.

The problem reads as follows: "modify the modules presented in this chapter that perform the bubble sort, selection sort, and insertion sort algorithms on an integer array, such that each module keeps a count of a number of swaps it makes. Then design an application that uses three identical arrays of at least 20 integers. It should call each module on a different array, and display the number of swaps made by each algorithm."

I need this code in python.

1500 points to the best answer!

Thank you!

Explanation / Answer

from random import randint

def swap(arr, l, r):
temp = arr[l]
arr[l] = arr[r]
arr[r] = temp

def insertionsort(arr):
count = 0
size = len(arr)
for i in range(1, size):
for j in range(i, 0, -1):
if(arr[j] < arr[j - 1]):
count += 1
swap(arr, j, j - 1)
else:
break
return count

def selectionsort(arr):
count = 0
size = len(arr)
for i in range(size - 1):
ind = i
for j in range(i, size):
if(arr[ind] > arr[j]):
ind = j
if(ind != i):
swap(arr, ind, i)
count += 1
return count

def bubblesort(arr):
count = 0
size = len(arr)
for i in range(size - 1):
for j in range(0, size - 1 - i):
if(arr[j] > arr[j + 1]):
swap(arr, j, j + 1)
count += 1
return count

def Main():
size = int(input("Enter Size of the Array = "))
arr1, arr2, arr3 = [], [], []
for i in range(size):
val = randint(1, 100)
arr1.append(val)
arr2.append(val)
arr3.append(val)
print("Initial Array is: ")
print(arr1)
  
print(" ")
print(" ")
count1 = insertionsort(arr1)
count2 = selectionsort(arr2)
count3 = bubblesort(arr3)
print("Number of swaps by Insertion sort is: " + str(count1))
print("Number of swaps by selection sort is: " + str(count2))
print("Number of swaps by bubble sort is: " + str(count3))
print(" ")
print("Sorted Array is: ")
print(arr1)
print(" ")
if __name__ == "__main__":
Main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote