Skip to content

Commit

Permalink
Feat: bubble sort in decreasing order (#223)
Browse files Browse the repository at this point in the history
## Pull Request type

Please check the type of change your PR introduces:

- [ ] Bugfix
- [X] 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?

The bubble sort function only allow to sort an array in ascending order.
this feature allow to sort in decreasing order.

Issue Number: N/A

## What is the new behavior?

- added a new boolean to select mode of sorting
- if the boolean is set to false, the array is sorted in decreasing
order
- if the boolean is set to true, the array is sorted in ascending order

## Does this introduce a breaking change?

- [ ] Yes
- [X] Maybe
- [ ] No

as the prototype of the function change, its possible that application
that used the old version will need to update their code by adding the
boolean.

## Other information

I was wondering about creating two distinct function to avoid breaking
change but i wasn't confortable to have two almost similar function.
feel free to give me feedback
  • Loading branch information
azurwastaken authored Dec 8, 2023
1 parent 1de9e5f commit 3cb40ac
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/sorting/src/bubble_sort.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
/// * `array` - Array to sort
/// # Returns
/// * `Array<usize>` - Sorted array
fn bubble_sort_elements<T, +Copy<T>, +Drop<T>, +PartialOrd<T>>(mut array: Array<T>) -> Array<T> {
fn bubble_sort_elements<T, +Copy<T>, +Drop<T>, +PartialOrd<T>, +PartialEq<T>>(
mut array: Array<T>, asc: bool
) -> Array<T> {
if array.len() <= 1 {
return array;
}
Expand All @@ -26,7 +28,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]) || !((asc) ^ (*array[idx1] < *array[idx2])) {
sorted_array.append(*array[idx1]);
idx1 = idx2;
idx2 += 1;
Expand Down
92 changes: 85 additions & 7 deletions src/sorting/src/tests/bubble_sort_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn bubblesort_test() {
let mut data = array![4_u32, 2_u32, 1_u32, 3_u32, 5_u32, 0_u32];
let mut correct = array![0_u32, 1_u32, 2_u32, 3_u32, 4_u32, 5_u32];

let sorted = bubble_sort::bubble_sort_elements(data);
let sorted = bubble_sort::bubble_sort_elements(data, true);

assert(is_equal(sorted.span(), correct.span()), 'invalid result');
}
Expand All @@ -18,7 +18,7 @@ fn bubblesort_test_empty() {
let mut data = array![];
let mut correct = array![];

let sorted = bubble_sort::bubble_sort_elements(data);
let sorted = bubble_sort::bubble_sort_elements(data, true);

assert(is_equal(sorted.span(), correct.span()), 'invalid result');
}
Expand All @@ -29,7 +29,7 @@ fn bubblesort_test_one_element() {
let mut data = array![2_u32];
let mut correct = array![2_u32];

let sorted = bubble_sort::bubble_sort_elements(data);
let sorted = bubble_sort::bubble_sort_elements(data, true);

assert(is_equal(sorted.span(), correct.span()), 'invalid result');
}
Expand All @@ -40,7 +40,7 @@ fn bubblesort_test_pre_sorted() {
let mut data = array![1_u32, 2_u32, 3_u32, 4_u32];
let mut correct = array![1_u32, 2_u32, 3_u32, 4_u32];

let sorted = bubble_sort::bubble_sort_elements(data);
let sorted = bubble_sort::bubble_sort_elements(data, true);

assert(is_equal(sorted.span(), correct.span()), 'invalid result');
}
Expand All @@ -51,7 +51,7 @@ fn bubblesort_test_pre_sorted_decreasing() {
let mut data = array![4_u32, 3_u32, 2_u32, 1_u32];
let mut correct = array![1_u32, 2_u32, 3_u32, 4_u32];

let sorted = bubble_sort::bubble_sort_elements(data);
let sorted = bubble_sort::bubble_sort_elements(data, true);

assert(is_equal(sorted.span(), correct.span()), 'invalid result');
}
Expand All @@ -62,7 +62,7 @@ fn bubblesort_test_pre_sorted_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);
let sorted = bubble_sort::bubble_sort_elements(data, true);

assert(is_equal(sorted.span(), correct.span()), 'invalid result');
}
Expand All @@ -73,7 +73,85 @@ fn bubblesort_test_2_same_values() {
let mut data = array![1_u32, 2_u32, 4_u32, 2_u32];
let mut correct = array![1_u32, 2_u32, 2_u32, 4_u32];

let sorted = bubble_sort::bubble_sort_elements(data);
let sorted = bubble_sort::bubble_sort_elements(data, true);

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

#[test]
#[available_gas(20000000000000)]
fn bubblesort_test_dsc() {
let mut data = array![4_u32, 2_u32, 1_u32, 3_u32, 5_u32, 0_u32];
let mut correct = array![5_u32, 4_u32, 3_u32, 2_u32, 1_u32, 0_u32];

let sorted = bubble_sort::bubble_sort_elements(data, false);

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


#[test]
#[available_gas(2000000)]
fn bubblesort_test_empty_dsc() {
let mut data = array![];
let mut correct = array![];

let sorted = bubble_sort::bubble_sort_elements(data, false);

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

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

let sorted = bubble_sort::bubble_sort_elements(data, false);

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

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

let sorted = bubble_sort::bubble_sort_elements(data, false);

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

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

let sorted = bubble_sort::bubble_sort_elements(data, false);

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

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

let sorted = bubble_sort::bubble_sort_elements(data, false);

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

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

let sorted = bubble_sort::bubble_sort_elements(data, false);

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

0 comments on commit 3cb40ac

Please sign in to comment.