Decisions Exam

Only available on StudyMode
  • Topic: Sorting algorithm, Shell sort, Insertion sort
  • Pages : 8 (1238 words )
  • Download(s) : 103
  • Published : February 16, 2013
Open Document
Text Preview
thAlgorithms

Chapter assessment

1. Use the following pseudo code with your own choice of values of X.

10INPUT X
20LET Y = X/3
30LET R = Y – INT(Y)
40IF R = 0 PRINT ACCEPT, GOTO A
50PRINT REJECT
60LABEL A
70END

(i) Explain what is being achieved.[4]

(ii) If you replaced the line LET Y = X/3 by the line LET Y = X/5, how would this change the outcome of the above?[1]

2. The following algorithm is to be applied to the positive integers from 1 to 12.

Step 1:Cross out every even number.
Step 2:Change the state of every multiple of 3 (including 3) – i.e. for every multiple of 3, if it is crossed out then remove the crossing out, and if it is not crossed out then cross it out.

Step 3:Change the state of every multiple of 4 (including 4). Step 4:Change the state of every multiple of 5 (including 5).

Step 11:Change the state of every multiple of 12 (including 12).

(iii) Numbers| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12| Step 1| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12| Step 2| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12| Step 3| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12| Step 4| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12| Step 5| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12| Step 6| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12| Step 7| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12| Step 8| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12| Step 9| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12| Step 10| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12| Step 11| 1| 2| 3| 4| 5| 6| 7| 8| 9| 10| 11| 12|

Apply the algorithm, using a row of the table below for each step. In each step the crossings-out should be the same as in the previous step except for those numbers whose state is changed.[4]

(iv) Say what the algorithm achieves.[1]
3. In a test Ali scores 57, Bill scores 67, Cleo scores 43 and Debbie scores 73.

(v) Use the bubble sort to rearrange the individuals from alphabetic order into descending order of merit. Show the list of scores after each pass of the algorithm.[3]

Ewan’s score of 60 subsequently becomes available. He is inserted into the list by comparing his score with the best score, then with the second best, etc., until it can be put in the correct place.

(vi) How many comparisons were made in performing the bubble sort and then inserting Ewan into the list?[1]

(vii) How many comparisons would have been made if Ewan’s score had been available in the first place, and he had been listed after Debbie initially?[1]

4. The following algorithm is intended to carry out a process on a sequence of numbers.

Step 1:Compare the first and second numbers.
If the first number is larger then swap these two numbers. Step 2:Compare the second and third numbers.
If the second number is larger then swap these two numbers. Step 3:Continue in this way until you reach the end of the sequence of numbers. That is, until you have completed a pass of all the numbers in the sequence.

Step 4:If there were no swaps during the last pass then stop. Step 5:Go back to step 1.

(viii) By trying out the algorithm on the sequence {5, 3, 8, 0, 9, 6, 8, 4}, explain what the process is achieving.[4]

(ix) How many comparisons did you need to make to sort the above list? [1]

(x) How many swaps did you need to make? [1]

(xi) Apply the Quicksort Algorithm to the original set of numbers.[3]

(xii) How many comparisons did you make in (iv)?[1]

(xiii) Apply the Shell Sort algorithm to the original set of numbers. [4]

(xiv) How many comparisons did you make in (vi)?[1]

5. The following algorithm is to be applied to two numbers.

10INPUT X...
tracking img