From f1816a9927e86e1020f22ad2490b147202074269 Mon Sep 17 00:00:00 2001 From: Aditya Chouksey Date: Thu, 23 Oct 2025 13:01:56 +0530 Subject: [PATCH] Create bubble_sort.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Idea: Repeatedly swap adjacent elements if they are in the wrong order. Time Complexity: O(n²) --- application of algorithm/bubble_sort.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 application of algorithm/bubble_sort.cpp diff --git a/application of algorithm/bubble_sort.cpp b/application of algorithm/bubble_sort.cpp new file mode 100644 index 0000000..7697125 --- /dev/null +++ b/application of algorithm/bubble_sort.cpp @@ -0,0 +1,21 @@ +#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, 3, 8, 4, 2}; + int n = 5; + + bubbleSort(arr, n); + + cout << "Sorted array: "; + for (int x : arr) cout << x << " "; +}