Recursive Selection Sort

Recursive Selection Sort

Selection sort was introduced in Sorting Arrays. Recall that it finds the smallest element in the list and swaps it with the first element. It then finds the smallest element remaining and swaps it with the first element in the remaining list, and so on until the remaining list contains only a single element. The problem can be divided into two subproblems:

  • Find the smallest element in the list and swap it with the first element.
  • Ignore the first element and sort the remaining smaller list recursively.

The base case is that the list contains only one element. The code below gives the recursive sort method.

Image description

Two overloaded sort methods are defined. The first method, sort(double[] list), sorts an array in list[0..list.length – 1] and the second method, sort(double[] list, int low, int high), sorts an array in list[low..high]. The second method can be invoked recursively to sort an ever-shrinking subarray.


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *