Chapter 9- Algorithm Workbench

NOTE: There are no TABS on the codes. THESE ARE MY PERSONAL solutions to the problem. Copy at your own risk.

1. A swap module that accepts two arguments of the Real data type and swaps them.

Module swap(Real Ref a, Real Ref b)
//Local variable for temporary storage
Declare Real temp
//Swap the values in a and b
Set temp = a
Set a = b
Set b = temp
End Module

2. What algorithm does the following pseudocode perform?

Declare Integer maxElement
Declare Integer index

For maxElement = arraySize – 1 To 0 Step -1
For index = 0 To maxElement – 1
If array[index] > array[index + 1] Then
Call swap(array[index], array[index + 1])
End If
End For
End For
ANSWER: The pseudocode performs Bubble Sort algorithm.

3. What algorithm does the following pseudocode perform?

Declare Integer index
Declare Integer scan
Declare Integer unsortedValue

For index = 1 To arraySize – 1
Set unsortedValue = array[index]
Set scan = index

While scan > 0 AND array[scan-1] < array[scan] Call swap(array[scan-1], array[scan]) Set scan = scan - 1 End While Set array[scan] = unsortedValue End For ANSWER: The pseudocode performs Insertion Sort algorithm. 4. What algorithm does the following pseudocode perform? Declare Integer startScan Declare Integer minIndex Declare Integer minValue Declare Integer index For startScan = 0 To arraySize - 2 Set minIndex = startScan Set minValue = array[startScan] For index = startScan + 1 To arraySize - 1 If array[index] < minValue Set minValue = array[index] Set minIndex = index End If End For Call swap(array[minIndex], array[startScan]) End For ANSWER: The pseudocode performs Selection Sort algorithm.

Leave a Reply

Your email address will not be published. Required fields are marked *