Skip to content

Commit be8a632

Browse files
authored
bugfix: fixed infinite loop bug on bubble sort (#219)
## Pull Request type Please check the type of change your PR introduces: - [X] Bugfix - [ ] Feature - [ ] Code style update (formatting, renaming) - [ ] Refactoring (no functional changes, no API changes) - [ ] Build-related changes - [ ] Documentation content changes - [ ] Other (please describe): ## What is the current behavior? Issue Number: #217 ## What is the new behavior? - Bubble sort don't infinite loop anymore when there is equal values ## Does this introduce a breaking change? - [ ] Yes - [X] No ## Other information Also added a test to ensure the problem doesn't happen again
1 parent 01a7690 commit be8a632

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/sorting/src/bubble_sort.cairo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn bubble_sort_elements<T, +Copy<T>, +Drop<T>, +PartialOrd<T>>(mut array: Array<
2626
idx2 = 1;
2727
sorted_iteration = 0;
2828
} else {
29-
if *array[idx1] < *array[idx2] {
29+
if *array[idx1] <= *array[idx2] {
3030
sorted_array.append(*array[idx1]);
3131
idx1 = idx2;
3232
idx2 += 1;

src/sorting/src/tests/bubble_sort_test.cairo

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,14 @@ fn bubblesort_test_pre_sorted() {
4444

4545
assert(is_equal(sorted.span(), correct.span()), 'invalid result');
4646
}
47+
48+
#[test]
49+
#[available_gas(2000000)]
50+
fn bubblesort_test_2_same_values() {
51+
let mut data = array![1_u32, 2_u32, 2_u32, 4_u32];
52+
let mut correct = array![1_u32, 2_u32, 2_u32, 4_u32];
53+
54+
let sorted = bubble_sort::bubble_sort_elements(data);
55+
56+
assert(is_equal(sorted.span(), correct.span()), 'invalid result');
57+
}

0 commit comments

Comments
 (0)