selection sort / insertion sort

December 8, 2008

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();

int a[5],i,j,p,min,t,x,k;
cout<<”enter the numbers:”;

for(i=0;i<5;i++)
{
cin>>a[i];
}

cout<<”enter the type of sorting u wanna do:\n”;
cout<<”1:insertion sort”<<”\n”<<”2:selection sort”;
cin>>t;
switch (t)
{
case 1:
cout<<”sorting by INSERTION sort…” ;

for(i=1;i<5;i++)
{
if(min>a[i])
{
min=a[i];
p=i;
}
}
a[p]=a[0];
a[0]=min ;
for(i=2;i<5;i++)
{
x=a[i];
j=1;
while(x<a[j-1])
{
a[j]=a[j-1];
j=j-1;
}
a[j]=x;
}cout<<”\n sorted list is”;
for(i=0;i<5;i++)
{
cout<<” “<<a[i]<<” “;
}
break;

case 2:
cout<<”SELECTION SORTING…”;
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
{
if(a[j]<a[i])
{ k=a[j];
a[j]=a[i];
a[i]=k;
}
}
}
cout<<”\n sorted list is: \n” ;
for(i=0;i<5;i++)
{
cout<<a[i]<<” “;
}
break;
default : cout<<”wrong choice”;
break;
}
getch();
}

bubble sort binary search

December 8, 2008

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int a[100] , i , j ,k , l, flag=1;
cout<<”enter no. \n “;
for(i=0;i<=10;i++)
{
cin>>a[i];
}

for(i=0;i<=10;i++)
{
for(j=0;j<=9;j++)
{
if(a[j+1]<a[j])
{
k=a[j+1];
a[j+1]=a[j];
a[j]=k;
}
}
}
cout<<”\n sorted list is:”;
for(i=0;i<=10;i++)
{
cout<<a[i]<<” “;
}

cout<<”\n sorting finished…”<<”Now performing search operation using binary search method”<<endl;
cout<<”\n enter no to be searched:”;
cin>>l;
int left=0;
int right=9;
while(left<=right)
{
int mid=(left+right)/2;
if (l<a[mid])
right=mid-1;
else if(l>a[mid])
left=mid+1;
else if(l==a[mid])
{
cout<<l<<”number is at “<<mid<<”position”;
flag=0;
break;
}
}
if(flag==1)
cout<<”not found”;

}

voting vaala…:)

December 7, 2008

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct voter
{
int id, age;
char nm[20], add[50];
};

voter ward[20];
int main()
{
clrscr();
for(int i=0;i<2;i++)
{
cout<<”\n voter no.”<<i+1;
cout<<” \n enter id no.:”;
cin>>ward[i].id;
cout<<”\n Enter name:”;
gets(ward[i].nm);
cout<<”\n enter address:”;
gets(ward[i].add);
cout<<”\n Enter age”;
cin>> ward[i].age;
}
cout<<”\n voters with age more then 60 are:\n”;
for(i=0;i<20;i++)
{
if(ward[i].age>60)
{
cout<<”\n id no:”;
cout<<ward[i].id;
cout<<”\n name:”;
cout.write(ward[i].nm,20);
cout<<”\n”;
}
}
}

TO FIND A STRING IS A PALINDROME OR NOT

December 7, 2008

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
char st[100],c;
cout<<”enter string:”;
cin.getline(st,80);

for (int i=0;i<=st[i]!=”;i++);
{
int j , k , z=1 ;
for(j=0, k=i-1; j<i/2; j++,k– )
{
if(st[j]!=st[k])
{
z=0;
break;
}
}
if(z)
cout<<”\n It is a palindrome”;
else
cout<<”\n It is not a palindrome”;

return 0 ;
}

}

list of programs

December 7, 2008

>> to add no of days to a given date

>> c++ function to add, sub, multiply, divide complex nos

>> funvtion to count & display the no of lines starting with  A present in lines.txt

>>FUNCTION TO FIND DA SUM OF DIAGONAL ELEMENTS OF 2D ARRAYOF TYPE FLOAT

>>CREATE AN ARRAY 100 NO, SORT USING BUBBLE SORT N FIND A NO USING BINARY SEARCH

>> INTERICTIVE PROG TO IMPLEMENT STACK USING ARRAY. ARRAY CONTAINS SAL OF EMP

>> w.a.p.  TO INSERT AN ELEMENT IN QUEUE & TO DEL AN ELEMENT FROM QUEUE. QUEUE STORES NAME OF STU.


Follow

Get every new post delivered to your Inbox.