Skip to content

Commit 120d278

Browse files
committed
Cleanup bubble sort code for C and C++
- remove random generator function - use nested loop instead of is_sorted var
1 parent 847d744 commit 120d278

File tree

4 files changed

+59
-97
lines changed

4 files changed

+59
-97
lines changed

chapters/sorting_searching/bubble/bubble_sort.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
1313
{% sample lang="cs" %}
1414
[import:9-27, lang:"csharp"](code/csharp/BubbleSort.cs)
1515
{% sample lang="c" %}
16-
[import:4-22, lang:"c_cpp"](code/c/bubble_sort.c)
16+
[import:11-23, lang:"c_cpp"](code/c/bubble_sort.c)
17+
{% sample lang="cpp" %}
18+
[import:13-23, lang:"c_cpp"](code/c++/bubble_sort.cpp)
1719
{% sample lang="java" %}
1820
[import:2-12, lang:"java"](code/java/bubble.java)
1921
{% sample lang="js" %}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <algorithm>
2+
#include <cstddef>
3+
#include <iostream>
4+
#include <iterator>
5+
6+
template <class Iter>
7+
void print_range(Iter first, Iter last) {
8+
for (auto it = first; it != last; ++it)
9+
std::cout << *it << " ";
10+
std::cout << std::endl;
11+
}
12+
13+
template <class Iter>
14+
void bubble_sort(Iter first, Iter last) {
15+
for (auto it1 = last; it1 != first; --it1) {
16+
for (auto it2 = first; it2 + 1 != it1; ++it2) {
17+
// these are unsorted! gotta swap 'em
18+
if (*(it2 + 1) < *it2) {
19+
std::swap(*it2, *(it2 + 1));
20+
}
21+
}
22+
}
23+
}
24+
25+
int main() {
26+
int input[] = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4};
27+
28+
std::cout << "Unsorted array:\n";
29+
print_range(std::begin(input), std::end(input));
30+
31+
bubble_sort(std::begin(input), std::end(input));
32+
33+
std::cout << "\nSorted array:\n";
34+
print_range(std::begin(input), std::end(input));
35+
}

chapters/sorting_searching/bubble/code/c++/bubblesort.cpp

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,38 @@
11
#include <stdio.h>
22
#include <stddef.h>
33

4-
void bubble_sort(int *array, size_t n) {
5-
int swapped = 0;
6-
for (size_t i = 0; i < n - 1; ++i) {
7-
swapped = 0;
8-
for (size_t j = 0; j < n - i - 1; ++j) {
9-
if (array[j] > array[j + 1]) {
10-
int tmp = array[j];
11-
array[j] = array[j + 1];
12-
array[j + 1] = tmp;
4+
void print_range(int *array, size_t n) {
5+
for (size_t i = 0; i < n; ++i) {
6+
printf("%d ", array[i]);
7+
}
8+
printf("\n");
9+
}
1310

14-
swapped = 1;
11+
void bubble_sort(int *array, size_t n) {
12+
for (size_t j = n; j > 0; --j) {
13+
for (size_t i = 0; i < j - 1; ++i) {
14+
for (size_t j = 0; j < n - i - 1; ++j) {
15+
if (array[j] > array[j + 1]) {
16+
int tmp = array[j];
17+
array[j] = array[j + 1];
18+
array[j + 1] = tmp;
19+
}
1520
}
1621
}
17-
18-
if (!swapped) {
19-
break;
20-
}
2122
}
2223
}
2324

2425
int main() {
25-
int array[10] = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4};
26+
const int N = 10;
27+
int array[N] = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4};
2628

2729
printf("Unsorted array:\n");
28-
for (size_t i = 0; i < 10; ++i) {
29-
printf("%d ", array[i]);
30-
}
31-
printf("\n\n");
30+
print_range(array, N);
3231

33-
bubble_sort(array, 10);
32+
bubble_sort(array, N);
3433

35-
printf("Sorted array:\n");
36-
for (size_t i = 0; i < 10; ++i) {
37-
printf("%d ", array[i]);
38-
}
39-
printf("\n");
34+
printf("\nSorted array:\n");
35+
print_range(array, N);
4036

4137
return 0;
4238
}

0 commit comments

Comments
 (0)