December
1999 QUESTION 4 Total Marks: 15 Marks |
Click here to access other
questions
SUGGESTED SOLUTIONS |
(a) |
Briefly describe the selection Sort algorithm. Selection sort: •Moves the first item in the list that has not been sorted to the first unsorted position;(1 mark) •By swapping the two values around;(1 mark)
|
[2] |
(b) |
When a list of size N is sorted by selection sort, how many passes of the list are required to ensure the list is sorted? N -1 passes of the list are required.
|
[1] |
(c) |
Given the unsorted list x below,show the state of the list after each pass of a descending selection sort. x =[2 ,6 ,13 ,7 ,12 ] •x =[2 ,6 ,13 ,7 ,12 ] •x =[13 ,6 ,2 ,7 ,12 ](1 mark) •x =[13 ,12 ,2 ,7 ,6 ](1 mark) •x =[13 ,12 ,7 ,2 ,6 ](1 mark) •x =[13 ,12 ,7 ,6 ,2 ](1 mark)
|
[4] |
(d) |
Assume the following definition of a dynamic linked list:
Write a procedure DescendingSelectionSort , the signature of which is given below, which takes an unsorted list of the type given above and performs a descending selection sort on the list.You may assume the existence of the procedures and functions whose signatures are given above. PROCEDURE DescendingSelectionSort(VAR x :List); A sample definition of DescendingSelectionSort follows: PROCEDURE DescendingSelectionSort(VAR
x : List);
Declaring an outer loop,bounded from 0 to Length(x)-1 ;(1 mark) Declaring a greatest counter,and initialising it with ValAt(a);(1 mark) Declaring the iterators b and indicator ,and initialising them to a on each iteration of the outer loop;(1 mark) Declaring an inner loop,bounded from i+1 to Length(x);(1 mark) Incrementing the b iterator on each iteration of the inner loop,using MoveIteratorAlong ;(1 mark) Comparing the current value and the greatest value,and updating indicator and greatest if the current value is greater;(1 mark) •Swapping a and the indicator iterator before the inner loop completes an iteration.(1 mark) |
[8] |