Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Algorithms of some sorting methods. #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions algos/bubble sort in array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include<iostream>
using namespace std;
void bubblesort(int a[],int n)
{
int small;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
swap(a[i],a[j]);
}
}
}
void view(int a[],int n)
{
for(int i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
}
int main()
{
int a[100],i,n;
cout<<"enter number of elements \n";
cin>>n;
for(i=0;i<n;i++)
{cout<<"enter value of "<<i+1<<" element \t";
cin>>a[i];
}
cout<<"\n**************before sort****************\n";
view(a,n);
bubblesort(a,n);
cout<<"\n*******************after sort****************\n";
view(a,n);
}
31 changes: 31 additions & 0 deletions algos/insertionsortinarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include<iostream>
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<k;i++)
{
cout<<"enter "<<i+1<<" element of array\n";
cin>>a[i];
}
insertionsort(a,k);
cout<<"after sorting\n";
for(i=0;i<k;i++)
{
cout<<a[i]<<endl;
}
}

void insertionsort(int a[],int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
for(j=i-1;j>=0;j--)
if(a[j+1]<a[j])
swap(a[j+1],a[j]);
}
}
50 changes: 50 additions & 0 deletions algos/mergesortinarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include<iostream>
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<r)
{
int q=(p+r)/2;
mergesort(a,p,q);
mergesort(a,q+1,r);
merge(a,p,q,r);
}
}

int main()
{
int a[10];
for(int i=1;i<=7;i++)
{
cin>>a[i];
}
mergesort(a,1,7);
for(int i=1;i<=7;i++)
cout<<a[i]<<"\t";
return 0;
}
38 changes: 38 additions & 0 deletions algos/selectionsortinarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include<iostream>
using namespace std;
void selectionsort(int a[],int n)
{
int small;
for(int i=0;i<n;i++)
{
small=i;
for(int j=i+1;j<n;j++)
{
if(a[small]>a[j])
small=j;
}
swap(a[i],a[small]);
}
}
void view(int a[],int n)
{
for(int i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
}
int main()
{
int a[100],i,n;
cout<<"enter number of elements \n";
cin>>n;
for(i=0;i<n;i++)
{cout<<"enter value of"<<i+1<<" element \t";
cin>>a[i];
}
cout<<"\n**************before sort****************\n";
view(a,n);
selectionsort(a,n);
cout<<"\n*******************after sort****************\n";
view(a,n);
}