Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d706339351 | |||
| 09c121833d | |||
| bf28764ef5 | |||
| 1d806e22a4 |
144
README.MD
Normal file
144
README.MD
Normal file
@ -0,0 +1,144 @@
|
|||||||
|
# Roadmap / Todo list
|
||||||
|
|
||||||
|
Because I am not familiar with Rust, so it will be a good practice
|
||||||
|
|
||||||
|
> Goal: Learn Rust by implementing classic algorithms\
|
||||||
|
> Principle: start simple, index-based first, avoid lifetimes early
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
|
||||||
|
## 🔍 Search
|
||||||
|
|
||||||
|
### 1. Binary Search
|
||||||
|
|
||||||
|
- Implement iterative version
|
||||||
|
- Return `Option<usize>`
|
||||||
|
- Practice slice borrowing: `&[T]`
|
||||||
|
|
||||||
|
**Rust focus** - `usize` - `Option` - bounds checking
|
||||||
|
|
||||||
|
**Extensions** - `lower_bound` - `upper_bound` - `partition_point`
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
|
||||||
|
## 🔃 Sort
|
||||||
|
|
||||||
|
### 2. Insertion Sort
|
||||||
|
|
||||||
|
- In-place sorting
|
||||||
|
- Good for understanding mutable slices
|
||||||
|
|
||||||
|
**Rust focus** - `&mut [T]` - index manipulation - borrow checker basics
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
|
||||||
|
### 3. Merge Sort
|
||||||
|
|
||||||
|
- Recursive implementation
|
||||||
|
- Use temporary `Vec`
|
||||||
|
|
||||||
|
**Rust focus** - ownership vs borrowing - recursion - `Vec` allocation
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
|
||||||
|
### 4. Generic Sort
|
||||||
|
|
||||||
|
``` rust
|
||||||
|
fn sort<T: Ord>(arr: &mut [T])
|
||||||
|
```
|
||||||
|
|
||||||
|
**Rust focus** - generics - trait bounds (`Ord`)
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
|
||||||
|
## 🧭 Graph
|
||||||
|
|
||||||
|
### 5. Graph Representation
|
||||||
|
|
||||||
|
Recommended structure:
|
||||||
|
|
||||||
|
``` rust
|
||||||
|
type Graph = Vec<Vec<(usize, i64)>>;
|
||||||
|
```
|
||||||
|
|
||||||
|
**Rust focus** - tuple - type alias - index-based graph
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
|
||||||
|
### 6. BFS / DFS
|
||||||
|
|
||||||
|
- Implement using adjacency list
|
||||||
|
- Use queue / stack
|
||||||
|
|
||||||
|
**Rust focus** - `VecDeque` - mutable borrow scope control
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
|
||||||
|
### 7. Dijkstra
|
||||||
|
|
||||||
|
- Use `BinaryHeap`
|
||||||
|
- Implement min-heap with `Reverse`
|
||||||
|
|
||||||
|
**Rust focus** - `BinaryHeap` - `Reverse<T>` - ownership in heap
|
||||||
|
elements
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
|
||||||
|
## 🌱 Extra (Optional but Recommended)
|
||||||
|
|
||||||
|
### 8. Union-Find (Disjoint Set)
|
||||||
|
|
||||||
|
- Path compression
|
||||||
|
- Union by rank
|
||||||
|
|
||||||
|
**Rust focus** - `struct` + `impl` - mutable state management
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
|
||||||
|
### 9. Tree Traversal
|
||||||
|
|
||||||
|
- Preorder / Inorder / Postorder
|
||||||
|
|
||||||
|
**Rust focus** - `Option<Box<T>>` - `match` exhaustiveness
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
|
||||||
|
## 🗂 Suggested Repository Structure
|
||||||
|
|
||||||
|
``` text
|
||||||
|
algorithms-in-rust/
|
||||||
|
├── src/
|
||||||
|
│ ├── search/
|
||||||
|
│ │ └── binary_search.rs
|
||||||
|
│ ├── sort/
|
||||||
|
│ │ ├── insertion.rs
|
||||||
|
│ │ └── merge.rs
|
||||||
|
│ ├── graph/
|
||||||
|
│ │ ├── bfs.rs
|
||||||
|
│ │ └── dijkstra.rs
|
||||||
|
│ ├── lib.rs
|
||||||
|
└── tests/
|
||||||
|
```
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
|
||||||
|
## 🧠 Implementation Notes
|
||||||
|
|
||||||
|
- Prefer index-based structures early
|
||||||
|
- Avoid `Rc<RefCell<T>>` unless truly needed
|
||||||
|
- If borrow checker complains:
|
||||||
|
- reduce scope
|
||||||
|
- split logic into smaller functions
|
||||||
|
|
||||||
|
------------------------------------------------------------------------
|
||||||
|
|
||||||
|
## 🎯 Recommended Order
|
||||||
|
|
||||||
|
1. Binary Search\
|
||||||
|
2. Insertion Sort\
|
||||||
|
3. Merge Sort\
|
||||||
|
4. BFS\
|
||||||
|
5. Dijkstra
|
||||||
|
|
||||||
|
After completing this list, you should be comfortable reading and
|
||||||
|
writing most Rust algorithm code.
|
||||||
18
src/main.rs
18
src/main.rs
@ -1,3 +1,19 @@
|
|||||||
|
mod search;
|
||||||
|
|
||||||
|
use search::binary_search::binary_search;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
let array = [1, 3, 5, 7, 9, 11, 13, 15];
|
||||||
|
let target = 7;
|
||||||
|
|
||||||
|
match binary_search(&array, target) {
|
||||||
|
Some(index) => println!("Found {} at index {}", target, index),
|
||||||
|
None => println!("{} not found in the array", target),
|
||||||
|
}
|
||||||
|
|
||||||
|
let target = 6;
|
||||||
|
match binary_search(&array, target) {
|
||||||
|
Some(index) => println!("Found {} at index {}", target, index),
|
||||||
|
None => println!("{} not found in the array", target),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
src/search/binary_search.rs
Normal file
16
src/search/binary_search.rs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
pub fn binary_search(array: &[i32], target: i32) -> Option<usize> {
|
||||||
|
let mut left = 0;
|
||||||
|
let mut right = array.len();
|
||||||
|
|
||||||
|
while left < right {
|
||||||
|
let mid = left + (right - left) / 2;
|
||||||
|
if array[mid] == target {
|
||||||
|
return Some(mid);
|
||||||
|
} else if array[mid] < target {
|
||||||
|
left = mid + 1;
|
||||||
|
} else {
|
||||||
|
right = mid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
1
src/search/mod.rs
Normal file
1
src/search/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod binary_search;
|
||||||
Loading…
x
Reference in New Issue
Block a user