From 291c7b02207965a14ab8bdf1fc323ada29e54245 Mon Sep 17 00:00:00 2001 From: MahimaSinghRathore <160035550+MahimaSinghRathore@users.noreply.github.com> Date: Sat, 19 Oct 2024 00:10:52 +0530 Subject: [PATCH] Added simple bubble sort program in C++ --- Cpp/simple_bubble_sort.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Cpp/simple_bubble_sort.cpp diff --git a/Cpp/simple_bubble_sort.cpp b/Cpp/simple_bubble_sort.cpp new file mode 100644 index 0000000..519277a --- /dev/null +++ b/Cpp/simple_bubble_sort.cpp @@ -0,0 +1,35 @@ +#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]) { + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } +} + +int main() { + int arr[] = {5, 3, 8, 4, 2}; + int n = 5; + + cout << "Original array: "; + for (int i = 0; i < n; i++) { + cout << arr[i] << " "; + } + cout << endl; + + bubbleSort(arr, n); + + cout << "Sorted array: "; + for (int i = 0; i < n; i++) { + cout << arr[i] << " "; + } + cout << endl; + + return 0; +}