From 6c7289bbf638a69541b1943ad6841ab640f93fbc Mon Sep 17 00:00:00 2001 From: Avinash Kumar Date: Mon, 31 Oct 2022 18:11:16 +0530 Subject: [PATCH] Bubble_Sort code added --- Bubble_Sort/Bubble_sort.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Bubble_Sort/Bubble_sort.cpp diff --git a/Bubble_Sort/Bubble_sort.cpp b/Bubble_Sort/Bubble_sort.cpp new file mode 100644 index 00000000..09f637e5 --- /dev/null +++ b/Bubble_Sort/Bubble_sort.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; + +// A function to implement bubble sort +void bubbleSort(int arr[], int n) +{ + int i, j; + for (i = 0; i < n - 1; i++) + + + for (j = 0; j < n - i - 1; j++) + if (arr[j] > arr[j + 1]) + swap(arr[j], arr[j + 1]); +} + + +void printArray(int arr[], int size) +{ + int i; + for (i = 0; i < size; i++) + cout << arr[i] << " "; + cout << endl; +} + + +int main() +{ + int arr[] = { 5, 1, 4, 2, 8}; + 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