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 o…


This content originally appeared on DEV Community and was authored by Paul Ngugi

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.


This content originally appeared on DEV Community and was authored by Paul Ngugi


Print Share Comment Cite Upload Translate Updates
APA

Paul Ngugi | Sciencx (2024-07-02T19:37:20+00:00) Recursive Selection Sort. Retrieved from https://www.scien.cx/2024/07/02/recursive-selection-sort/

MLA
" » Recursive Selection Sort." Paul Ngugi | Sciencx - Tuesday July 2, 2024, https://www.scien.cx/2024/07/02/recursive-selection-sort/
HARVARD
Paul Ngugi | Sciencx Tuesday July 2, 2024 » Recursive Selection Sort., viewed ,<https://www.scien.cx/2024/07/02/recursive-selection-sort/>
VANCOUVER
Paul Ngugi | Sciencx - » Recursive Selection Sort. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/02/recursive-selection-sort/
CHICAGO
" » Recursive Selection Sort." Paul Ngugi | Sciencx - Accessed . https://www.scien.cx/2024/07/02/recursive-selection-sort/
IEEE
" » Recursive Selection Sort." Paul Ngugi | Sciencx [Online]. Available: https://www.scien.cx/2024/07/02/recursive-selection-sort/. [Accessed: ]
rf:citation
» Recursive Selection Sort | Paul Ngugi | Sciencx | https://www.scien.cx/2024/07/02/recursive-selection-sort/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.