diff --git a/Sorting Algorithms/Bubble Sort/bubble_sort.cpp b/Sorting Algorithms/Bubble Sort/bubble_sort.cpp new file mode 100644 index 0000000..2fe5ea3 --- /dev/null +++ b/Sorting Algorithms/Bubble Sort/bubble_sort.cpp @@ -0,0 +1,35 @@ +//Repeatedly swap two adjacent elements if they are in wrong order. +// For n elements of an array, it takes (n-1) iterations to complete. So, for i^th iteration, it will be (n-i). +#include +using namespace std; +int main() +{ + int n; + cout<<"Enter the number of elements in an array: "; + cin>>n; + int array[n]; + cout<<"Enter the elements of the array:"<>array[i]; + } + int counter=1; + while(counterarray[i+1]) + { + int temp= array[i]; + array[i]=array[i+1]; + array[i+1]=temp; + } + } + counter++; + } + for(int i=0;i +using namespace std; +int main() +{ + int n; + // cout<<"Enter the number of elements of array: "; + cin>>n; + int array[n]; + // cout<<"Enter the elements of the array:"<>array[i]; + } + int count=0; + for(int i=1;icurrent && j>=0) + { + array[j+1]=array[j]; + j--; + count++; + } + array[j+1]=current; + } + // for(int i=0;i +using namespace std; +void merge(int array[],int l,int mid,int r) +{ + int n1=mid+1-l; + int n2=r-mid; + + int a[n1]; + int b[n2]; + // temporary arrays. + + for(int i=0;i>n; + int array[n]; + cout<<"Enter the elements of array: "; + for(int i=0;i>array[i]; + } + for(int i=0;i +using namespace std; +int main() +{ + int n; + cin>>n; + int array[n]; + for(int i=0;i>array[i]; + } + for(int i=0;iarray[j]) + { + int temp=array[j]; + array[j]=array[i]; + array[i]=temp; + } + } + } + for(int i=0;i