forked from thuva4/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.rs
More file actions
46 lines (34 loc) · 776 Bytes
/
Copy pathselection_sort.rs
File metadata and controls
46 lines (34 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Implementation of selection_sort in Rust
*/
fn selection_sort(mut list: Vec<i32>) -> Vec<i32> {
let n = list.len();
for j in 0..n-1 {
let mut cur_min = j;
for i in j+1..n {
if list[i] < list[cur_min] {
cur_min = i;
}
}
if cur_min != j {
list.swap(j, cur_min);
}
}
return list;
}
fn main() {
let mut mylist = Vec::new();
mylist.push(5);
mylist.push(4);
mylist.push(8);
mylist.push(9);
mylist.push(20);
mylist.push(14);
mylist.push(3);
mylist.push(1);
mylist.push(2);
mylist.push(2);
println!("{:?}", mylist);
let selection_sorted = selection_sort(mylist);
println!("{:?}", selection_sorted);
}