-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Adding smoothsort #808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Adding smoothsort #808
Changes from 2 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
8638d90
Create smooth_sort.rs
kanishkgits 9c4b24c
Update DIRECTORY.md
kanishkgits 0c50473
Update src/sorting/smooth_sort.rs
kanishkgits cbc2d2c
adding smooth sort to mod.rs
kanishkgits 52d2c3e
Merge branch 'master' of https://github.com/kanishkgits/Rust
kanishkgits 7801ad4
Update smooth_sort.rs
kanishkgits e026a98
Update mod.rs
kanishkgits 60a7cc1
Update smooth_sort.rs
kanishkgits e691798
Update smooth_sort.rs
kanishkgits 78fbc3e
Update smooth_sort.rs
kanishkgits 24fad3d
Update smooth_sort.rs
kanishkgits 0b680ff
Update smooth_sort.rs
kanishkgits 5717344
Update smooth_sort.rs
kanishkgits 22aef53
Update smooth_sort.rs
kanishkgits 5e50163
Update smooth_sort.rs
kanishkgits 1949559
Update smooth_sort.rs
kanishkgits 8902e41
Update smooth_sort.rs
kanishkgits 4c469de
Update smooth_sort.rs
kanishkgits e8dd564
Update smooth_sort.rs
kanishkgits 8ca5693
Update smooth_sort.rs
kanishkgits 58166f8
Update smooth_sort.rs
kanishkgits 004043b
Update smooth_sort.rs
kanishkgits 5bb0a8c
Update smooth_sort.rs
kanishkgits File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| //smooth sort | ||
|
|
||
|
|
||
| pub fn smooth_sort(nums: &mut Vec<i32>) { | ||
| let n = nums.len(); | ||
| if n <= 1 { | ||
| return; | ||
| } | ||
| let mut leonardo_heap_sizes = Vec::new(); | ||
| let mut num_of_heaps = 0; | ||
|
|
||
| for i in 0..n { | ||
| add_to_leonardo_heap(nums, i, &mut leonardo_heap_sizes, &mut num_of_heaps); | ||
| } | ||
| for i in (0..n).rev() { | ||
| remove_from_leonardo_heap(nums, i, &mut leonardo_heap_sizes, &mut num_of_heaps); | ||
| } | ||
| } | ||
|
|
||
| fn add_to_leonardo_heap(nums: &mut Vec<i32>, index: usize, sizes: &mut Vec<usize>, heaps: &mut usize) { | ||
| if *heaps >= 2 && sizes[*heaps - 2] == sizes[*heaps - 1] + 1 { | ||
| sizes[*heaps - 2] += 1; | ||
| sizes.pop(); | ||
| *heaps -= 1; | ||
| } else if *heaps >= 2 && sizes[*heaps - 1] == sizes[*heaps - 2] + 1 { | ||
| sizes[*heaps - 1] += 1; | ||
| } else { | ||
| sizes.push(1); | ||
| } | ||
| heapify_leonardo(nums, index, sizes, *heaps); | ||
| } | ||
|
|
||
| fn remove_from_leonardo_heap(nums: &mut Vec<i32>, index: usize, sizes: &mut Vec<usize>, heaps: &mut usize) { | ||
| let size = sizes.pop().unwrap(); | ||
| *heaps -= 1; | ||
| if size >= 2 { | ||
| sizes.push(size - 1); | ||
| sizes.push(size - 2); | ||
| *heaps += 2; | ||
| heapify_leonardo(nums, index - size + 1, sizes, *heaps - 2); | ||
| heapify_leonardo(nums, index - 1, sizes, *heaps - 1); | ||
| } | ||
| } | ||
|
|
||
| fn heapify_leonardo(nums: &mut Vec<i32>, index: usize, sizes: &Vec<usize>, heaps: usize) { | ||
| let mut current = index; | ||
| let mut heap_size = sizes[heaps]; | ||
|
|
||
| while heaps > 1 { | ||
| let left_child = current - heap_size; | ||
| let right_child = current - 1; | ||
|
|
||
| if nums[current] < nums[left_child] { | ||
| nums.swap(current, left_child); | ||
| current = left_child; | ||
| } else if nums[current] < nums[right_child] { | ||
| nums.swap(current, right_child); | ||
| current = right_child; | ||
| } else { | ||
| break; | ||
| } | ||
|
|
||
| heap_size -= 1; | ||
| } | ||
| } | ||
|
|
||
| //tests | ||
kanishkgits marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn smooth_sort_example_1() { | ||
| let mut arr = vec![3, 5, 2, 1, 6, 4]; | ||
| smooth_sort(&mut arr); | ||
| assert_eq!(arr, vec![1, 2, 3, 4, 5, 6]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn smooth_sort_example_2() { | ||
| let mut arr = vec![4, 1, 3, 5, 2]; | ||
| smooth_sort(&mut arr); | ||
| assert_eq!(arr, vec![1, 2, 3, 4, 5]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn smooth_sort_repeated_elements() { | ||
| let mut arr = vec![5, 5, 5, 5]; | ||
| smooth_sort(&mut arr); | ||
| assert_eq!(arr, vec![5, 5, 5, 5]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn smooth_sort_large_elements() { | ||
| let mut arr = vec![100, 200, 5, 10, 15]; | ||
| smooth_sort(&mut arr); | ||
| assert_eq!(arr, vec![5, 10, 15, 100, 200]); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not to use
nums.len()directly?