From a163a9a515413598f585f290ed042b7e04780d41 Mon Sep 17 00:00:00 2001 From: Sharmistha Mandal <65134125+Sharmi-1999@users.noreply.github.com> Date: Thu, 1 Oct 2020 22:14:27 +0530 Subject: [PATCH] Add files via upload --- .../Bubble_Sort/bubble_sort.cpp | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/algorithm_practice/Searching_And_Sorting/Bubble_Sort/bubble_sort.cpp diff --git a/src/algorithm_practice/Searching_And_Sorting/Bubble_Sort/bubble_sort.cpp b/src/algorithm_practice/Searching_And_Sorting/Bubble_Sort/bubble_sort.cpp new file mode 100644 index 00000000..347bceb9 --- /dev/null +++ b/src/algorithm_practice/Searching_And_Sorting/Bubble_Sort/bubble_sort.cpp @@ -0,0 +1,61 @@ +// Best Case Time Complexity: O(n). Best case occurs when array is already sorted. +// Worst and Average Case Time Complexity: O(n^2). Worst case occurs when array is reverse sorted. + +// Auxiliary Space: O(1) + +#include +#include +using namespace std; + +void bubble_sort(int a[],int n) +{ + int flag,p=0; + for(int i=0;ia[j+1]) + { + swap(a[j],a[j+1]); + flag=1; + p++; + } + } + if(flag==0) + { + break; + } + } + + cout<<"Array is sorted in"<<" "<>n; + int a[n]; + for(int i=0;i>a[i]; + } + bubble_sort(a,n); + cout<<"Sorted array is:"<<" "; + for(int i=0;i