Professional Documents
Culture Documents
CIS2203 Solution Practice
CIS2203 Solution Practice
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.
a) 14 def selectionSort(nlist):
b) 33 for i in range(len(nlist)):
c) All elements of the list minPosition = i
d) None of mentioned. 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
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
1 15 4 15 3 16 17 13 11
1 4 15 15 3 16 17 13 11
1 4 15 3 15 16 17 13 11
1 4 15 3 15 16 13 17 11
1 4 15 3 15 16 13 11 17
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
1 15 4 15 3 16 17 13 11
1 3 4 15 15 16 17 13 11
1 3 4 11 15 16 17 13 15
1 3 4 11 13 16 17 15 15
1 3 4 11 13 15 17 16 15
- Write 3 midpoint values (indices-if any) by using binary search for the list below. (target
= 3). (Show all work).
0 1 2 3 4 5 6 7 8
11 3 4 1 13 15 9 16 17
1 3 4 9 11 13 15 16 17
Target = 3
M1 = (0+8)/2 = 4 = 11
M2 = (0+3)/2 = 1 = 3
Midpoint is = Found
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
1 15 4 15 3 16 17 13 11
1 4 15 15 3 16 17 13 11
1 3 4 15 15 16 17 13 11
1 3 4 13 15 15 16 17 11
1 3 4 11 13 15 15 16 17