diff --git a/libsort/src/dual_pivot_quick_sort.rs b/libsort/src/dual_pivot_quick_sort.rs index 4d0b7f6..0625739 100644 --- a/libsort/src/dual_pivot_quick_sort.rs +++ b/libsort/src/dual_pivot_quick_sort.rs @@ -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 { diff --git a/libsort/src/hybrid_sort.rs b/libsort/src/hybrid_sort.rs index 0f7940c..e815bf5 100644 --- a/libsort/src/hybrid_sort.rs +++ b/libsort/src/hybrid_sort.rs @@ -48,17 +48,9 @@ impl Sort for HybridSort { println!(""); } - 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 { diff --git a/libsort/src/merge_sort.rs b/libsort/src/merge_sort.rs index 74c65d9..f5edba3 100644 --- a/libsort/src/merge_sort.rs +++ b/libsort/src/merge_sort.rs @@ -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; diff --git a/libsort/src/my_sort.rs b/libsort/src/my_sort.rs index d8de98b..8427f03 100644 --- a/libsort/src/my_sort.rs +++ b/libsort/src/my_sort.rs @@ -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(); diff --git a/libsort/src/quick_sort.rs b/libsort/src/quick_sort.rs index 6bbb68a..beb558c 100644 --- a/libsort/src/quick_sort.rs +++ b/libsort/src/quick_sort.rs @@ -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()); } }