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

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 chapters/sorting_searching/bubble/bubble_sort.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
</script>
$$
$$
\newcommand{\d}{\mathrm{d}}
\newcommand{\bff}{\boldsymbol{f}}
\newcommand{\bfg}{\boldsymbol{g}}
Expand All @@ -25,7 +25,7 @@ $$
When it comes to sorting algorithms, Bubble Sort is usually the first that comes to mind.
Though it might not be the fastest tool in the shed, it's definitely straightforward to implement and is often the first sorting method new programmers think of when trying to implement a sorting method on their own.

Here's how it works: we go through each element in our vector and check to see if it is larger than the element to it's right.
Here's how it works: we go through each element in our vector and check to see if it is larger than the element to it's right.
If it is, we swap the elements and then move to the next element.
In this way, we sweep through the array $$n$$ times for each element and continually swap any two adjacent elements that are improperly ordered.
This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with code similar to the following:
Expand All @@ -35,6 +35,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
[import:1-10, lang:"julia"](code/julia/bubble.jl)
{% sample lang="cs" %}
[import:9-27, lang:"csharp"](code/cs/BubbleSort.cs)
{% sample lang="c" %}
[import:3-21, lang:"c_cpp"](code/c/bubble_sort.c)
{% sample lang="js" %}
[import:1-11, lang:"javascript"](code/js/bubble.js)
{% endmethod %}
Expand Down
42 changes: 42 additions & 0 deletions chapters/sorting_searching/bubble/code/c/bubble_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdio.h>
#include <stddef.h>

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) {
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};

Choose a reason for hiding this comment

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

Could you use a #define instead of magic constants throughout the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think that's needed really, it's just an example.


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

bubble_sort(array, 10);

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

return 0;
}