From 99cd8fbd0eaef3dc2de43f96f1d97d896ef31be9 Mon Sep 17 00:00:00 2001 From: Saasha Joshi <32019413+SaashaJoshi@users.noreply.github.com> Date: Thu, 4 Oct 2018 10:41:01 +0530 Subject: [PATCH] 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 https://github.com/NITSkmOS/Algorithms/issues/240 --- bubble_sort/Cpp/bubble_sort.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/bubble_sort/Cpp/bubble_sort.cpp b/bubble_sort/Cpp/bubble_sort.cpp index 7afbf06d..44f9a352 100644 --- a/bubble_sort/Cpp/bubble_sort.cpp +++ b/bubble_sort/Cpp/bubble_sort.cpp @@ -1,4 +1,5 @@ -#include +#include +using namespace std; void swap(int *xp, int *yp) { int temp = *xp; @@ -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; } @@ -28,9 +27,8 @@ 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 @@ -38,7 +36,7 @@ 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; }