Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ca74189

Browse files
committed
Add sort.rs
1 parent 92d4a62 commit ca74189

File tree

4 files changed

+100
-13
lines changed

4 files changed

+100
-13
lines changed

‎labocédai/src/lib.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
pub mod simple_challenges;
21
pub mod recursion;
32
pub mod search;
3+
pub mod simple_challenges;
4+
pub mod sort;
45

56
#[cfg(test)]
67
mod tests {
@@ -125,9 +126,51 @@ mod tests {
125126

126127
#[test]
127128
fn test_binary_search_nums() {
128-
assert_eq!(binary_search_nums(&[1, 2, 3, 4, 5], 3), 2);
129+
assert_eq!(binary_search_nums(&[1, 2, 3, 4, 5], 1), 0);
129130
assert_eq!(binary_search_nums(&[1, 2, 3, 3, 4, 5], 6), -1);
131+
assert_eq!(binary_search_nums(&[3, 3, 3, 4, 5, 5, 6], 6), 6);
132+
assert_eq!(binary_search_nums(&[3, 3, 3, 4, 5, 5, 5, 6], 6), 7);
130133
assert_eq!(binary_search_nums(&[], 6), -1);
131134
}
132135
}
136+
137+
mod sort {
138+
use crate::sort::*;
139+
140+
#[test]
141+
fn test_bubble_sort() {
142+
assert_eq!(
143+
bubble_sort(&mut [29, 10, 14, 30, 37, 14, 18]),
144+
[10, 14, 14, 18, 29, 30, 37]
145+
);
146+
assert_eq!(bubble_sort(&mut []), []);
147+
assert_eq!(bubble_sort(&mut [1, 1, 1, 2, 1]), [1, 1, 1, 1, 2]);
148+
assert_eq!(bubble_sort(&mut [2, 1, 1, 1, 2]), [1, 1, 1, 2, 2]);
149+
assert_eq!(bubble_sort(&mut [1]), [1]);
150+
}
151+
152+
#[test]
153+
fn test_selection_sort() {
154+
assert_eq!(
155+
selection_sort(&mut [29, 10, 14, 30, 37, 14, 18]),
156+
[10, 14, 14, 18, 29, 30, 37]
157+
);
158+
assert_eq!(selection_sort(&mut []), []);
159+
assert_eq!(selection_sort(&mut [1, 1, 1, 2, 1]), [1, 1, 1, 1, 2]);
160+
assert_eq!(selection_sort(&mut [2, 1, 1, 1, 2]), [1, 1, 1, 2, 2]);
161+
assert_eq!(selection_sort(&mut [1]), [1]);
162+
}
163+
164+
#[test]
165+
fn test_insertion_sort() {
166+
assert_eq!(
167+
insertion_sort(&mut [29, 10, 14, 30, 37, 14, 18]),
168+
[10, 14, 14, 18, 29, 30, 37]
169+
);
170+
assert_eq!(insertion_sort(&mut []), []);
171+
assert_eq!(insertion_sort(&mut [1, 1, 1, 2, 1]), [1, 1, 1, 1, 2]);
172+
assert_eq!(insertion_sort(&mut [2, 1, 1, 1, 2]), [1, 1, 1, 2, 2]);
173+
assert_eq!(insertion_sort(&mut [1]), [1]);
174+
}
175+
}
133176
}

‎labocédai/src/recursion.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub fn reverse(s: &str) -> String {
4444
}
4545
let char = match s.chars().nth(0) {
4646
Some(c) => c.to_string(),
47-
None => "".to_string()
47+
None => "".to_string(),
4848
};
49-
return reverse(&s[1..s.len()]) + &char
50-
}
49+
return reverse(&s[1..s.len()]) + &char;
50+
}

‎labocédai/src/search.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub fn linear_search(nums: &[isize], target: isize) -> isize {
22
/* takes a collection of nums and a target number to search for;
3-
returns the index or -1 if not found */
3+
returns the index or -1 if not found */
44
for (i, num) in nums.iter().enumerate() {
55
if num == &target {
66
return i.try_into().unwrap();
@@ -10,22 +10,22 @@ pub fn linear_search(nums: &[isize], target: isize) -> isize {
1010
}
1111

1212
pub fn binary_search_nums(nums: &[isize], target: isize) -> isize {
13-
/* takes a collection of nums and a target number to search for;
14-
returns the index or -1 if not found */
13+
/* takes a sorted collection of nums and a target number to search for;
14+
returns the index or -1 if not found */
1515
if nums.len() == 0 {
1616
return -1;
1717
}
1818
let mut left = 0;
1919
let mut right = nums.len() - 1;
20-
while left < right {
20+
while left <= right {
2121
let midpoint = (left + right) / 2;
2222
if nums[midpoint] == target {
2323
return midpoint.try_into().unwrap();
24-
} else if nums[midpoint] < target {
25-
right = midpoint;
2624
} else if nums[midpoint] > target {
27-
left = midpoint;
25+
right = midpoint - 1;
26+
} else if nums[midpoint] < target {
27+
left = midpoint + 1;
2828
}
2929
}
3030
-1
31-
}
31+
}

‎labocédai/src/sort.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
pub fn bubble_sort(nums: &mut [isize]) -> &mut [isize] {
2+
for i in 0..nums.len() {
3+
let mut hasnt_swapped = true;
4+
for j in 0..nums.len() - i {
5+
if j < nums.len() - 1 && nums[j] > nums[j + 1] {
6+
nums.swap(j, j + 1);
7+
hasnt_swapped = false;
8+
}
9+
}
10+
if hasnt_swapped {
11+
return nums;
12+
}
13+
}
14+
nums
15+
}
16+
17+
pub fn selection_sort(nums: &mut [isize]) -> &mut [isize] {
18+
for i in 0..nums.len() {
19+
let mut min_num_idx = i;
20+
for j in i..nums.len() {
21+
if nums[j] < nums[min_num_idx] {
22+
min_num_idx = j;
23+
}
24+
}
25+
nums.swap(i, min_num_idx);
26+
}
27+
nums
28+
}
29+
30+
pub fn insertion_sort(nums: &mut [isize]) -> &mut [isize] {
31+
if nums.len() < 2 {
32+
return nums;
33+
}
34+
for i in 1..nums.len() {
35+
for j in (0..i).rev() {
36+
if nums[j + 1] < nums[j] {
37+
nums.swap(j, j + 1);
38+
} else {
39+
break;
40+
}
41+
}
42+
}
43+
nums
44+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /