Coding and Algorithms

Ontario 09 Academic (MPM1D)

Debugging

The list of numbers `4, 9, 8, 1, 6`

is to be sorted using the selection sort algorithm.

a

Enter the output after each line of the selection sort algorithm.

Step Number | Algorithm Step | `OUTPUT` |
---|---|---|

$1$1 | Find the smallest number. Swap it with the first number. |
`$\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,` |

$2$2 | Find the second-smallest number. Swap it with the second number. |
`$\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,` |

$3$3 | Find the third-smallest number. Swap it with the third number. |
`$\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,` |

$4$4 | Find the fourth-smallest number. Swap it with the fourth number. |
`$\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,` |

b

A bug is an error in the algorithm which results in the algorithm not producing the desired output.

The algorithm below contains a bug. What is the output of each line of the algorithm?

Step Number | Algorithm Step | `OUTPUT` |
---|---|---|

$1$1 | Find the smallest number. Swap it with the first number. |
`$\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,` |

$2$2 | Find the second-smallest number. Swap it with the first number. |
`$\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,` |

$3$3 | Find the third-smallest number. Swap it with the third number. |
`$\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,` |

$4$4 | Find the fourth-smallest number. Swap it with the fourth number. |
`$\editable{},\editable{},\editable{},\editable{},\editable{}$,,,,` |

c

Which step contains the bug?

Step 1

A

Step 3

B

Step 4

C

Step 2

D

Step 1

A

Step 3

B

Step 4

C

Step 2

D

Easy

Approx 5 minutes

Sign up to try all questions