From 815e213b44b583f8de99f442b8ca0f43924376cb Mon Sep 17 00:00:00 2001 From: sangge <2251250136@qq.com> Date: Mon, 7 Jul 2025 10:46:19 +0800 Subject: [PATCH] test: test selection --- src/bin/enc.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/bin/enc.rs b/src/bin/enc.rs index d88f21a..7f4991f 100644 --- a/src/bin/enc.rs +++ b/src/bin/enc.rs @@ -196,8 +196,20 @@ fn encrypted_knn_classify( } println!(); // New line after progress bar - println!("📊 Sorting {} distances...", distances.len()); - bitonic_sort_encrypted(&mut distances, true); + println!("📊 Finding {} smallest distances using selection...", k); + + // Use selection instead of full sorting to match plain version behavior + for target_pos in 0..k.min(distances.len()) { + // Find minimum in remaining elements [target_pos..] + for i in (target_pos + 1)..distances.len() { + let should_swap = distances[i].distance.lt(&distances[target_pos].distance); + + // Use split_at_mut to avoid borrowing issues + let (left, right) = distances.split_at_mut(i); + encrypted_conditional_swap(&mut left[target_pos], &mut right[0], &should_swap); + } + } + distances.truncate(k); distances.iter().map(|n| n.index.clone()).collect()