Skip to content

Commit 99cd8fb

Browse files
SaashaJoshisangamcse
authored andcommitted
bubble_sort.cpp: Optimize the C++ code
Replaced use of stdio.h by much safer iostream header file Replaced printf statements by std::cout statements. Closes #240
1 parent 84bea02 commit 99cd8fb

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

bubble_sort/Cpp/bubble_sort.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#include <stdio.h>
1+
#include <iostream>
2+
using namespace std;
23

34
void swap(int *xp, int *yp) {
45
int temp = *xp;
@@ -17,8 +18,6 @@ void bubble_sort(int arr[], int n) {
1718
swapped = true;
1819
}
1920
}
20-
21-
// If no two elements were swapped by inner loop, then break
2221
if (swapped == false)
2322
break;
2423
}
@@ -28,17 +27,16 @@ void bubble_sort(int arr[], int n) {
2827
void print_array(int arr[], int size) {
2928
int i;
3029
for (i = 0; i < size; i++)
31-
printf("%d ", arr[i]);
32-
33-
printf("\n");
30+
cout << arr[i] << " ";
31+
cout << endl;
3432
}
3533

3634
// Driver program to test above functions
3735
int main() {
3836
int arr[] = {64, 34, 25, 12, 22, 11, 90};
3937
int n = sizeof(arr) / sizeof(arr[0]);
4038
bubble_sort(arr, n);
41-
printf("Sorted array: \n");
39+
cout << "Sorted array: " << endl;
4240
print_array(arr, n);
4341
return 0;
4442
}

0 commit comments

Comments
 (0)