Skip to content

Commit

Permalink
bubble_sort.cpp: Optimize the C++ code
Browse files Browse the repository at this point in the history
Replaced use of stdio.h by much safer iostream header file
Replaced printf statements by std::cout statements.

Closes #240
  • Loading branch information
SaashaJoshi authored and sangamcse committed Oct 4, 2018
1 parent 84bea02 commit 99cd8fb
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions bubble_sort/Cpp/bubble_sort.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <iostream>
using namespace std;

void swap(int *xp, int *yp) {
int temp = *xp;
Expand All @@ -17,8 +18,6 @@ void bubble_sort(int arr[], int n) {
swapped = true;
}
}

// If no two elements were swapped by inner loop, then break
if (swapped == false)
break;
}
Expand All @@ -28,17 +27,16 @@ void bubble_sort(int arr[], int n) {
void print_array(int arr[], int size) {
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);

printf("\n");
cout << arr[i] << " ";
cout << endl;
}

// Driver program to test above functions
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
printf("Sorted array: \n");
cout << "Sorted array: " << endl;
print_array(arr, n);
return 0;
}

0 comments on commit 99cd8fb

Please sign in to comment.