Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 08f2df0

Browse files
authored
Merge pull request #282 from ben1009/148
feat: 148, add quicksort
2 parents 10b4f39 + 046798f commit 08f2df0

File tree

1 file changed

+69
-4
lines changed

1 file changed

+69
-4
lines changed

src/problem/p0148_sort_list.rs

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,43 @@ use crate::util::linked_list::ListNode;
4343
pub struct Solution;
4444

4545
impl Solution {
46+
// need randomize pivot, optimize for == povit, three way quick sort,
47+
// e.g. [1,1,1,,1,1,4,4,4,6,5,7,8,9], bla, bla
48+
pub fn sort_list_with_quick_sort(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
49+
if head.is_none() || head.as_ref().unwrap().next.is_none() {
50+
return head;
51+
}
52+
53+
let povit = head.as_ref().unwrap().val;
54+
let mut less = None;
55+
let mut great = None;
56+
let mut current = head;
57+
while let Some(mut n) = current {
58+
current = n.next.take();
59+
if n.val < povit {
60+
n.next = less;
61+
less = Some(n);
62+
} else {
63+
n.next = great;
64+
great = Some(n);
65+
}
66+
}
67+
68+
let less = Solution::sort_list_with_quick_sort(less);
69+
let great = Solution::sort_list_with_quick_sort(great);
70+
71+
let mut less_head_pointer = ListNode::new(0);
72+
less_head_pointer.next = less;
73+
let mut less_tail = &mut less_head_pointer;
74+
while let Some(ref mut n) = less_tail.next {
75+
less_tail = n;
76+
}
77+
78+
less_tail.next = great;
79+
80+
less_head_pointer.next
81+
}
82+
4683
pub fn sort_list_with_vec(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
4784
if head.is_none() || head.as_ref().unwrap().next.is_none() {
4885
return head;
@@ -189,6 +226,21 @@ mod tests {
189226

190227
#[test]
191228
fn test_148() {
229+
// sort_list_with_quick_sort
230+
assert_eq!(
231+
Solution::sort_list_with_quick_sort(linked_list::to_list(vec![4, 2, 1, 3])),
232+
linked_list::to_list(vec![1, 2, 3, 4])
233+
);
234+
assert_eq!(
235+
Solution::sort_list_with_quick_sort(linked_list::to_list(vec![-1, 5, 3, 4, 0])),
236+
linked_list::to_list(vec![-1, 0, 3, 4, 5])
237+
);
238+
assert_eq!(
239+
Solution::sort_list_with_quick_sort(linked_list::to_list(vec![])),
240+
linked_list::to_list(vec![])
241+
);
242+
243+
// sort_list_half_cut
192244
assert_eq!(
193245
Solution::sort_list_half_cut(linked_list::to_list(vec![4, 2, 1, 3])),
194246
linked_list::to_list(vec![1, 2, 3, 4])
@@ -202,6 +254,7 @@ mod tests {
202254
linked_list::to_list(vec![])
203255
);
204256

257+
// sort_list_half_cut_len
205258
assert_eq!(
206259
Solution::sort_list_half_cut_len(linked_list::to_list(vec![4, 2, 1, 3])),
207260
linked_list::to_list(vec![1, 2, 3, 4])
@@ -215,6 +268,7 @@ mod tests {
215268
linked_list::to_list(vec![])
216269
);
217270

271+
// sort_list_with_vec
218272
assert_eq!(
219273
Solution::sort_list_with_vec(linked_list::to_list(vec![4, 2, 1, 3])),
220274
linked_list::to_list(vec![1, 2, 3, 4])
@@ -232,10 +286,21 @@ mod tests {
232286
extern crate test;
233287
use test::{Bencher, black_box};
234288

235-
#[rustfmt::skip]
236-
// test problem::p0148_sort_list::tests::bench_sort_list_half_cut ... bench: 7,633.79 ns/iter (+/- 207.15)
237-
// test problem::p0148_sort_list::tests::bench_sort_list_half_cut_len ... bench: 1,823.08 ns/iter (+/- 40.39)
238-
// test problem::p0148_sort_list::tests::bench_sort_list_with_vec ... bench: 1,034.86 ns/iter (+/- 30.86)
289+
#[rustfmt::skip]
290+
// test problem::p0148_sort_list::tests::bench_sort_list_half_cut ... bench: 8,389.08 ns/iter (+/- 243.06)
291+
// test problem::p0148_sort_list::tests::bench_sort_list_half_cut_len ... bench: 1,913.11 ns/iter (+/- 57.90)
292+
// test problem::p0148_sort_list::tests::bench_sort_list_with_quick ... bench: 16,013.85 ns/iter (+/- 116.08)
293+
// test problem::p0148_sort_list::tests::bench_sort_list_with_vec ... bench: 1,142.24 ns/iter (+/- 35.95)
294+
295+
#[bench]
296+
fn bench_sort_list_with_quick(b: &mut Bencher) {
297+
b.iter(|| {
298+
black_box(Solution::sort_list_with_quick_sort(linked_list::to_list(
299+
(1..=100).rev().collect(),
300+
)))
301+
});
302+
}
303+
239304
#[bench]
240305
fn bench_sort_list_half_cut(b: &mut Bencher) {
241306
b.iter(|| {

0 commit comments

Comments
 (0)