clean up vec stuff
This commit is contained in:
parent
cc822b9f04
commit
f0ff9303bd
5 changed files with 20 additions and 62 deletions
|
@ -108,18 +108,13 @@ impl Sort for DualPivotQuickSort {
|
|||
println!("{:?}", list);
|
||||
}
|
||||
|
||||
let mut vec = list.clone();
|
||||
vec.drain(lp..);
|
||||
self.sort_mut(&mut vec);
|
||||
if lp != 0 {
|
||||
self.sort_mut(&mut list[0..=(lp - 1)].to_vec());
|
||||
}
|
||||
|
||||
let mut vec = list.clone();
|
||||
vec.drain(rp..);
|
||||
vec.drain(..=lp);
|
||||
self.sort_mut(&mut vec);
|
||||
self.sort_mut(&mut list[(lp + 1)..=(rp - 1)].to_vec());
|
||||
|
||||
let mut vec = list.clone();
|
||||
vec.drain(..=rp);
|
||||
self.sort_mut(&mut vec);
|
||||
self.sort_mut(&mut list[(rp + 1)..].to_vec());
|
||||
}
|
||||
|
||||
fn num_comp(&self) -> u64 {
|
||||
|
|
|
@ -48,17 +48,9 @@ impl Sort for HybridSort {
|
|||
println!("</hybrid_quick>");
|
||||
}
|
||||
|
||||
let mut vec = Vec::new();
|
||||
for i in 0..pivot as usize {
|
||||
vec.push(list[i]);
|
||||
}
|
||||
self.sort_mut(&mut vec);
|
||||
self.sort_mut(&mut list[..pivot as usize].to_vec());
|
||||
|
||||
vec.clear();
|
||||
for i in (pivot as usize + 1)..list.len() {
|
||||
vec.push(list[i]);
|
||||
}
|
||||
self.sort_mut(&mut vec);
|
||||
self.sort_mut(&mut list[pivot as usize + 1..].to_vec());
|
||||
}
|
||||
|
||||
fn num_comp(&self) -> u64 {
|
||||
|
|
|
@ -65,15 +65,14 @@ impl Sort for MergeSort {
|
|||
if list.len() > 1 {
|
||||
let size = list.len() / 2;
|
||||
|
||||
let mut vec = list.clone();
|
||||
vec.drain(size..);
|
||||
let mut vec = list[..size].to_vec();
|
||||
self.sort_mut(&mut vec);
|
||||
let left = vec;
|
||||
|
||||
let mut vec = list.clone();
|
||||
vec.drain(..size);
|
||||
let mut vec = list[size..].to_vec();
|
||||
self.sort_mut(&mut vec);
|
||||
let right = vec;
|
||||
|
||||
let merged = self.merge(&left, &right);
|
||||
|
||||
*list = merged;
|
||||
|
|
|
@ -46,26 +46,16 @@ impl MySort {
|
|||
if mid <= e {
|
||||
self._mysort(list, e + 1, high, e + 1, s);
|
||||
|
||||
let mut left = list.clone();
|
||||
left.drain(e + 1..);
|
||||
left.drain(..low);
|
||||
let mut right = list.clone();
|
||||
right.drain(high + 1..);
|
||||
right.drain(..=e);
|
||||
list.copy_from_slice(&self.merge.merge(&left, &right));
|
||||
let list_clone = list.clone();
|
||||
list.copy_from_slice(&self.merge.merge(&list_clone[low..=e].to_vec(), &list_clone[(e + 1)..=high].to_vec()));
|
||||
|
||||
self.comparisons += self.merge.num_comp();
|
||||
self.swaps += self.merge.num_swap();
|
||||
} else if mid >= s {
|
||||
self._mysort(list, low, s - 1, e, s - 1);
|
||||
|
||||
let mut left = list.clone();
|
||||
left.drain(s..);
|
||||
left.drain(..low);
|
||||
let mut right = list.clone();
|
||||
right.drain(high + 1..);
|
||||
right.drain(..s);
|
||||
list.copy_from_slice(&self.merge.merge(&left, &right));
|
||||
let list_clone = list.clone();
|
||||
list.copy_from_slice(&self.merge.merge(&list_clone[low..=(s - 1)].to_vec(), &list_clone[s..=high].to_vec()));
|
||||
|
||||
self.comparisons += self.merge.num_comp();
|
||||
self.swaps += self.merge.num_swap();
|
||||
|
@ -80,13 +70,8 @@ impl MySort {
|
|||
self._mysort(list, low, i - 1, e, i - 1);
|
||||
self._mysort(list, i, high, j, s);
|
||||
|
||||
let mut left = list.clone();
|
||||
left.drain(i..);
|
||||
left.drain(..low);
|
||||
let mut right = list.clone();
|
||||
right.drain(high + 1..);
|
||||
right.drain(..i);
|
||||
list.copy_from_slice(&self.merge.merge(&left, &right));
|
||||
let list_clone = list.clone();
|
||||
list.copy_from_slice(&self.merge.merge(&list_clone[low..=(i - 1)].to_vec(), &list_clone[i..=high].to_vec()));
|
||||
|
||||
self.comparisons += self.merge.num_comp();
|
||||
self.swaps += self.merge.num_swap();
|
||||
|
@ -94,13 +79,8 @@ impl MySort {
|
|||
self._mysort(list, low, j, e, i);
|
||||
self._mysort(list, j + 1, high, j + 1, s);
|
||||
|
||||
let mut left = list.clone();
|
||||
left.drain(j + 1..);
|
||||
left.drain(..low);
|
||||
let mut right = list.clone();
|
||||
right.drain(high + 1..);
|
||||
right.drain(..=j);
|
||||
list.copy_from_slice(&self.merge.merge(&left, &right));
|
||||
let list_clone = list.clone();
|
||||
list.copy_from_slice(&self.merge.merge(&list_clone[low..=j].to_vec(), &list_clone[(j + 1)..=high].to_vec()));
|
||||
|
||||
self.comparisons += self.merge.num_comp();
|
||||
self.swaps += self.merge.num_swap();
|
||||
|
|
|
@ -63,17 +63,9 @@ impl Sort for QuickSort {
|
|||
println!("{:?}", list);
|
||||
}
|
||||
|
||||
let mut vec = Vec::new();
|
||||
for i in 0..pivot as usize {
|
||||
vec.push(list[i]);
|
||||
}
|
||||
self.sort_mut(&mut vec);
|
||||
self.sort_mut(&mut list[..pivot as usize].to_vec());
|
||||
|
||||
vec.clear();
|
||||
for i in (pivot as usize + 1)..list.len() {
|
||||
vec.push(list[i]);
|
||||
}
|
||||
self.sort_mut(&mut vec);
|
||||
self.sort_mut(&mut list[pivot as usize + 1..].to_vec());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue