Added Rust implementations of algos

This commit is contained in:
devoalda 2023-04-10 22:16:56 +08:00
parent 0a84de2d2c
commit e54a39cc2c
218 changed files with 172 additions and 0 deletions

0
.gitlab-ci.yml Normal file → Executable file
View File

0
.gitmodules vendored Normal file → Executable file
View File

0
LICENSE Normal file → Executable file
View File

0
archetypes/about.md Normal file → Executable file
View File

0
archetypes/archive.md Normal file → Executable file
View File

0
archetypes/author.md Normal file → Executable file
View File

0
archetypes/code.md Normal file → Executable file
View File

0
archetypes/contact.md Normal file → Executable file
View File

0
archetypes/default.md Normal file → Executable file
View File

0
archetypes/gallery.md Normal file → Executable file
View File

0
archetypes/header.md Normal file → Executable file
View File

0
archetypes/presentation.md Normal file → Executable file
View File

0
archetypes/publication.md Normal file → Executable file
View File

0
archetypes/resume.md Normal file → Executable file
View File

0
archetypes/rich.md Normal file → Executable file
View File

0
archetypes/showcase.md Normal file → Executable file
View File

0
archetypes/talk.md Normal file → Executable file
View File

0
config.toml Normal file → Executable file
View File

0
config/_default/config.toml Normal file → Executable file
View File

0
config/_default/languages.toml Normal file → Executable file
View File

0
config/_default/menus.en.toml Normal file → Executable file
View File

0
config/_default/params.toml Normal file → Executable file
View File

0
content/en/_index.md Normal file → Executable file
View File

0
content/en/about/index.md Normal file → Executable file
View File

0
content/en/archive/_index.md Normal file → Executable file
View File

0
content/en/gallery/Carnage/index.md Normal file → Executable file
View File

0
content/en/gallery/Mysterio/index.md Normal file → Executable file
View File

0
content/en/posts/2022-10-12-picoctf-mod-26.md Normal file → Executable file
View File

0
content/en/posts/2022-10-12-picoctf-obedient-cat.md Normal file → Executable file
View File

0
content/en/posts/2022-11-13-SumOf5DigitNo.md Normal file → Executable file
View File

View File

View File

View File

0
content/en/posts/2022-11-18-picoctf-wave-a-flag.md Normal file → Executable file
View File

View File

View File

View File

0
content/en/posts/2022-11-19-leetcode-two-sums-1.md Normal file → Executable file
View File

0
content/en/posts/2022-11-20-hackerrank-handshake.md Normal file → Executable file
View File

View File

0
content/en/posts/2022-11-21-the-factorial-function.md Normal file → Executable file
View File

View File

View File

0
content/en/posts/BinarySearch.md Normal file → Executable file
View File

0
content/en/posts/Hugo.md Normal file → Executable file
View File

0
content/en/posts/Leetcode-BinarySearch704.md Normal file → Executable file
View File

0
content/en/posts/Leetcode-FirstBadVersion278.md Normal file → Executable file
View File

View 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.

View File

@ -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.

0
content/en/posts/_index.md Normal file → Executable file
View File

0
content/en/posts/arch_installation.md Normal file → Executable file
View File

0
content/en/posts/bash_cheatsheet.md Normal file → Executable file
View File

0
content/en/posts/directory_cleanup.md Normal file → Executable file
View File

0
content/en/posts/equation_solver.md Normal file → Executable file
View File

0
content/en/posts/fail2ban.md Normal file → Executable file
View File

0
content/en/posts/leetcode-DefangIP.md Normal file → Executable file
View File

0
content/en/posts/nbs.md Normal file → Executable file
View File

0
content/en/posts/privacy.md Normal file → Executable file
View File

0
content/en/posts/pywal.md Normal file → Executable file
View File

0
content/en/posts/ssh.md Normal file → Executable file
View File

0
content/en/posts/welcome.md Normal file → Executable file
View File

0
content/en/showcase/Hugo/_index.md Normal file → Executable file
View File

0
content/en/showcase/Hugo/hugo-theme-zdoc.md Normal file → Executable file
View File

0
content/en/showcase/Hugo/hugo-theme-zzo.md Normal file → Executable file
View File

0
content/en/showcase/_index.md Normal file → Executable file
View File

0
content/en/showcase/linux/_index.md Normal file → Executable file
View File

0
content/en/showcase/linux/devoalda.website.io.md Normal file → Executable file
View File

0
content/en/showcase/linux/devohome.md Normal file → Executable file
View File

0
content/en/showcase/linux/dotfiles.md Normal file → Executable file
View File

0
data/font.toml Normal file → Executable file
View File

0
layouts/robots.txt Normal file → Executable file
View File

0
public/images/postImages/hugo.svg Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.5 KiB

0
static/.well-known/brave-rewards-verification.txt Normal file → Executable file
View File

0
static/favicon/android-icon-144x144.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 8.3 KiB

After

Width:  |  Height:  |  Size: 8.3 KiB

0
static/favicon/android-icon-192x192.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 7.5 KiB

After

Width:  |  Height:  |  Size: 7.5 KiB

0
static/favicon/android-icon-36x36.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB

0
static/favicon/android-icon-48x48.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.5 KiB

0
static/favicon/android-icon-72x72.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 3.4 KiB

After

Width:  |  Height:  |  Size: 3.4 KiB

0
static/favicon/android-icon-96x96.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 4.3 KiB

After

Width:  |  Height:  |  Size: 4.3 KiB

0
static/favicon/apple-icon-114x114.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 6.1 KiB

After

Width:  |  Height:  |  Size: 6.1 KiB

0
static/favicon/apple-icon-120x120.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 6.5 KiB

After

Width:  |  Height:  |  Size: 6.5 KiB

0
static/favicon/apple-icon-144x144.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 8.3 KiB

After

Width:  |  Height:  |  Size: 8.3 KiB

0
static/favicon/apple-icon-152x152.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 8.8 KiB

After

Width:  |  Height:  |  Size: 8.8 KiB

0
static/favicon/apple-icon-180x180.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

0
static/favicon/apple-icon-57x57.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

0
static/favicon/apple-icon-60x60.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

0
static/favicon/apple-icon-72x72.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 3.4 KiB

After

Width:  |  Height:  |  Size: 3.4 KiB

0
static/favicon/apple-icon-76x76.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 3.5 KiB

After

Width:  |  Height:  |  Size: 3.5 KiB

0
static/favicon/apple-icon-precomposed.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

0
static/favicon/apple-icon.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 10 KiB

After

Width:  |  Height:  |  Size: 10 KiB

0
static/favicon/browserconfig.xml Normal file → Executable file
View File

0
static/favicon/favicon-16x16.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

0
static/favicon/favicon-32x32.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 2.0 KiB

0
static/favicon/favicon-96x96.png Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 4.3 KiB

After

Width:  |  Height:  |  Size: 4.3 KiB

0
static/favicon/favicon.ico Normal file → Executable file
View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

Some files were not shown because too many files have changed in this diff Show More