MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / insertion_sort

Function insertion_sort

publication/code/chapter07/tim_sort.rs:437–456  ·  view source on GitHub ↗
(list: &mut [i32])

Source from the content-addressed store, hash-verified

435}
436
437fn insertion_sort(list: &mut [i32]) {
438 unsafe {
439 let list_ptr = list.as_mut_ptr();
440 let len = list.len();
441 for i in 0..len {
442 let mut j = i;
443 let list_i = list_ptr.offset(i as isize);
444 while j > 0 && &*list_i < &*list_ptr.offset(j as isize -1) {
445 j -= 1;
446 }
447
448 if i != j {
449 let list_j = list_ptr.offset(j as isize);
450 let temp = ptr::read(list_i);
451 ptr::copy(list_j, list_j.offset(1), i - j);
452 ptr::write(list_j, temp);
453 }
454 }
455 }
456}
457
458// 计算 minrun 值,实际范围为 [32, 64]
459fn calc_minrun(len: usize) -> usize {

Callers 2

sortMethod · 0.70
tim_sortFunction · 0.70

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected