From 69c689609e9a9750bfaf260f3f6c593222458919 Mon Sep 17 00:00:00 2001 From: sanaa-duhh Date: Thu, 31 Oct 2024 14:35:49 +0530 Subject: [PATCH] added bubblesort --- C++/bubbleSort.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 C++/bubbleSort.cpp diff --git a/C++/bubbleSort.cpp b/C++/bubbleSort.cpp new file mode 100644 index 0000000..256a9c8 --- /dev/null +++ b/C++/bubbleSort.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; +void bubbleSort(int arr[], int size) { + for (int step = 0; step < size; ++step) { + for (int i = 0; i < size - step; ++i) { + if (arr[i] > arr[i + 1]) { + int temp = arr[i]; + arr[i] = arr[i + 1]; + arr[i + 1] = temp; + } + } + } +} + +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; +} \ No newline at end of file