Skip to content

Commit

Permalink
Insertion Sort Added
Browse files Browse the repository at this point in the history
Signed-off-by: yourarj <[email protected]>

 Date:      Sat Dec 30 22:41:11 2023 +0530

 On branch main

 Changes to be committed:
	modified:   Cargo.lock
	modified:   Cargo.toml
	renamed:    codility-array-rotation/src/main.rs -> codility-array-rotation/src/lib.rs
	renamed:    codility-binary-gap/src/main.rs -> codility-binary-gap/src/lib.rs
	deleted:    codility-frog-jump/Cargo.toml
	deleted:    codility-frog-jump/src/main.rs
	renamed:    codility-possible-string-palindrome/src/main.rs -> codility-possible-string-palindrome/src/lib.rs
	modified:   leetcode-design-a-stack-with-increment-operation/src/lib.rs
	modified:   leetcode-two-sum/src/main.rs
	renamed:    merge-sort/Cargo.toml -> sorting-algorithms/Cargo.toml
	new file:   sorting-algorithms/src/insertion_sort.rs
	new file:   sorting-algorithms/src/lib.rs
	renamed:    merge-sort/src/lib.rs -> sorting-algorithms/src/merge_sort.rs
	modified:   university-of-princeton-3sum/src/lib.rs
	modified:   university-of-princeton-path-compressed-weighted-quick-union-canonical-element/src/lib.rs
  • Loading branch information
yourarj committed Dec 30, 2023
1 parent 22ef2d0 commit 5fe730d
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 66 deletions.
4 changes: 0 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ members = [
"codility-possible-string-palindrome",
"codility-binary-gap",
"codility-array-rotation",
"codility-frog-jump",
"leetcode-queue-reconstruction-by-height",
"leetcode-add-two-numbers",
"leetcode-basic-calculator-ii",
Expand All @@ -34,7 +33,7 @@ members = [
"adaface-rust-tricky-quiz",
"gg-minimize-the-heights",
"leetcode-reverse-an-array-recursive",
"merge-sort",
"sorting-algorithms",
"leetcode-roman-to-integer",
"leetcode-palindrome-number",
"leetcode-longest-common-prefix",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
fn main() {
assert_eq!(solution(&mut [3, 8, 9, 7, 6], 3), [9, 7, 6, 3, 8]);
assert_eq!(solution(&mut [1, 2, 3, 4], 4), [1, 2, 3, 4]);
}

fn solution(input_array: &mut [i32], rotations: i32) -> &[i32] {
pub fn solution(input_array: &mut [i32], rotations: i32) -> &[i32] {
if input_array.is_empty() {
return input_array;
}
Expand All @@ -20,3 +15,14 @@ fn solution(input_array: &mut [i32], rotations: i32) -> &[i32] {

input_array
}

#[cfg(test)]
mod tests {
use crate::solution;

#[test]
fn it_works() {
assert_eq!(solution(&mut [3, 8, 9, 7, 6], 3), [9, 7, 6, 3, 8]);
assert_eq!(solution(&mut [1, 2, 3, 4], 4), [1, 2, 3, 4]);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
fn main() {
println!("Max gap for {} is {}", 1041, solution(1041));
println!("Max gap for {} is {}", 10, solution(10));
println!("Max gap for {} is {}", 10, solution(13748));
println!("Max gap for {} is {}", 10, solution(1_000_000_001));
println!("Max gap for {} is {}", 10, solution(10123));
}

fn solution(mut num: i32) -> i32 {
pub fn solution(mut num: i32) -> i32 {
let mut max_gap = 0;

let mut current_gap = 0;
Expand All @@ -24,3 +16,9 @@ fn solution(mut num: i32) -> i32 {
}
max_gap
}

#[cfg(test)]
mod tests {
#[test]
fn it_works() {}
}
8 changes: 0 additions & 8 deletions codility-frog-jump/Cargo.toml

This file was deleted.

3 changes: 0 additions & 3 deletions codility-frog-jump/src/main.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
fn main() {
println!("?a? : {}", solution("?a?"));
println!();

println!("abac?a? : {}", solution("abac?a?"));
println!();

println!("???? : {}", solution("????"));
println!();

println!("??????? : {}", solution("???????"));
println!();

println!("??ab : {}", solution("??ab"));
println!();

println!("???????asdfce : {}", solution("???????asdfce"));
}

fn solution(str: &str) -> String {
pub fn solution(str: &str) -> String {
let str_length = str.len();
let approx_mid = str_length as f32 / 2_f32;

Expand Down Expand Up @@ -50,3 +31,23 @@ fn solution(str: &str) -> String {
}
str_chars.into_iter().collect()
}

#[cfg(test)]
mod tests {
use crate::solution;

#[test]
fn it_works() {
assert_eq!("aaa", solution("?a?"));

assert_eq!("NO", solution("abac?a?"));

assert_eq!("aaaa", solution("????"));

assert_eq!("aaaaaaa", solution("???????"));

assert_eq!("baab", solution("??ab"));

assert_eq!("ecfdsaaasdfce", solution("???????asdfce"));
}
}
6 changes: 0 additions & 6 deletions leetcode-design-a-stack-with-increment-operation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,8 @@ impl CustomStack {
}

pub fn push(&mut self, x: i32) {
println!(
"Push {:02} : Current stack length is {}",
x,
self.inner.len()
);
if self.inner.len() < self.max_size as usize {
self.inner.push(x);
println!("{:?}", self.inner);
}
}

Expand Down
4 changes: 1 addition & 3 deletions leetcode-two-sum/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
fn main() {
println!("Hello, world!");
}
fn main() {}

use std::collections::HashMap;
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
Expand Down
File renamed without changes.
58 changes: 58 additions & 0 deletions sorting-algorithms/src/insertion_sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
pub fn sort(arr: &mut [i32]) {
if arr.is_empty() {
return;
}

for i in 1..arr.len() {
let mut j = i;
while j > 0 && arr[j - 1] > arr[j] {
arr.swap(j - 1, j);
j -= 1;
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let mut arr = [5, 4, 3, 2, 1, 0];
sort(&mut arr);

assert_eq!(arr[0], 0);
assert_eq!(arr[3], 3);
assert_eq!(arr[5], 5);
}

#[test]
fn sort_negative_numbers() {
let mut arr = [5, -4, 3, 2, -1, 0];
sort(&mut arr);

assert_eq!(arr[0], -4);
assert_eq!(arr[1], -1);
assert_eq!(arr[2], 0);
assert_eq!(arr[3], 2);
assert_eq!(arr[4], 3);
assert_eq!(arr[5], 5);
}

#[test]
fn sort_empty_array() {
let mut arr = [];
sort(&mut arr);
assert!(arr.is_empty());
}

#[test]
fn it_works_02() {
let mut arr = [5, 5, 5, 4, 4, 2, 2, 2, 2, 1, 0];
sort(&mut arr);

assert_eq!(arr[0], 0);
assert_eq!(arr[3], 2);
assert_eq!(arr[5], 2);
}
}
1 change: 1 addition & 0 deletions sorting-algorithms/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod merge_sort;
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ mod tests {
let mut arr = [5, 4, 3, 2, 1, 0];
merge_sort(&mut arr, 0, 5);

println!("arr: {:?}", &arr);
assert_eq!(arr[0], 0);
assert_eq!(arr[3], 3);
assert_eq!(arr[5], 5);
Expand All @@ -62,7 +61,6 @@ mod tests {
let end = arr.len() - 1;
merge_sort(&mut arr, 0, end);

println!("arr: {:?}", &arr);
assert_eq!(arr[0], 0);
assert_eq!(arr[3], 2);
assert_eq!(arr[5], 2);
Expand Down
1 change: 0 additions & 1 deletion university-of-princeton-3sum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ mod tests {
fn it_works() {
let mut arr = [-1, 1, 2, -1, 5, -3];
let result = ThreeSum::three_sum(&mut arr);
println!("result is {:?}", result);

assert!(result.first().eq(&Some(&vec![-3, 1, 2])));
assert!(result.get(1).eq(&Some(&vec![-1, -1, 2])));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ mod tests {
fn it_works_from_lecture() {
let mut qf = WeightedQuickUnion::<10>::new();
qf.union(4, 3);
println!("{}", qf.find_largest(3));

println!("{}", qf.find_largest(3));
qf.union(3, 8);
qf.union(6, 5);
qf.union(9, 4);
Expand Down

0 comments on commit 5fe730d

Please sign in to comment.