Added Rust implementations of algos
0
content/en/posts/2022-11-20-hackerrank-project-euler-1-multiples-of-3-and-5.md
Normal file → Executable file
0
content/en/posts/2023-02-11-leetcode-build-array-from-permutation.md
Normal file → Executable file
|
@ -0,0 +1,75 @@
|
||||||
|
---
|
||||||
|
author: "Devoalda"
|
||||||
|
authorEmoji: 🐺
|
||||||
|
title: "Rust Binary Search"
|
||||||
|
date: 2023-04-10T21:42:22+08:00
|
||||||
|
description: Binary Search in Rust
|
||||||
|
draft: false
|
||||||
|
hideToc: false
|
||||||
|
enableToc: true
|
||||||
|
enableTocContent: true
|
||||||
|
tocPosition: inner
|
||||||
|
tocLevels: ["h1", "h2", "h3"]
|
||||||
|
tags:
|
||||||
|
- rust
|
||||||
|
- Searching Algorithm
|
||||||
|
- Binary Search
|
||||||
|
series:
|
||||||
|
- rust
|
||||||
|
categories:
|
||||||
|
- Searching Algorithm
|
||||||
|
image:
|
||||||
|
libraries:
|
||||||
|
- mathjax
|
||||||
|
math: true
|
||||||
|
---
|
||||||
|
|
||||||
|
# Abstract
|
||||||
|
[Binary Search]( {{< ref "posts/BinarySearch.md" >}}) is one of my favourite searchin algorithms, it is quick and efficient most of the times to search, given a sorted array.
|
||||||
|
|
||||||
|
This is an implementation of this algorithm in Rust. This post marks the start of my Rust learning journey. Implementing this algorithm is a must and this allows me to learn the Rust syntax given a familiar algorithm.
|
||||||
|
|
||||||
|
In this implementation, a vector is used instead of an array, this allows for dynamic memory allocation of a given list of sorted numbers.
|
||||||
|
|
||||||
|
# Code
|
||||||
|
|
||||||
|
```rust
|
||||||
|
fn main() {
|
||||||
|
let vec = vec![1,2,3,4,5,6,7,8,9,10];
|
||||||
|
let val = 3;
|
||||||
|
let pos = binary_search(vec, val);
|
||||||
|
|
||||||
|
if pos != -1 {
|
||||||
|
println!("{} is at position {}", val, pos)
|
||||||
|
} else {
|
||||||
|
println!("{} is not in the vector", val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn binary_search(vec: Vec<i32>, val:i32) -> i32{
|
||||||
|
let mut left:i32 = 0;
|
||||||
|
let mut right:i32 = vec.len() as i32;
|
||||||
|
let result = -1;
|
||||||
|
|
||||||
|
while left <= right {
|
||||||
|
let mid:i32 = (left + right) >> 1;
|
||||||
|
|
||||||
|
if vec[mid as usize] == val {
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
else if vec[mid as usize] < val {
|
||||||
|
left = mid + 1;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
right = mid - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Output:
|
||||||
|
```bash
|
||||||
|
3 is at position 2
|
||||||
|
```
|
||||||
|
|
||||||
|
Similar to the previous implementation of [Binary Search]( {{< ref "posts/BinarySearch.md" >}}), I used the right shift `>> 1` to do the division by 2 to get the `mid` value, followed by the iterative version of the comparison.
|
|
@ -0,0 +1,97 @@
|
||||||
|
---
|
||||||
|
author: "Devoalda"
|
||||||
|
authorEmoji: 🐺
|
||||||
|
title: "Rust Merge Sort"
|
||||||
|
date: 2023-04-10T22:10:04+08:00
|
||||||
|
description: Merge Sort in Rust
|
||||||
|
draft: false
|
||||||
|
hideToc: false
|
||||||
|
enableToc: true
|
||||||
|
enableTocContent: true
|
||||||
|
tocPosition: inner
|
||||||
|
tocLevels: ["h1", "h2", "h3"]
|
||||||
|
tags:
|
||||||
|
- Sorting Algorithms
|
||||||
|
- Merge Sort
|
||||||
|
series:
|
||||||
|
- rust
|
||||||
|
categories:
|
||||||
|
- rust
|
||||||
|
- Sorting Algorithms
|
||||||
|
image:
|
||||||
|
libraries:
|
||||||
|
- mathjax
|
||||||
|
math: true
|
||||||
|
---
|
||||||
|
|
||||||
|
# Abstract
|
||||||
|
|
||||||
|
Merge Sort is one quick and efficient Sorting algorithm, with a time complexity of $O(n \log n)$. This is another familiar algorithm that I've managed to implement in Rust as part of learning the Rust Syntax.
|
||||||
|
|
||||||
|
The algorithm takes in a vector and sorts it with the merge sort algorithm, returning the sorted vector.
|
||||||
|
|
||||||
|
In this implementation, a vector of 10 random integers are used and sorted.
|
||||||
|
|
||||||
|
# Code
|
||||||
|
|
||||||
|
```rust
|
||||||
|
fn main() {
|
||||||
|
let mut vec = vec![];
|
||||||
|
for _ in 0..10 {
|
||||||
|
let mut val = rand::random::<i32>() % 100;
|
||||||
|
vec.push(val);
|
||||||
|
}
|
||||||
|
println!("Unsorted vector: {:?}", vec);
|
||||||
|
merge_sort(&mut vec);
|
||||||
|
println!("Sorted vector: {:?}", vec);
|
||||||
|
}
|
||||||
|
fn merge_sort(vec: &mut Vec<i32>) {
|
||||||
|
let mut left = vec![];
|
||||||
|
let mut right = vec![];
|
||||||
|
let mid = vec.len() >> 1;
|
||||||
|
if vec.len() > 1 {
|
||||||
|
// Split vector into two halves
|
||||||
|
for i in 0..mid {
|
||||||
|
left.push(vec[i]);
|
||||||
|
}
|
||||||
|
for i in mid..vec.len() {
|
||||||
|
right.push(vec[i]);
|
||||||
|
}
|
||||||
|
merge_sort(&mut left);
|
||||||
|
merge_sort(&mut right);
|
||||||
|
*vec = merge(&mut left, &mut right); // Merge the two halves and assign to vec
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn merge(left: &mut Vec<i32>, right: &mut Vec<i32>) -> Vec<i32> {
|
||||||
|
let mut left_index = 0;
|
||||||
|
let mut right_index = 0;
|
||||||
|
let mut vec = vec![];
|
||||||
|
while left_index < left.len() && right_index < right.len() {
|
||||||
|
if left[left_index] < right[right_index] {
|
||||||
|
vec.push(left[left_index]);
|
||||||
|
left_index += 1;
|
||||||
|
} else {
|
||||||
|
vec.push(right[right_index]);
|
||||||
|
right_index += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while left_index < left.len() {
|
||||||
|
vec.push(left[left_index]);
|
||||||
|
left_index += 1;
|
||||||
|
}
|
||||||
|
while right_index < right.len() {
|
||||||
|
vec.push(right[right_index]);
|
||||||
|
right_index += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
vec // Return merged vector
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```bash
|
||||||
|
Unsorted vector: [-92, -91, 0, 55, 98, 8, -71, -3, -56, -63]
|
||||||
|
Sorted vector: [-92, -91, -71, -63, -56, -3, 0, 8, 55, 98]
|
||||||
|
```
|
||||||
|
|
||||||
|
This uses the `rand` module in Rust to generate the random numbers. This is the merge sort algorithm implemented in Rust.
|
Before Width: | Height: | Size: 2.5 KiB After Width: | Height: | Size: 2.5 KiB |
0
resources/_gen/assets/scss/sass/main.scss_2db6762e963dbcacb7e05525e366a39a.content
Normal file → Executable file
0
resources/_gen/assets/scss/sass/main.scss_2db6762e963dbcacb7e05525e366a39a.json
Normal file → Executable file
0
resources/_gen/assets/scss/sass/main.scss_b4f67ac5085b89b62b54c1923e5a9145.content
Normal file → Executable file
0
resources/_gen/assets/scss/sass/main.scss_b4f67ac5085b89b62b54c1923e5a9145.json
Normal file → Executable file
Before Width: | Height: | Size: 8.3 KiB After Width: | Height: | Size: 8.3 KiB |
Before Width: | Height: | Size: 7.5 KiB After Width: | Height: | Size: 7.5 KiB |
Before Width: | Height: | Size: 2.1 KiB After Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 2.5 KiB After Width: | Height: | Size: 2.5 KiB |
Before Width: | Height: | Size: 3.4 KiB After Width: | Height: | Size: 3.4 KiB |
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 4.3 KiB |
Before Width: | Height: | Size: 6.1 KiB After Width: | Height: | Size: 6.1 KiB |
Before Width: | Height: | Size: 6.5 KiB After Width: | Height: | Size: 6.5 KiB |
Before Width: | Height: | Size: 8.3 KiB After Width: | Height: | Size: 8.3 KiB |
Before Width: | Height: | Size: 8.8 KiB After Width: | Height: | Size: 8.8 KiB |
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 11 KiB |
Before Width: | Height: | Size: 2.9 KiB After Width: | Height: | Size: 2.9 KiB |
Before Width: | Height: | Size: 3.0 KiB After Width: | Height: | Size: 3.0 KiB |
Before Width: | Height: | Size: 3.4 KiB After Width: | Height: | Size: 3.4 KiB |
Before Width: | Height: | Size: 3.5 KiB After Width: | Height: | Size: 3.5 KiB |
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 2.0 KiB After Width: | Height: | Size: 2.0 KiB |
Before Width: | Height: | Size: 4.3 KiB After Width: | Height: | Size: 4.3 KiB |
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |