Skip to content
Snippets Groups Projects
Unverified Commit b15aa83a authored by Alex Lyon's avatar Alex Lyon
Browse files

stdlib: move insertion sort from introsort() into a separate function

parent aa21e5fc
No related branches found
No related tags found
No related merge requests found
......@@ -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,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment