forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Patience Sort (TheAlgorithms#439)
- Loading branch information
Showing
13 changed files
with
120 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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,82 @@ | ||
use std::vec; | ||
|
||
pub fn patience_sort<T: Ord + Copy>(arr: &mut [T]) { | ||
if arr.is_empty() { | ||
return; | ||
} | ||
|
||
// collect piles from arr | ||
let mut piles: Vec<Vec<T>> = Vec::new(); | ||
for &card in arr.iter() { | ||
let mut left = 0usize; | ||
let mut right = piles.len(); | ||
|
||
while left < right { | ||
let mid = left + (right - left) / 2; | ||
if piles[mid][piles[mid].len() - 1] >= card { | ||
right = mid; | ||
} else { | ||
left = mid + 1; | ||
} | ||
} | ||
|
||
if left == piles.len() { | ||
piles.push(vec![card]); | ||
} else { | ||
piles[left].push(card); | ||
} | ||
} | ||
|
||
// merge the piles | ||
let mut idx = 0usize; | ||
while let Some((min_id, pile)) = piles | ||
.iter() | ||
.enumerate() | ||
.min_by_key(|(_, pile)| *pile.last().unwrap()) | ||
{ | ||
arr[idx] = *pile.last().unwrap(); | ||
idx += 1; | ||
piles[min_id].pop(); | ||
|
||
if piles[min_id].is_empty() { | ||
_ = piles.remove(min_id); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::sorting::is_sorted; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn basic() { | ||
let mut array = vec![ | ||
-2, 7, 15, -14, 0, 15, 0, 100_33, 7, -7, -4, -13, 5, 8, -14, 12, | ||
]; | ||
patience_sort(&mut array); | ||
assert!(is_sorted(&array)); | ||
} | ||
|
||
#[test] | ||
fn empty() { | ||
let mut array = Vec::<i32>::new(); | ||
patience_sort(&mut array); | ||
assert!(is_sorted(&array)); | ||
} | ||
|
||
#[test] | ||
fn one_element() { | ||
let mut array = vec![3]; | ||
patience_sort(&mut array); | ||
assert!(is_sorted(&array)); | ||
} | ||
|
||
#[test] | ||
fn pre_sorted() { | ||
let mut array = vec![-123_456, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; | ||
patience_sort(&mut array); | ||
assert!(is_sorted(&array)); | ||
} | ||
} |
This file contains 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 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 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 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