diff --git a/src/stdlib/src/sort.rs b/src/stdlib/src/sort.rs index 5729bd511d285300c8461a1e0c1344eb3945997c..bbcd9fb1417c3c2c3c840aab993ff0b30ca86921 100644 --- a/src/stdlib/src/sort.rs +++ b/src/stdlib/src/sort.rs @@ -34,18 +34,7 @@ fn introsort_helper( const THRESHOLD: size_t = 8; if nel < THRESHOLD { - // use an insertion sort instead of an introsort on small arrays - for i in 0..nel { - for j in (0..i).rev() { - let current = unsafe { base.add(j * width) }; - let prev = unsafe { base.add((j + 1) * width) }; - if comp(current as *const c_void, prev as *const c_void) > 0 { - swap(current, prev, width); - } else { - break; - } - } - } + insertion_sort(base, nel, width, comp); } else if nel > 1 { if maxdepth == 0 { heapsort(base, nel, width, comp); @@ -58,6 +47,25 @@ fn introsort_helper( } } +fn insertion_sort( + base: *mut c_char, + nel: size_t, + width: size_t, + comp: extern "C" fn(*const c_void, *const c_void) -> c_int, +) { + for i in 0..nel { + for j in (0..i).rev() { + let current = unsafe { base.add(j * width) }; + let prev = unsafe { base.add((j + 1) * width) }; + if comp(current as *const c_void, prev as *const c_void) > 0 { + swap(current, prev, width); + } else { + break; + } + } + } +} + fn heapsort( base: *mut c_char, nel: size_t,