From 65833c2ea12307ff7f5a18833643c08ce3cfe7bf Mon Sep 17 00:00:00 2001 From: Vanshu2005 <140275765+Vanshu2005@users.noreply.github.com> Date: Sun, 22 Oct 2023 19:08:44 +0530 Subject: [PATCH] Create BubbleSort.cpp --- C++/BubbleSort.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 C++/BubbleSort.cpp diff --git a/C++/BubbleSort.cpp b/C++/BubbleSort.cpp new file mode 100644 index 0000000..bc13234 --- /dev/null +++ b/C++/BubbleSort.cpp @@ -0,0 +1,43 @@ + +#include +using namespace std; + + +void bubbleSort(int arr[], int n) +{ + int i, j; + bool swapped; + for (i = 0; i < n - 1; i++) { + swapped = false; + for (j = 0; j < n - i - 1; j++) { + if (arr[j] > arr[j + 1]) { + swap(arr[j], arr[j + 1]); + swapped = true; + } + } + + + if (swapped == false) + break; + } +} + + +void printArray(int arr[], int size) +{ + int i; + for (i = 0; i < size; i++) + cout << " " << arr[i]; +} + + +int main() +{ + int arr[] = { 64, 34, 25, 12, 22, 11, 90 }; + int N = sizeof(arr) / sizeof(arr[0]); + bubbleSort(arr, N); + cout << "Sorted array: \n"; + printArray(arr, N); + return 0; +} +