Skip to content

Commit bec222f

Browse files
committed
feat: head sort
1 parent 7be14be commit bec222f

File tree

3 files changed

+79
-34
lines changed

3 files changed

+79
-34
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,19 @@ insert take time PT5.840068629S
6363
```
6464

6565
插入排序没有特别有用的优化方式,因此并没有记录研究.
66+
67+
## 堆排序
68+
69+
> 堆排序可以分为两个阶段.1:在堆的构造阶段,我们将原始数数组构造成一个大堆.2:从对顶取出最大值与堆的最后一个值进行交换,再调整堆.堆排序主要工作都是在第二阶段.
70+
71+
**空间复杂度: O(nlgn); 时间复杂度: O(1); 不稳定**
72+
73+
在堆的调整阶段使用了两种方式:
74+
75+
- 递归调整
76+
- 循环调整
77+
78+
```
79+
head take time PT0.036101004S
80+
head take time PT0.036012785S
81+
```

src/head.rs

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,89 @@
11
use time::PreciseTime;
22
use array::swap;
33

4-
fn adjust(val: &mut Vec<i32>, i: usize) {
5-
let mut index = i;
6-
let mut parent_index = (index - 1) / 2;
7-
println!("i: {}, parent_index: {}", i, parent_index);
4+
fn adjust(val: &mut Vec<i32>, i: usize, len: usize) {
5+
let left_index = i * 2 + 1;
6+
let right_index = i * 2 + 2;
7+
let mut big_index = i;
88

9-
while parent_index >= 0 {
9+
if left_index < len && val[left_index] > val[big_index] {
10+
big_index = left_index;
11+
}
12+
if right_index < len && val[right_index] > val[big_index] {
13+
big_index = right_index;
14+
}
15+
if big_index != i {
16+
swap(val, big_index, i);
17+
adjust(val, big_index, len);
18+
}
19+
}
20+
21+
fn adjust1(val: &mut Vec<i32>, i: usize, len: usize) {
22+
let mut parent_index = i;
23+
24+
while parent_index * 2 <= len {
1025
let left_index = parent_index * 2 + 1;
1126
let right_index = parent_index * 2 + 2;
12-
println!("left: {}, right: {}, parent: {}", left_index, right_index, parent_index);
13-
14-
if right_index >= val.len() {
15-
// only left child
16-
if val[left_index] > val[parent_index] {
17-
println!("left: {}, parent: {}", left_index, parent_index);
18-
swap(val, left_index, parent_index);
19-
}
20-
} else {
21-
let mut result_index = left_index;
22-
if val[left_index] < val[right_index] {
23-
result_index = right_index;
24-
}
25-
if val[result_index] > val[parent_index] {
26-
println!("result: {}, parent: {}", result_index, parent_index);
27-
swap(val, result_index, parent_index);
28-
}
29-
}
27+
let mut big_index = parent_index;
3028

31-
if parent_index == 0 {
29+
if left_index < len && val[left_index] > val[big_index] {
30+
big_index = left_index;
31+
}
32+
if right_index < len && val[right_index] > val[big_index] {
33+
big_index = right_index;
34+
}
35+
if big_index != parent_index {
36+
swap(val,big_index, parent_index);
37+
parent_index = big_index;
38+
} else {
3239
break;
3340
}
34-
35-
index = left_index - 2;
36-
parent_index = (index - 1) / 2;
3741
}
3842
}
3943

4044
// 堆
4145
fn head(val: &Vec<i32>) -> Vec<i32> {
42-
let mut value = vec![5, 6, 5, 9, 8, 9, 6, 5, 1, 7];
46+
let mut value = val.clone();
4347
let len = value.len();
44-
println!("{:?}", value);
4548

46-
for i in (len / 2..len).rev() {
47-
adjust(&mut value, i);
49+
for i in (0..len / 2).rev() {
50+
adjust(&mut value, i, len);
51+
}
52+
53+
for i in (0..len).rev() {
54+
swap(&mut value, i, 0);
55+
adjust1(&mut value, 0, i);
56+
}
57+
value
58+
}
59+
60+
fn head1(val: &Vec<i32>) -> Vec<i32> {
61+
let mut value = val.clone();
62+
let len = value.len();
63+
64+
for i in (0..len / 2).rev() {
65+
adjust1(&mut value, i, len);
66+
}
67+
68+
for i in (0..len).rev() {
69+
swap(&mut value, i, 0);
70+
adjust1(&mut value, 0, i);
4871
}
49-
println!("{:?}", value);
5072
value
5173
}
5274

5375
pub fn test(array_value: &Vec<i32>) {
5476
println!("Start test head sort");
5577

5678
let start_head = PreciseTime::now();
57-
head(&array_value);
79+
let a = head(&array_value);
80+
let end_head = PreciseTime::now();
81+
println!("head take time {}", start_head.to(end_head));
82+
83+
let start_head = PreciseTime::now();
84+
let b = head1(&array_value);
5885
let end_head = PreciseTime::now();
5986
println!("head take time {}", start_head.to(end_head));
87+
88+
println!("a == b ? {}", a == b);
6089
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn main() {
1818
"bubble" => bubble::test(&array_value),
1919
"select" => select::test(&array_value),
2020
"insert" => insert::test(&array_value),
21-
// "head" => head::test(&array_value),
21+
"head" => head::test(&array_value),
2222
_ => println!("please input sort name")
2323
}
2424
}

0 commit comments

Comments
 (0)