DIFFERENT SORTING ALGORITHMS AND BINARY SEARCH IN C++
If you study Data Structures then you might know how important is it to know the sorting algorithms. Not just important it is also a famous question during placement in engineering colleges to show them different types of sorting in languages like c++,c or Java.
Today I am going to show u how to make sorting algorithms in C++ and is very similar to C.
To change it to C you just have to do the following tasks:-
- Change cout to printf()
- Change cin to scanf()
- Change to
- Remove the line “using namespace std”
First, 3 of the most basic type of sort which is used to sort but can be used for a small database only.
BUBBLE SORT
#include
using namespace std;
int main()
{
int n,i,j;
cout<<"enter the no. of elements : "; cin>>n;
int ar[n];
for (i=0;i
{ cout<<"enter element "<>ar[i]; }
for (i=0;i
{
for (j=0;j
{
if (ar[j]>ar[j+1])
{
int c;
c=ar[j];
ar[j]=ar[j+1];
ar[j+1]=c;
}
}
}
for (i=0;i
{ cout<
}
SELECTION SORT
#include
using namespace std;
int main()
{
int n,i,j;
cout<<"enter the no. of elements : "; cin>>n;
int ar[n];
for (i=0;i
{ cout<<"enter element "<>ar[i]; }
for (i=0;i
{
for (j=i;j
{
if (ar[i]>ar[j])
{
int c;
c=ar[i];
ar[i]=ar[j];
ar[j]=c;
}
}
}
for (i=0;i
{ cout<
}
INSERTION SORT
#include
using namespace std;
int main()
{
int n,i,j;
cout<<"enter the no. of elements : "; cin>>n;
int ar[n];
for (i=0;i
{ cout<<"enter element "<>ar[i]; }
for (i=1;i
{
int cur=ar[i],j=i-1;
while (j>=0 && ar[j]>cur)
{
ar[j+1]=ar[j];
j--;
}
ar[j+1]=cur;
}
for (i=0;i
{ cout<
}
The method used to sort really big databases and mainly used by companies and often asked in placement due to the less time taken, high speed and high efficiency.
MERGE SORT
#include
using namespace std;
int merge(int ar[],int srt,int mid,int ed)
{
int n1=mid-srt+1 , n2=ed-mid , a=0 , b=0 , i , j , k=srt;
int ar1[n1] , ar2[n2];
for (i=0;i
{ ar1[i]=ar[srt+i]; }
for (i=0;i
{ ar2[i]=ar[mid+1+i]; }
while (a
{
if (ar[a]
{
ar[k]=ar[a];
k++; a++;
}
else
{
ar[k]=ar[b];
k++; b++;
}
}
while (a
{ ar[k]=ar[a];
k++; a++;
}
while (b
{
ar[k]=ar[b];
k++; b++;
}
}
int sort(int ar[],int srt,int ed)
{
if (srt<=ed)
{
int mid=(srt+ed)/2;
sort(ar,srt,mid);
sort(ar,mid+1,ed);
merge(ar,srt,mid,ed);
}
}
int main()
{
int n,k;
cout<<"enter the no. of elements : "; cin>>n;
int ar[n];
for (k=0;k
{ cout<<"enter element "<>ar[k]; }
sort(ar,0,n-1);
for (k=0;k
{ cout<
}
Questions on binary search are very important during the placement as it is really efficient in in finding an element in a sorted arrey.
BINARY SEARCH
#include
Using namespace std;
int binary(int ar[],int low,int high,int search_ele)
{
int mid=(low+high)/2;
if (ar[mid]==search_ele)
{ return mid; }
else if (ar[mid]>search_ele)
{ binary(low,mid-1,search_ele); }
else if (ar[mid]
{ binary(mid+1,high,search_ele); }
}
int main()
{
int pos,i,se,n;
cout<<"enter the no. of elements : "; cin>>n;
int ar[n];
for (i-0;i
{ cout<<"enter element "<>ar[i]; }
cout<<"enter element to search :"; cin>>se;
pos=binary(ar,0,n-1,se);
cout<<"the element "<
}
WRITER:- JAYESH KESWANI