From 200c8515d808343a662c83c8c038e35b74f6c2dc Mon Sep 17 00:00:00 2001 From: Vishakhasarode <116514239+Vishakhasarode@users.noreply.github.com> Date: Mon, 24 Oct 2022 00:14:57 +0530 Subject: [PATCH] created bubblesort algorithm using cpp --- data-structure/bubble sort.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 data-structure/bubble sort.cpp diff --git a/data-structure/bubble sort.cpp b/data-structure/bubble sort.cpp new file mode 100644 index 0000000..e4aedb5 --- /dev/null +++ b/data-structure/bubble sort.cpp @@ -0,0 +1,18 @@ +#include +using namespace std; +void bubbleSort(int arr[], int n) +{ + for (int i = 0; i < n - 1; i++) + for (int j = 0; j < n - i - 1; j++) + if (arr[j] > arr[j + 1]) + swap(arr[j], arr[j + 1]); +} +int main() +{ + int arr[5] = {5, 2, 7, 9, 1}; + int n = sizeof(arr) / sizeof(int); + bubbleSort(arr, n); + for (int i = 0; i < n; i++) + cout << arr[i] << " "; + return 0; +}