fix normal select for different ks and update plots

This commit is contained in:
jacekpoz 2024-05-09 11:44:10 +02:00
parent c9713418b3
commit 5e80cb7736
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
7 changed files with 10 additions and 10 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -28,7 +28,7 @@ impl NormalSelect {
pub fn partition(&mut self, list: &mut Vec<u64>, lo: usize, hi: usize, pivot: u64) -> usize { pub fn partition(&mut self, list: &mut Vec<u64>, lo: usize, hi: usize, pivot: u64) -> usize {
use CompareResult::*; use CompareResult::*;
let mut pivot_index = 0; let mut pivot_index = lo;
for i in lo..=hi { for i in lo..=hi {
if self.compare(pivot, list[i]) == EQUAL { if self.compare(pivot, list[i]) == EQUAL {
@ -73,12 +73,12 @@ impl NormalSelect {
if self.should_print { println!("list: {:?}; lo: {lo}; hi: {hi}; ord_stat: {ord_stat}; k: {k}", list); } if self.should_print { println!("list: {:?}; lo: {lo}; hi: {hi}; ord_stat: {ord_stat}; k: {k}", list); }
let n = hi - lo + 1; let n = hi - lo + 1;
let num_groups = if n % 5 == 0 { n / 5 } else { (n / 5) + 1 }; let num_groups = if n % k == 0 { n / k } else { (n / k) + 1 };
let mut medians: Vec<u64> = vec![0; num_groups]; let mut medians: Vec<u64> = vec![0; num_groups];
let mut index = 0; let mut index = 0;
for i in (lo..=hi).step_by(5) { for i in (lo..=hi).step_by(k) {
if i + 4 <= hi { if i + (k - 1) <= hi {
self.insertion_sort(list, i, i + 4); self.insertion_sort(list, i, i + (k - 1));
medians[index] = list[i + 2]; medians[index] = list[i + 2];
index += 1; index += 1;
} else { } else {
@ -94,18 +94,18 @@ impl NormalSelect {
println!("pivot_index: {pivot_index}; pivot: {pivot}"); println!("pivot_index: {pivot_index}; pivot: {pivot}");
} }
let r = self.partition(list, lo, hi, pivot); let r = self.partition(list, lo, hi, pivot);
let k = r - lo + 1; let i = r - lo + 1;
if self.should_print { if self.should_print {
println!("r: {r}"); println!("r: {r}");
println!("k: {k}"); println!("i: {i}");
} }
if k == ord_stat { if i == ord_stat {
if self.should_print { println!("k == ord_stat"); } if self.should_print { println!("k == ord_stat"); }
return (r, list[r]); return (r, list[r]);
} else if k < ord_stat { } else if i < ord_stat {
if self.should_print { println!("k < ord_stat"); } if self.should_print { println!("k < ord_stat"); }
return self._select(list, r + 1, hi, ord_stat - k, k); return self._select(list, r + 1, hi, ord_stat - i, k);
} else { } else {
if self.should_print { println!("k > ord_stat"); } if self.should_print { println!("k > ord_stat"); }
return self._select(list, lo, r - 1, ord_stat, k); return self._select(list, lo, r - 1, ord_stat, k);