0

Selection Sort Recursion and Iteration in Rust

Last Updated on

This post is about a sorting algorithm called Selection Sort in Rust using recursion and iteration.

Selection Sort Using Recursion

The Selection Sort codes that use recursion have 3 functions. The first function is non-recursive and simply starts the recursive function.

The second function is the actual function for the Selection Sort that uses recursion. It runs the function swap.

The swap function is as follows.

Below is an example of codes that use out bootstrap function.

The codes output the following.

Selection Sort Using Iteration

The Selection Sort codes that use iteration have a single function that accepts a mutable reference to an array of i32‘s with any length and returns nothing.

Below are example codes to run the iterative function.

The codes output the following.

Tested with Rust 1.38.0.

Other Algorithms in Rust

For more algorithms in Rust, please go to Algorithms In Codes page.

Karl San Gabriel

Karl San Gabriel

Professional Software Developer