Professional Documents
Culture Documents
1. Consider the following code below and the list = [1, 4, 5, 7]
for n in range(len(list)):
if (list[n] % 5 == 0 ):
print(list[n])
a) 5
b) 4
c) All elements
d) None of mentioned.
2. After executing the following code, the output will be: def selectionSort(nlist):
for i in range(len(nlist)):
a) 14 minPosition = i
b) 33 for j in range(i+1, len(nlist)):
if nlist[minPosition] > nlist[j]:
c) All elements of the list
minPosition = j
d) None of mentioned. temp = nlist[i]
nlist[i] = nlist[minPosition]
nlist[minPosition] = temp
1. For the list = [1, 15, 4, 15, 3, 16, 17, 13, 11].
def bubbleSort(alist):
for passnum in range(len(alist)-1, 0 ,-1):
for i in range(passnum):
if alist[i]>alist[i+1]:
print(alist)
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
2. For the list = [1, 15, 4, 15, 3, 16, 17, 13, 11].
- Write the list order for 3 iterations by using Selection sort.
def selectionSort(nlist):
for i in range(len(nlist)):
minPosition = i
for j in range(i+1, len(nlist)):
if nlist[minPosition] > nlist[j]:
minPosition = j
temp = nlist[i]
nlist[i] = nlist[minPosition]
nlist[minPosition] = temp
- Write 3 midpoint values (indices-if any) by using binary search for the list below.
(target = 3). (Show all work).
11 3 4 1 13 15 9 16 17
3. For the list = [1, 15, 4, 15, 3, 16, 17, 13, 11].
- Write the list order for 3 iterations by using Insertion sort.
def insertionSort(nlist):
for index in range(1,len(nlist)):
currentvalue = nlist[index]
position = index
while position>0 and nlist[position-1]> currentvalue:
nlist[position]=nlist[position-1]
position = position-1
nlist[position]=currentvalue