Skip to content

Commit

Permalink
sort: Add benchmark for selection sort
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Jun 29, 2024
1 parent 59e4269 commit 81940bc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
4 changes: 4 additions & 0 deletions sort/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ criterion = "0.5.1"

[[bench]]
name = "bubble_sort"
harness = false

[[bench]]
name = "selection_sort"
harness = false
29 changes: 29 additions & 0 deletions sort/benches/selection_sort.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved.
// Use of this source is governed by General Public License that can be found
// in the LICENSE file.

use criterion::{Criterion, criterion_group, criterion_main};

use sort::selection_sort;
use sort::util::random_ints;

fn criterion_benchmark(c: &mut Criterion) {
for exp in 1..5 {
let len: usize = 2 * 10_usize.pow(exp);
let arr = random_ints(len).expect("Failed to generate random integers");
let title1 = format!("std_sort {len}");
let title2 = format!("selection_sort {len}");

c.bench_function(&title1, |b| b.iter(|| {
let mut arr1 = arr.clone();
arr1.sort();
}));
c.bench_function(&title2, |b| b.iter(|| {
let mut arr2 = arr.clone();
selection_sort(&mut arr2);
}));
}
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
29 changes: 5 additions & 24 deletions sort/src/selection_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,25 @@
// Use of this source is governed by General Public License that can be
// found in the LICENSE file.

/// Selection sort works as follows:
/// - Find the smallest element in the array, and exchange it with the first
/// element in the array.
/// - Find the second smallest element in the array, and exchange it with the
/// element in the second position.
/// - Continue in this way until the entire array is sorted.
///
/// It takes about as long to run selection sort for a file that is already
/// in order, or for a file with all keys equal, as it does for a randomly
/// ordered file.
///
/// It is the method of choice for sorting files with huge items and small keys.
/// For such applications, the cost of moving the data dominates the cost of
/// making comparisons, and no algorithms can sort a file with substantially less
/// data movement than selection sort.
///
/// 即使输入数据已经是排好序的, 该算法依然需要 N^2 次的操作.
/// N^2 / 2 次比较以及 N 次交换.
///
/// 平均是 O(NlogN)
///
/// 这种算法, 每个元素最多只移动一次, 也就是说, 这个算法比较适合那种比较元素时的
/// 成本低, 但移动元素成本比较高的情况.
pub fn selection_sort<T>(list: &mut [T])
where
T: PartialOrd,
{
let len = list.len();
if list.len() < 2 {
return;
}
let len = list.len();
for i in 0..(len - 1) {

// 找到最小元素的索引
let mut min_index = i;
for j in (i + 1)..len {
if list[j] < list[min_index] {
min_index = j;
}
}

// 如果最小元素不是 `list[i]`, 就交换两个元素
if i != min_index {
list.swap(i, min_index);
}
Expand Down

0 comments on commit 81940bc

Please sign in to comment.