Bubble Sort

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,c,a[20],n;
for(i=0;i<n;i++)
{
cout<<“enter the elements”;
for(i=0;i<n;i++)
{
cin>>a[i];
} }
for(i=1;i<=n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
c=a[j];
a[j]=a[j+1];
a[j+1]=c;
}
}
}
for(i=0;i<n;i++)
{
cout<<a[i];
}
getch();
}

Selection Sort

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct student
{
int marks;
}s1[100],s2,s[100];
void sel(student&s[],int x);
void main()
{
clrscr();
int n;
cout<<“enter n”;
cin>>n;
cout<<“enter”;
for(int a=0;a<n;a++)
{
cin>>s1[a].marks;

}
cout<<“list is”;
sel(s1,n);
getch();
}
void sel(student&s[],int x)
{
int pos,small,temp;
for(int i=0;i<x-1;i++)
{

small=s[i].marks;
pos=i;
for(int j=i+1;j<x;j++)
{
if(small>s[j].marks)
{
small=s[j].marks;
pos=j;
}
}
s2.marks=s[pos].marks;
s[pos].marks=s[i].marks;
s[i].marks=s2.marks;
}
for(int f=0;f<x;f++)
{
cout<<“\n “<<s[f].marks<<“\t”;

}
}

 

Insertion Sort

#include<iostream.h>
#include<conio.h>
void insert(int array[],int n);
void main()
{ int n;
clrscr();
cout<<” enter n”;
cin>>n;
int array[50];
cout<<“enter”;
for(int a=0;a<=n;a++)
{
cin>>array[a];
}
insert(array,n);
for(int b=0;b<n;b++)
{
cout<<array[b];
cout<<“\n”;
}
getch();
}
void insert(int array[],int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
temp=array[i];
j=i-1;
while((temp<array[j])&&(j>=0))
{
array[j+1]=array[j];
j–;
}
array[j+1]=temp;
}
}

QUICK SORT

#include<iostream.h>
#include<conio.h>
int quick(int a[],int n,int beg,int end);

int main()
{
int a[50];
int n;
cout<<“enter size”;
cin>>n;
cout<<“enter elements”;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
if(n<1)
{
cout<<“error”;
exit(0);
}
int top,lower[50],upper[50];
int beg,end,loct;
beg=0;
end=n-1;
top=0;
lower[0]=0;
upper[0]=n-1;
while(top>-1)
{
beg=lower[top];
end=upper[top];
top–;
loct=quick(a,n,beg,end);
if(beg<(loct-1))
{
top++;
lower[top]=beg;
upper[top]=loct-1;
}
if(end>(loct+1))
{
top++;
lower[top]=loct+1;
upper[top]=end;
}
}
cout<<“sorted array is”;
for(int b=0;b<n;b++)
{
cout<<a[b]<<“\n”;
}
getch();
return 0;
}

int quick(int a[],int n,int beg,int end)
{
int loc;
loc=beg;
int right,left,temp;
left=beg;
right=end;
int count,flag1,flag2;
count=1;
flag1=flag2=0;
while(count==1||flag1==1)
{
while(((a[loc]<a[right])||(a[loc]==a[right]))&&(loc!=right))
{
right–;
}
if(loc==right)
{
return loc;
}
else
{
if(a[loc]>a[right])
{
temp=a[loc];
a[loc]=a[right];
a[right]=temp;
loc=right;
flag2=1;
}
}
while(((a[loc]>a[left])||(a[loc]==a[left]))&&(loc!=left))
{
left++;
}
if(loc==left)
{
return loc;
}
else
{
if(a[loc]<a[left])
{
temp=a[loc];
a[loc]=a[left];
a[left]=temp;
loc=left;
flag1=1;
}
}
}
}

Calendar

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

Archives