Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am just trying to use Rust so I will be thankful for all remarks and corrections:

fn selection_sort(array: &mut [i32]) {

    let mut min;

    for i in 0..array.len() {

        min = i;

        for j in (i+1)..array.len() {

            if array[j] < array[min] {
                min = j;
            }
        }

        let tmp = array[i];
        array[i] = array[min];
        array[min] = tmp;
    }
}

fn main() {

    let mut values = [ 5, 8, 4, 1, 7, 2, 3, 6 ];
    println!("Hello, world! The value is {:?}", values);

    selection_sort(&mut values);
    println!("Hello, world! The value is {:?}", values);
}
share|improve this question

You could use min_by_key to find the minimum's index and the method swap defined in slices:

fn selection_sort(array: &mut [i32]) {
    let len = array.len();
    for i in 0..len {
        let min = (i..len).min_by_key(|x| array[*x])
                          .unwrap();
        array.swap(min, i);
    }
}
share|improve this answer
  1. No newlines after an opening brace but before the code starts
  2. Use spaces around binary operators like +.
  3. Use swap. This prevents you from needing to make a copy/clone of the variable.
  4. Declare variables in as small a scope as possible. There's no reason to declare min outside of the array.
  5. I'd use more iterator methods like enumerate to avoid array indexing, which incurs a small penalty for out-of-bounds checking.
fn selection_sort(array: &mut [i32]) {
    for i in 0..array.len() {
        let min_idx = array[i..].iter()
            .enumerate()
            .min_by_key(|&(_, v)| v)
            .map(|(i, _)| i)
            .unwrap_or(0);

        array.swap(i, min_idx + i);
    }
}

As a follow-up, I'd challenge you to make this algorithm work with more than just i32 types!

share|improve this answer

Why use selection sort? It has O(N^2) best, average AND worst case time complexity while Insertion Sort has O(N) best case, and O(N^2) worst and average case. Try Insertion Sort instead (or Quick Sort if your system can handle the recursion). Here's a good implementation of Insertion Sort done in Java. Basically, we go through the array and if we see that a value is out of place (not sorted correctly) then we move it towards the start of the array until it fits between two appropriate values.

    public static void insertionSort(int[] ar)
{       
    int index = 0;
    while (index < ar.length - 1) {
        if (ar[index] > ar[index + 1]) {
            swap(ar, index, index + 1);
            int oldIndex = index;
            while (index > 0 && ar[index] < ar[index - 1]) {
                swap(ar, index, index - 1);
                index--;
            }
            index = oldIndex + 1;
        }
        else
            index++;
    }
}

swap is just a helper method that switches two elements in an array.

Also check out:

http://bigocheatsheet.com/

https://en.wikipedia.org/wiki/Selection_sort

share|improve this answer
4  
I am studying Rust and just want to implement some algorithms in Rust to have a practice in this language. I know difference between sorting algorithms, but not sure that my Rust code is idiomatic. – demas Mar 14 at 8:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.