Skip to content

Match bubble sort C implementation with Julia #254

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

Merged
merged 1 commit into from
Jul 17, 2018
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
2 changes: 1 addition & 1 deletion chapters/algorithms/bubble_sort/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
{% sample lang="cs" %}
[import:9-27, lang:"csharp"](code/csharp/BubbleSort.cs)
{% sample lang="c" %}
[import:4-22, lang:"c_cpp"](code/c/bubble_sort.c)
[import:11-21, lang:"c_cpp"](code/c/bubble_sort.c)
{% sample lang="java" %}
[import:2-12, lang:"java"](code/java/bubble.java)
{% sample lang="js" %}
Expand Down
37 changes: 15 additions & 22 deletions chapters/algorithms/bubble_sort/code/c/bubble_sort.c
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
#include <stdio.h>
#include <stddef.h>

void print_range(int *array, size_t n) {
for (size_t i = 0; i < n; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}

void bubble_sort(int *array, size_t n) {
int swapped = 0;
for (size_t i = 0; i < n - 1; ++i) {
swapped = 0;
for (size_t j = 0; j < n - i - 1; ++j) {
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < n - 1; ++j) {
if (array[j] > array[j + 1]) {
int tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;

swapped = 1;
}
}

if (!swapped) {
break;
}
}
}

int main() {
int array[10] = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4};
int array[] = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4};
size_t N = sizeof(array) / sizeof(*array);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just have size_t N = 10;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will make code less stable, if you make a mistake and assign 100 to N it
will crash.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm asking you to set N to 10, that will not create any issues.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Gathros That's reduntant, though. There is only one correct value for N and that's the length of the array. You can calculate that, so why hardcode it? That way, you can add/remove numbers from the array without having to worry about changing N, too.


printf("Unsorted array:\n");
for (size_t i = 0; i < 10; ++i) {
printf("%d ", array[i]);
}
printf("\n\n");
print_range(array, N);

bubble_sort(array, 10);
bubble_sort(array, N);

printf("Sorted array:\n");
for (size_t i = 0; i < 10; ++i) {
printf("%d ", array[i]);
}
printf("\n");
printf("\nSorted array:\n");
print_range(array, N);

return 0;
}