From 049087e941041646e538bb54d2c3a44e3bc1c018 Mon Sep 17 00:00:00 2001 From: LeenaKaushik11 Date: Thu, 26 Sep 2019 17:52:58 +0530 Subject: [PATCH] Algorithms of some sorting methods. --- algos/bubble sort in array.cpp | 36 ++++++++++++++++++++++++ algos/insertionsortinarray.cpp | 31 +++++++++++++++++++++ algos/mergesortinarray.cpp | 50 ++++++++++++++++++++++++++++++++++ algos/selectionsortinarray.cpp | 38 ++++++++++++++++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 algos/bubble sort in array.cpp create mode 100644 algos/insertionsortinarray.cpp create mode 100644 algos/mergesortinarray.cpp create mode 100644 algos/selectionsortinarray.cpp diff --git a/algos/bubble sort in array.cpp b/algos/bubble sort in array.cpp new file mode 100644 index 0000000..2427328 --- /dev/null +++ b/algos/bubble sort in array.cpp @@ -0,0 +1,36 @@ +#include +using namespace std; +void bubblesort(int a[],int n) +{ + int small; + for(int i=0;ia[j]) + swap(a[i],a[j]); + } + } +} +void view(int a[],int n) +{ + for(int i=0;i>n; + for(i=0;i>a[i]; + } + cout<<"\n**************before sort****************\n"; + view(a,n); + bubblesort(a,n); + cout<<"\n*******************after sort****************\n"; + view(a,n); + } diff --git a/algos/insertionsortinarray.cpp b/algos/insertionsortinarray.cpp new file mode 100644 index 0000000..40de816 --- /dev/null +++ b/algos/insertionsortinarray.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; +void insertionsort(int A[],int n); +int main() +{ + int a[100],k,i; + cout<<"enter no of values in your array\n"; + cin>>k; + for(int i=0;i>a[i]; + } + insertionsort(a,k); + cout<<"after sorting\n"; + for(i=0;i=0;j--) + if(a[j+1] +using namespace std; +void merge(int a[],int p,int q,int r) +{ + int n1=q-p+1; + int n2=r-q; + int L[n1+1],R[n2+1]; + for(int i=1;i<=n1;i++) + L[i]=a[p+i-1]; + for(int j=1;j<=n2;j++) + R[j]=a[q+j]; + L[n1+1]=300; + R[n2+1]=300; + int i=1,j=1; + for(int k=p;k<=r;k++) + { + if(L[i]<=R[j]) + { + a[k]=L[i]; + i++; + } + else{ + a[k]=R[j]; + j++; + } + } +} +void mergesort(int a[], int p, int r) +{ + if(p>a[i]; + } + mergesort(a,1,7); + for(int i=1;i<=7;i++) + cout< +using namespace std; +void selectionsort(int a[],int n) +{ + int small; + for(int i=0;ia[j]) + small=j; + } + swap(a[i],a[small]); + } +} +void view(int a[],int n) +{ + for(int i=0;i>n; + for(i=0;i>a[i]; + } + cout<<"\n**************before sort****************\n"; + view(a,n); + selectionsort(a,n); + cout<<"\n*******************after sort****************\n"; + view(a,n); + }