Skip to content

Commit

Permalink
bugfix: fixed infinite loop bug on bubble sort (#219)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
azurwastaken authored Nov 23, 2023
1 parent 01a7690 commit be8a632
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/sorting/src/bubble_sort.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn bubble_sort_elements<T, +Copy<T>, +Drop<T>, +PartialOrd<T>>(mut array: Array<
idx2 = 1;
sorted_iteration = 0;
} else {
if *array[idx1] < *array[idx2] {
if *array[idx1] <= *array[idx2] {
sorted_array.append(*array[idx1]);
idx1 = idx2;
idx2 += 1;
Expand Down
11 changes: 11 additions & 0 deletions src/sorting/src/tests/bubble_sort_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,14 @@ fn bubblesort_test_pre_sorted() {

assert(is_equal(sorted.span(), correct.span()), 'invalid result');
}

#[test]
#[available_gas(2000000)]
fn bubblesort_test_2_same_values() {
let mut data = array![1_u32, 2_u32, 2_u32, 4_u32];
let mut correct = array![1_u32, 2_u32, 2_u32, 4_u32];

let sorted = bubble_sort::bubble_sort_elements(data);

assert(is_equal(sorted.span(), correct.span()), 'invalid result');
}

0 comments on commit be8a632

Please sign in to comment.