Reference no: EM132233489
Exercises -
5-12. Create a program that sorts an array of integers in ascending order by using a bubble sort. If you are not familiar with the bubble sort algorithm you should be able to find an explanation in just about any introductory programming text. Assume that the array is defined to be six elements as follows:
ARRAY:
.WORD 100
.WORD 74
.WORD -144
.WORD 10022
.WORD 657
.WORD -33
The bubble sort should modify the array contents, not put the result into a second array.
5-13. An array X is considered to be greater than an array Y if for every element i, X[i] > Y[i] (the arrays are assumed to be the same length). Develop a program that sets the value of ACC = 1 if X > Y and ACC = 0, otherwise. Assume for test purposes that the arrays have six elements and are defined as follows:
X: .WORD 95 Y: .WORD 35
.WORD 1002 .WORD 102
.WORD 233 .WORD 109
.WORD -137 .WORD -1378
.WORD 6332 .WORD 6332
.WORD -33191 .WORD -33194
Try your program with a variety of other test arrays.
5-15. Develop a program that reads in an integer value from the user and then uses a binary search on a sorted array to determine if the value is stored in the array. If you are not familiar with the binary search algorithm you should be able to find an explanation in most introductory programming texts. If the value is found, the program should print to the screen "Data is found," otherwise it should print "Data not found." Include a conversational mechanism so that multiple queries can be tried. For example, if the sorted array is defined as follows:
SORTED:
.WORD -345
.WORD -98
.WORD 109
.WORD 1555
.WORD 6332
.WORD 33194
then the following is an appropriate sample output:
Enter Data: 100
Data not found
Want to continue? (1=yes,0=no): 1
Enter data: 109
Data is found
Want to continue? (1=yes,0=no): 0
Problems -
Problem 1 - Exercise 5-13 in your textbook. Your program should load into memory at address $000. Put your solution in a single text file named hw3p1.csp. Put the object code in a file named hw3p1.obj.
Problem 2 - A variant of exercise 5-12 in your textbook, with these changes: use selection sort instead of bubble sort. Do not assume that the array has only six elements. Assume that the number of elements in the array will be found at memory address $100. Assume that ARRAY begins at $101. The sorting should be done in ascending order, so that when the sorting is done, your sorted array should have the smallest value at address $101. We discussed selection sort in class last week. Your program should load into memory at address $000. Put your solution in a single text file named hw3p2.csp. Put the object code in a file named hw3p2.obj.
Problem 3 - Another variant of exercise 5-12 in your textbook, with these changes: use insertion sort instead of bubble sort. Do not assume that the array has only six elements. Assume that the number of elements in the array will be found at memory address $100. Assume that ARRAY begins at $101. The sorting should be done in ascending order, so that when the sorting is done, your sorted array should have the smallest value at address $101. Your program should load into memory at address $000. Put your solution in a single text file named hw3p3.csp. Put the object code in a file named hw3p3.obj. Here's insertion sort implemented in Python (because somebody asked for Python), borrowed from the ECS 32B textbook:
def insertionSort(alist):
for index in range(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0 and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue
alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)
Problem 4 - A variant of exercise 5-15 in your textbook, with these changes: Do not read an integer value from the user. Instead, find the integer value at memory address $100. Assume that the number of elements in the sorted array will be found at memory address $101. Assume that SORTED begins at $102. If the target integer is found, set the value of ACC to 1. If the target integer is not found, set the value of ACC to 0. Do not include a conversational mechanism to permit multiple queries. Do not print anything to the screen. We discussed binary search in class last week. Your program should load into memory at address $000. Put your solution in a single text file named hw3p4.csp. Put the object code in a file named hw3p4.obj.