mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-10-31 18:37:48 +08:00 
			
		
		
		
	Bug fixes and improvements. (#1780)
* Fix the "尾递归优化" to "递归深度优化" in quick_sort. * Update landing pages. * Sync zh and zh-hant versions. * Sync zh and zh-hant versions.
This commit is contained in:
		| @ -6,18 +6,28 @@ | ||||
|  | ||||
| use hello_algo_rust::include::print_util; | ||||
|  | ||||
| use std::collections::BinaryHeap; | ||||
| use std::{cmp::Reverse, collections::BinaryHeap}; | ||||
|  | ||||
| fn test_push(heap: &mut BinaryHeap<i32>, val: i32, flag: i32) { | ||||
|     heap.push(flag * val); // 元素入堆積 | ||||
| fn test_push_max(heap: &mut BinaryHeap<i32>, val: i32) { | ||||
|     heap.push(val); // 元素入堆積 | ||||
|     println!("\n元素 {} 入堆積後", val); | ||||
|     print_util::print_heap(heap.iter().map(|&val| flag * val).collect()); | ||||
|     print_util::print_heap(heap.iter().map(|&val| val).collect()); | ||||
| } | ||||
| fn test_push_min(heap: &mut BinaryHeap<Reverse<i32>>, val: i32) { | ||||
|     heap.push(Reverse(val)); // 元素入堆積 | ||||
|     println!("\n元素 {} 入堆積後", val); | ||||
|     print_util::print_heap(heap.iter().map(|&val| val.0).collect()); | ||||
| } | ||||
|  | ||||
| fn test_pop(heap: &mut BinaryHeap<i32>, flag: i32) { | ||||
| fn test_pop_max(heap: &mut BinaryHeap<i32>) { | ||||
|     let val = heap.pop().unwrap(); | ||||
|     println!("\n堆積頂元素 {} 出堆積後", flag * val); | ||||
|     print_util::print_heap(heap.iter().map(|&val| flag * val).collect()); | ||||
|     println!("\n堆積頂元素 {} 出堆積後", val); | ||||
|     print_util::print_heap(heap.iter().map(|&val| val).collect()); | ||||
| } | ||||
| fn test_pop_min(heap: &mut BinaryHeap<Reverse<i32>>) { | ||||
|     let val = heap.pop().unwrap().0; | ||||
|     println!("\n堆積頂元素 {} 出堆積後", val); | ||||
|     print_util::print_heap(heap.iter().map(|&val| val.0).collect()); | ||||
| } | ||||
|  | ||||
| /* Driver Code */ | ||||
| @ -26,31 +36,29 @@ fn main() { | ||||
|     // 初始化小頂堆積 | ||||
|     #[allow(unused_assignments)] | ||||
|     let mut min_heap = BinaryHeap::new(); | ||||
|     // Rust 的 BinaryHeap 是大頂堆積,當入列時將元素值乘以 -1 將其反轉,當出列時將元素值乘以 -1 將其還原 | ||||
|     let min_heap_flag = -1; | ||||
|     // Rust 的 BinaryHeap 是大頂堆積,小頂堆積一般會“套上”Reverse | ||||
|     // 初始化大頂堆積 | ||||
|     let mut max_heap = BinaryHeap::new(); | ||||
|     let max_heap_flag = 1; | ||||
|  | ||||
|     println!("\n以下測試樣例為大頂堆積"); | ||||
|  | ||||
|     /* 元素入堆積 */ | ||||
|     test_push(&mut max_heap, 1, max_heap_flag); | ||||
|     test_push(&mut max_heap, 3, max_heap_flag); | ||||
|     test_push(&mut max_heap, 2, max_heap_flag); | ||||
|     test_push(&mut max_heap, 5, max_heap_flag); | ||||
|     test_push(&mut max_heap, 4, max_heap_flag); | ||||
|     test_push_max(&mut max_heap, 1); | ||||
|     test_push_max(&mut max_heap, 3); | ||||
|     test_push_max(&mut max_heap, 2); | ||||
|     test_push_max(&mut max_heap, 5); | ||||
|     test_push_max(&mut max_heap, 4); | ||||
|  | ||||
|     /* 獲取堆積頂元素 */ | ||||
|     let peek = max_heap.peek().unwrap() * max_heap_flag; | ||||
|     let peek = max_heap.peek().unwrap(); | ||||
|     println!("\n堆積頂元素為 {}", peek); | ||||
|  | ||||
|     /* 堆積頂元素出堆積 */ | ||||
|     test_pop(&mut max_heap, max_heap_flag); | ||||
|     test_pop(&mut max_heap, max_heap_flag); | ||||
|     test_pop(&mut max_heap, max_heap_flag); | ||||
|     test_pop(&mut max_heap, max_heap_flag); | ||||
|     test_pop(&mut max_heap, max_heap_flag); | ||||
|     test_pop_max(&mut max_heap); | ||||
|     test_pop_max(&mut max_heap); | ||||
|     test_pop_max(&mut max_heap); | ||||
|     test_pop_max(&mut max_heap); | ||||
|     test_pop_max(&mut max_heap); | ||||
|  | ||||
|     /* 獲取堆積大小 */ | ||||
|     let size = max_heap.len(); | ||||
| @ -65,9 +73,9 @@ fn main() { | ||||
|     min_heap = BinaryHeap::from( | ||||
|         vec![1, 3, 2, 5, 4] | ||||
|             .into_iter() | ||||
|             .map(|val| min_heap_flag * val) | ||||
|             .collect::<Vec<i32>>(), | ||||
|             .map(|val| Reverse(val)) | ||||
|             .collect::<Vec<Reverse<i32>>>(), | ||||
|     ); | ||||
|     println!("\n輸入串列並建立小頂堆積後"); | ||||
|     print_util::print_heap(min_heap.iter().map(|&val| min_heap_flag * val).collect()); | ||||
|     print_util::print_heap(min_heap.iter().map(|&val| val.0).collect()); | ||||
| } | ||||
|  | ||||
| @ -90,7 +90,7 @@ impl QuickSortMedian { | ||||
|     } | ||||
| } | ||||
|  | ||||
| /* 快速排序(尾遞迴最佳化) */ | ||||
| /* 快速排序(遞迴深度最佳化) */ | ||||
| struct QuickSortTailCall; | ||||
|  | ||||
| impl QuickSortTailCall { | ||||
| @ -111,7 +111,7 @@ impl QuickSortTailCall { | ||||
|         i // 返回基準數的索引 | ||||
|     } | ||||
|  | ||||
|     /* 快速排序(尾遞迴最佳化) */ | ||||
|     /* 快速排序(遞迴深度最佳化) */ | ||||
|     pub fn quick_sort(mut left: i32, mut right: i32, nums: &mut [i32]) { | ||||
|         // 子陣列長度為 1 時終止 | ||||
|         while left < right { | ||||
| @ -141,8 +141,8 @@ fn main() { | ||||
|     QuickSortMedian::quick_sort(0, (nums.len() - 1) as i32, &mut nums); | ||||
|     println!("快速排序(中位基準數最佳化)完成後 nums = {:?}", nums); | ||||
|  | ||||
|     /* 快速排序(尾遞迴最佳化) */ | ||||
|     /* 快速排序(遞迴深度最佳化) */ | ||||
|     let mut nums = [2, 4, 1, 0, 3, 5]; | ||||
|     QuickSortTailCall::quick_sort(0, (nums.len() - 1) as i32, &mut nums); | ||||
|     println!("快速排序(尾遞迴最佳化)完成後 nums = {:?}", nums); | ||||
|     println!("快速排序(遞迴深度最佳化)完成後 nums = {:?}", nums); | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Yudong Jin
					Yudong Jin