## Exercises: Sorting

Apr 162018Before attempting these exercises, you should read the posts on comparisons and swaps, iterative sorting and recursive sorting.

### Exercises

- Write a function which, given a list and two indices, swaps the values at those indices.
- Modify your function so that it only swaps if the first value is greater than the second value.

- Consider the interactive model for the “out-of-place” selection sort algorithm.
- Work through an example step-by-step — at each step, write down the value being sorted, and the current state of both lists.
- State which list operations are needed to implement this algorithm.
- Write a function which finds the index of the minimum value in the input list. (Hint: use plans.)
- Implement the algorithm using a loop which sorts values into the output list, one at a time.

- Consider the interactive model for the “out-of-place” insertion sort algorithm.
- Work through an example step-by-step — at each step, write down the value being sorted, and the current state of both lists.
- State which list operations are needed to implement this algorithm.
- Write a function which finds the index of the first value in the output list greater than a given target. (Hint: use plans.)
- Implement the algorithm using a loop which sorts values into the output list, one at a time.

- Add the following print statements to your algorithms from 2. and 3.:
`print('Input list:', repr(input_list))`

`print('Output list:', repr(output_list))`

`print('Sorting the value', repr(value))`

## There are no published comments.