0

Rust – Selection Sort using Recursion and Iteration

This post is about a sorting algorithm called Selection Sort with recursive and iterative implementations in Rust.

Recursive Selection Sort

There are two functions for this implementation; although it is not a requirement. The first function is non-recursive and is just used to “bootstrap” the recursive function.

The other function is the recursive Selection Sort function.

We have another function called swap  that swaps the value of two items.

Sample usage would be:

This outputs

Iterative Selection Sort

For this implementation, we have a single function that accepts a mutable reference to an array of i32 ‘s with any length and returns nothing.

Sample usage would be:

This outputs

Tested with Rust 1.38.0.

Karl San Gabriel

Karl San Gabriel

Professional Software Developer