a) Consider a list of integers: [1, 3, 7, 4, 2, 5] Provides steps of how this list is sorted by the following algorithms
Posted: Mon May 09, 2022 5:54 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 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.
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.