a) Consider a list of integers: [1, 3, 7, 4, 2, 5] Provides steps of how this list is sorted by the following algorithms

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

a) Consider a list of integers: [1, 3, 7, 4, 2, 5] Provides steps of how this list is sorted by the following algorithms

Post by answerhappygod »

a) Consider a list of integers: [1, 3, 7, 4, 2, 5]
Provides steps of how this list is sorted by the following
algorithms respectively:
def bubbleSort(alist):
N = len(alist)
for p in range(N-1):
for i in
range(N-p-1):
if
alist > alist[i+1]:
alist,
alist[i+1] = alist[i+1], alist
def selectionSort(alist):
for fillslot in range(len(alist)-1):
posOfMin =
fillslot
for i in
range(fillslot+1,len(alist)):
if
alist < alist[posOfMin]:
posOfMin
= i
alist[fillslot],
alist[posOfMin] =
alist[posOfMin],
alist[fillslot]
def insertionSort(alist):
for i in range(1,len(alist)):
value =
alist
cur = i
while cur > 0
and alist[cur-1] > value:
cur
-= 1
if cur != i:
alist.pop(i)
alist.insert(cur,value)
You should only show steps wherever there is a change in the
internal order of the list. For example, given a different list [1,
3, 4, 5, 2], the content of the list after the first change in
order are as follows, respectively:
bubbleSort([1, 3, 4, 5, 2])
step 1: [1, 3, 4, 2, 5]
selectionSort([1, 3, 4, 5, 2])
step 1: [1, 2, 4, 5, 3]
insertionSort([1, 3, 4, 5, 2])
step 1: [1, 2, 3, 4, 5]
Now provide the steps to sort [1, 3, 7, 4, 2, 5] with
the three algorithms.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply