Skip to content

Commit 9ec6e7e

Browse files
committed
Finished Chapter 7
1 parent 1890c85 commit 9ec6e7e

File tree

12 files changed

+275
-0
lines changed

12 files changed

+275
-0
lines changed

chapter_7/counting_sort/Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chapter_7/counting_sort/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "counting_sort"
3+
version = "0.1.0"
4+
authors = ["Yohei Osawa <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
input = {path = "../../library/input"}

chapter_7/counting_sort/src/main.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
fn main() {
2+
let vec: Vec<i32> = input::read_numline();
3+
let max_value = vec.iter().max().unwrap();
4+
let mut count_vec = vec![0; *max_value as usize + 1];
5+
for i in vec {
6+
count_vec[i as usize] += 1;
7+
}
8+
let mut sorted_vec = Vec::<i32>::new();
9+
for i in 0..count_vec.len() {
10+
if count_vec[i] != 0 {
11+
for _ in 0..count_vec[i]{
12+
sorted_vec.push(i as i32);
13+
}
14+
}
15+
}
16+
println!("sorted vec is {:?}", sorted_vec);
17+
}

chapter_7/merge_sort/Cargo.lock

+95
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chapter_7/merge_sort/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "merge_sort"
3+
version = "0.1.0"
4+
authors = ["Yohei Osawa <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
input = {path = "../../library/input"}
11+
num = "0.3.1"

chapter_7/merge_sort/src/main.rs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use num::Bounded;
2+
3+
fn merge<T: Copy + Bounded + Ord>(vec: &mut Vec<T>, left: usize, right: usize, mid: usize) {
4+
let max_value:T = Bounded::max_value();
5+
let mut vec_left = vec[left..mid].to_vec();
6+
let mut vec_right = vec[mid..right].to_vec();
7+
vec_left.push(max_value);
8+
vec_right.push(max_value);
9+
10+
let mut left_index = 0;
11+
let mut right_index = 0;
12+
for i in left..right{
13+
if vec_left[left_index] > vec_right[right_index] {
14+
vec[i] = vec_right[right_index];
15+
right_index += 1;
16+
} else {
17+
vec[i] = vec_left[left_index];
18+
left_index += 1;
19+
}
20+
}
21+
}
22+
23+
fn merge_sort<T: Copy + Bounded + Ord>(vec: &mut Vec<T>, left: usize, right: usize) {
24+
if left + 1 == right {
25+
return;
26+
}
27+
let mid = (left + right) / 2;
28+
merge_sort(vec, left, mid);
29+
merge_sort(vec, mid, right);
30+
merge(vec, left, right, mid);
31+
}
32+
33+
fn main() {
34+
let mut input_vec: Vec<i32> = input::read_numline::<i32>();
35+
let len = input_vec.len();
36+
merge_sort(& mut input_vec, 0, len);
37+
println!("sorted input is {:?}", input_vec);
38+
}

chapter_7/partition/Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chapter_7/partition/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "partition"
3+
version = "0.1.0"
4+
authors = ["Yohei Osawa <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
input = {path = "../../library/input"}

chapter_7/partition/src/main.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
fn main() {
2+
let patition_num: i32 = input::read_number();
3+
let mut vec: Vec<i32> = input::read_numline();
4+
vec.push(patition_num);
5+
let mut border = 0;
6+
for j in 0..vec.len() {
7+
if patition_num > vec[j] {
8+
vec.swap(border, j);
9+
border += 1;
10+
} else{
11+
// nothing
12+
}
13+
}
14+
let last_index = vec.len() -1;
15+
vec.swap(border, last_index);
16+
17+
println!("patitioned vec is {:?}", vec);
18+
}

chapter_7/quick_sort/Cargo.lock

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chapter_7/quick_sort/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "quick_sort"
3+
version = "0.1.0"
4+
authors = ["Yohei Osawa <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
input = {path = "../../library/input"}

chapter_7/quick_sort/src/main.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
fn make_partition(vec: &mut Vec<i32>, left: usize, right: usize) -> usize {
2+
let patition_num = vec[right -1];
3+
let mut border = left;
4+
for j in left..right {
5+
if patition_num > vec[j] {
6+
vec.swap(border, j);
7+
border += 1;
8+
} else{
9+
// do nothing
10+
}
11+
}
12+
vec.swap(border, right -1);
13+
border
14+
}
15+
16+
fn quick_sort(vec: &mut Vec<i32>, left: usize, right: usize) {
17+
if left + 1 >= right {
18+
return;
19+
}
20+
let border = make_partition(vec, left, right);
21+
quick_sort(vec, left, border);
22+
quick_sort(vec, border + 1, right);
23+
}
24+
25+
fn main() {
26+
let mut vec: Vec<i32> = input::read_numline();
27+
let vec_len = vec.len();
28+
quick_sort(&mut vec, 0, vec_len);
29+
println!("sort result is {:?}", vec);
30+
}

0 commit comments

Comments
 (0)