Next: About this document ...
Up: Assignment 10: Arrays
Previous: Recursion
One not-very-efficient way to sort the elements of an array
is to find the largest element and swap it with the first
element, then find the second-largest element and swap it with
the second, and so on.
- 1.
- Write a method called indexOfMaxInRange that finds the
largest element in the given range, and returns its index.
You can modify your recursive version of maxInRange or
you can write an iterative version from scratch.
- 2.
- Write a method called swapElement that takes an
array of integers and two indices, and that swaps the elements
at the given indices.
- 3.
- Write a method called sortArray that takes an array of
integers and that uses indexOfMaxInRange and swapElement
to sort the array from largest to smallest.
Allen Downey
1999-11-09