Skip to content

Commit

Permalink
refactor: rename function in sort (#536)
Browse files Browse the repository at this point in the history
bubble sort is actually insertion sort
  • Loading branch information
qazxcdswe123 committed Jun 11, 2024
1 parent 4f51a40 commit 3fb7b0a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
8 changes: 5 additions & 3 deletions array/fixedarray_sort.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn timsort[T : Compare](arr : FixedArraySlice[T]) -> Unit {
// Short arrays get sorted in-place via insertion sort to avoid allocations.
let len = arr.length()
if len <= max_insertion {
insertion_sort(arr)
FixedArraySlice::insertion_sort(arr)
}
let mut end = 0
let mut start = 0
Expand Down Expand Up @@ -79,7 +79,9 @@ fn timsort[T : Compare](arr : FixedArraySlice[T]) -> Unit {
}
}

fn insertion_sort[T : Compare](arr : FixedArraySlice[T]) -> Unit {
fn FixedArraySlice::insertion_sort[T : Compare](
arr : FixedArraySlice[T]
) -> Unit {
for i = 1, len = arr.length(); i < len; i = i + 1 {
for j = i; j > 0 && arr[j] < arr[j - 1]; j = j - 1 {
arr.swap(j, j - 1)
Expand Down Expand Up @@ -158,7 +160,7 @@ fn provide_sorted_batch[T : Compare](
// the sorted region to the left, so we push up MIN_INSERTION_RUN - 1 to the right. Which is
// more efficient that trying to push those already sorted elements to the left.
let sort_end = minimum(len, start + min_insertion_run)
insertion_sort(arr.slice(start, sort_end))
FixedArraySlice::insertion_sort(arr.slice(start, sort_end))
sort_end
} else {
end
Expand Down
14 changes: 5 additions & 9 deletions array/sort.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ fn quick_sort[T : Compare](arr : ArrayView[T], pred : T?, limit : Int) -> Unit {
let mut pred = pred
let mut was_partitioned = true
let mut balanced = true
let bubble_sort_len = 16
let insertion_sort_len = 16
while true {
let len = arr.length()
if len <= bubble_sort_len {
if len <= insertion_sort_len {
if len >= 2 {
bubble_sort(arr)
ArrayView::insertion_sort(arr)
}
return
}
Expand Down Expand Up @@ -129,12 +129,8 @@ fn try_bubble_sort[T : Compare](
true
}

/// Try to sort the array with bubble sort.
///
/// It will only tolerate at most 8 unsorted elements. The time complexity is O(n).
///
/// Returns whether the array is sorted.
fn bubble_sort[T : Compare](arr : ArrayView[T]) -> Unit {
/// Used when the array is small enough (<=16) to avoid recursion overhead.
fn ArrayView::insertion_sort[T : Compare](arr : ArrayView[T]) -> Unit {
for i = 1; i < arr.length(); i = i + 1 {
for j = i; j > 0 && arr[j - 1] > arr[j]; j = j - 1 {
arr.swap(j, j - 1)
Expand Down
8 changes: 6 additions & 2 deletions array/sort_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ test "heap_sort" {
test_sort(fn(arr) { heap_sort(arr.op_as_view(start=0, end=arr.length())) })?
}

test "bubble_sort" {
test_sort(fn(arr) { bubble_sort(arr.op_as_view(start=0, end=arr.length())) })?
test "insertion_sort" {
test_sort(
fn(arr) {
ArrayView::insertion_sort(arr.op_as_view(start=0, end=arr.length()))
},
)?
}

test "sort" {
Expand Down

0 comments on commit 3fb7b0a

Please sign in to comment.