CLO2 Exercises Solution
CLO2 Exercises Solution
def selectionSort(nlist):
for indx in range(0, len(nlist):
min = nlist[indx]
for L in range(indx+1,len(nlist)):
if nlist[L]<min:
min = nlist[L]
Location = L
temp = nlist[indx]
nlist[indx] = min
nlist[Location] = temp
nlist = [29, 72, 98, 13, 87, 66, 52, 51, 36]
selectionSort(nlist)
print(nlist)
def max(arr1):
max=arr1[0]
for i in range(len(arr1)):
if arr1[i]>max:
max=arr1[i]
return max
arr1=[2,8,9,0,-2]
print("the maximum value is", max(arr1))
def min(arr1):
min=arr1[0]
for i in range(len(arr1)):
if arr1[i]<min:
min=arr1[i]
return min
arr1=[2,8,9,0,-2]
print("the minimum value is", min(arr1))
CIS2203 Discrete Mathematics
c) Finding the sum of values in a list of numbers
def sum(lst1):
sum1=0
for i in range(len(lst1)):
sum1=lst1[i]+sum1
return sum1
lst1=[1,2,3,4,-2,0,3]
print(" the sum is",sum(lst1))
def count(list5):
count=0
for i in range (len(list5)):
if list5[i]>60:
count=count+1
return count
list5=[70,80,40,60,30,90]
print(" the count is",count(list5))
CIS2203 Discrete Mathematics
e) Linear Search
index = 0
return index
index=index+1
return -1
f) Binary search
g) Selection sort
def selectionSort(nlist):
min = nlist[indx]
for L in range(indx+1,len(nlist)):
if nlist[L]<min:
min = nlist[L]
Location = L
temp = nlist[indx]
nlist[indx] = min
nlist[Location] = temp
nlist = [29, 72, 98, 13, 87, 66, 52, 51, 36]
selectionSort(nlist)
print(nlist)
h) 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
nlist = [14,46,43,27,57,41,45,21,70]
insertionSort(nlist)
print(nlist)
CIS2203 Discrete Mathematics
7. How many loop iterations(passes) needed to find number 9 in the list [1 2 3 4 5 7 8 9 10]
a. Using linear search 8
b. Using binary search 3
8. The best case scenario in sequential search happens when the key search element is…
first element in the list.
9. The worst case scenario in linear search happens when……It is the last element or when
it is not in the list.
10. Which one is better to use linear or binary search? Justify your answer? Binary since it
has less complexity [logn<n]
11. What will be returned by the linear search algorithm if the
list=[ “Salem”, “Hassan”, “Abdullah”, “Saif”] and:
a. The key is Hassan answer: 1
b. The key is Ahmad answer:-1
12. What is the difference between selection and insertion sort? See slides
13. Write an algorithm that will display the maximum number in a list?
14. Write a Python code to find the maximum number in a list of numbers
def max(arr1):
max=arr1[0]
for i in range(len(arr1)):
if arr1[i]>max:
max=arr1[i]
return max
arr1=[2,8,9,0,-2]
CIS2203 Discrete Mathematics
If the item=14 and the alist= [2, 3, 5, 8, 9, 11, 13, 14, 18] answer the following
questions:
1) What will be the value of the variables (first, last, midpoint, index) after each
iteration (pass)
2) What will be the remaining numbers in the list after each iteration (pass)
3) How many iterations will be required to find the key
4) What will be returned by this algorithm (the output)
Solution:
1)
3) 3 iterations
4) 7