Skip to content

Commit c96c111

Browse files
author
build
committed
feat:exercises
1 parent 55c5e6f commit c96c111

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

exercises/algorithm/algorithm3.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@
33
This problem requires you to implement a sorting algorithm
44
you can use bubble sorting, insertion sorting, heap sorting, etc.
55
*/
6-
// I AM NOT DONE
76

8-
fn sort<T>(array: &mut [T]){
9-
//TODO
7+
fn sort<T: PartialOrd>(array: &mut [T]){
8+
if array.is_empty() || array.len() <= 1 {
9+
return;
10+
}
11+
12+
let length = array.len();
13+
for i in 0..length {
14+
for j in 0..length-i-1 {
15+
if array[j] > array[j+1] {
16+
array.swap(j, j+1);
17+
}
18+
}
19+
}
20+
1021
}
1122
#[cfg(test)]
1223
mod tests {

0 commit comments

Comments
 (0)