A2 CS Task 6 24-9-2024
A2 CS Task 6 24-9-2024
Page 2 of 13
Page 3 of 13
Page 4 of 13
Page 5 of 13
Page 6 of 13
10 5 6 7 1 12 13 15 21 8
Page 7 of 13
(c) Make necessary changes to the main program to output the unsorted DataList, as well as the sorted
DataList. Also, output
10 5 6 7 1 12 13 15 21 8 messages to the user to
indicate the outputs
before and after sorting.
print("Before")
PrintArray(DataList)
TheData = InsertionSort(DataList)
print("After")
PrintArray(TheData)
(d) Execute your program and take a screenshot of program output along with your program code.
(b) (i) The function InsertionSort() takes input an array as parameter, and returns the sorted array by
applying the Insertion Sort algorithm. Write program code for InsertionSort ().
b) (ii) Write a procedure ArrayOut() to output all the contents of the array DataList in a single line to the
screen. The procedure should use iteration. Pass the array into the procedure as a parameter.
(c) Make necessary changes to the main program to output the unsorted DataList, as well as the sorted
DataList. Also, output messages to the user to indicate the outputs before and after sorting.
Page 8 of 13
Save your program.
Copy and paste the program code into part 1(c) in the evidence document.
(d) Execute your program and take a screenshot of program output along with your program code.
Copy and paste the program code, along with the screenshot of program output
into part 1(d) in the evidence document.
Page 9 of 13
Q1) A program stores the following ten integers in a 1D array with the identifier arrayData:
21 5 7 13 3 5 19 30 21 0
arrayData = []*10
arrayData = [21, 5, 7, 13, 3, 5, 19, 30, 21, 8]
(b) (i) Write program code for the function bubbleSort(), which uses the Bubble Sort algorithm to sort
arrayData. The function should take an array as parameter and return the sorted array to the main
program. [4]
def BubbleSort(Array):
for X in range (0, len(Array)-1):
for y in range (0, (len(Array)-X-1)):
if Array[y]> Array[y+1]:
temp = Array[y]
Array[y]= Array[y+1]
Array[y+1]= temp
return Array
(b) (ii) Write program code for the function binarySearch(), which takes a sorted array and an integer as
parameters and performs a binary search on the members of the array to find the parameter value. It
returns True if it was found and False if it was not found. [6]
def binarySearch(Array,n):
global pos
l = 0
u = len(Array)-1
while l <= u:
mid = (l+u) // 2
if Array[mid] == n:
pos = mid
return True
else:
if Array[mid] < n:
l = mid+1
else:
u = mid-1
return False
Page 10 of 13
c) Modify your program code to appropriately prompt the user to input the binarySearch parameter.
Make changes to your code, wherever necessary, so that if the binarySearch was successful It outputs
the index of the parameter along with an appropriate message. If not, then it should also output an error
message if the parameter is not found. [2]
def binarySearch(Array,n):
global pos
l = 0
u = len(Array)-1
while l <= u:
mid = (l+u) // 2
if Array[mid] == n:
pos = mid
return True
else:
if Array[mid] < n:
l = mid+1
else:
u = mid-1
return False
def BubbleSort(Array):
for X in range (0, len(Array)-1):
for y in range (0, (len(Array)-X-1)):
if Array[y]> Array[y+1]:
temp = Array[y]
Array[y]= Array[y+1]
Array[y+1]= temp
return Array
arrayData = []*10
arrayData = [21, 5, 7, 13, 3, 5, 19, 30, 21, 8]
pos = -1
list = BubbleSort(arrayData)
searchValue = int(input("Enter search value: "))
returnvalue = binarySearch(list, searchValue)
if returnvalue:
print("Value found at ",pos)
else:
print("Not Found")
(d) Test your program with one value that is in arrayData and one value that is not in arrayData. Attach
screenshot of program output along with your program code. [2]
Page 11 of 13
Q1) A program stores the following ten integers in a 1D array with the identifier arrayData:
21 5 7 13 3 5 19 30 21 0
(b) (i) Write program code for the function bubbleSort(), which uses the Bubble Sort algorithm to sort
arrayData. The function should take an array as parameter and return the sorted array to the main
program. [4]
(b) (ii) Write program code for the function binarySearch(), which takes a sorted array and an integer as
parameters and performs a binary search on the members of the array to find the parameter value. It
returns True if it was found and False if it was not found. [6]
c) Modify your program code to appropriately prompt the user to input the binarySearch parameter.
Make changes to your code, wherever necessary, so that if the binarySearch was successful It outputs
the index of the parameter along with an appropriate message. If not, then it should also output an error
message if the parameter is not found. [2]
(d) Test your program with one value that is in arrayData and one value that is not in arrayData. Attach
screenshot of program output along with your program code. [2]
Page 12 of 13
Copy and paste the program code, along with the screenshot of program output
into part 1(d) in the evidence document.
Page 13 of 13