Algorithm, Rust, Software Development

Rust Selection Sort Recursion and Iteration

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

Rust Selection Sort Using Recursion

The sample Selection Sort codes in this post that use recursion have 3 functions. The first function bootstraps the recursive function.

The second function is the actual function for the Selection Sort that uses recursion. It runs the function swap function to switch values between two variables.

The swap function is as follows. It accepts a mutable array of i32, the indexes of elements whose values the codes to swap.

Below is an example of codes that use our bootstrap function in the main function in Rust.

The codes output the following.

Rust 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. Note that we are using nested loops for this kind of sorting. The outer loop iterates over an array of i32’s, while the inner loop iterates a subset of the same array for each i32.

Below are example codes to run the iterative function.

The codes output the following.

Tested with Rust 1.52.1.

Other Algorithms in Rust

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


Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.

You Might Also Like