Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: bubble sort in decreasing order #223

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}