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 d59bb3a

Browse files
committed
Add merge fn for merge sort.rs; Move eslint to dev dep
1 parent ca74189 commit d59bb3a

File tree

6 files changed

+280
-78
lines changed

6 files changed

+280
-78
lines changed

‎labocédai/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,5 +172,12 @@ mod tests {
172172
assert_eq!(insertion_sort(&mut [2, 1, 1, 1, 2]), [1, 1, 1, 2, 2]);
173173
assert_eq!(insertion_sort(&mut [1]), [1]);
174174
}
175+
176+
#[test]
177+
fn test_merge() {
178+
assert_eq!(merge(&[1, 2, 3], &[4, 5, 6]), vec![1, 2, 3, 4, 5, 6]);
179+
assert_eq!(merge(&[], &[]), vec![]);
180+
assert_eq!(merge(&[1, 3, 5], &[2, 4, 6]), vec![1, 2, 3, 4, 5, 6]);
181+
}
175182
}
176183
}

‎labocédai/src/recursion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ pub fn fibonacci(n: usize) -> usize {
3939
}
4040

4141
pub fn reverse(s: &str) -> String {
42-
if s.len() == 0 {
42+
if s.is_empty() {
4343
return s.to_owned();
4444
}
45-
let char = match s.chars().nth(0) {
45+
let char = match s.chars().next() {
4646
Some(c) => c.to_string(),
4747
None => "".to_string(),
4848
};

‎labocédai/src/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn linear_search(nums: &[isize], target: isize) -> isize {
1212
pub fn binary_search_nums(nums: &[isize], target: isize) -> isize {
1313
/* takes a sorted collection of nums and a target number to search for;
1414
returns the index or -1 if not found */
15-
if nums.len() == 0 {
15+
if nums.is_empty() {
1616
return -1;
1717
}
1818
let mut left = 0;

‎labocédai/src/sort.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,30 @@ pub fn insertion_sort(nums: &mut [isize]) -> &mut [isize] {
4242
}
4343
nums
4444
}
45+
46+
pub fn merge(first_arr: &[isize], second_arr: &[isize]) -> Vec<isize> {
47+
/* merges two similarly sorted arrays */
48+
let mut merged = Vec::with_capacity(first_arr.len() + second_arr.len());
49+
let mut i = 0;
50+
let mut j = 0;
51+
while i < first_arr.len() && j < second_arr.len() {
52+
if first_arr[i] <= second_arr[j] {
53+
merged.push(first_arr[i]);
54+
i += 1;
55+
} else if first_arr[i] > second_arr[j] {
56+
merged.push(second_arr[j]);
57+
j += 1;
58+
}
59+
}
60+
// push remaining values in unexhausted array into merged
61+
if i < first_arr.len() {
62+
for num in first_arr.iter().skip(i) {
63+
merged.push(*num);
64+
}
65+
} else if j < second_arr.len() {
66+
for num in second_arr.iter().skip(j) {
67+
merged.push(*num);
68+
}
69+
}
70+
merged
71+
}

0 commit comments

Comments
(0)

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