0% found this document useful (0 votes)
35 views14 pages

C++ Sorting and Data Structure Programs

Uploaded by

Shaunak Sardesai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views14 pages

C++ Sorting and Data Structure Programs

Uploaded by

Shaunak Sardesai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Program 1

#include<iostream>
using namespace std;
class BINARY
{
short int list[30];
unsigned short int N;
void search();
public:
BINARY();
};
BINARY::BINARY()
{
cout<<"\nHow many elements do you want to enter ";
cin>>N;
cout<<endl<<"Enter elements in Ascending order";
for(int i=0;i<N;i++)
cin>>list[i];
search();
}

void BINARY::search()
{
short int s,mid,low=0,high=N-1,f=0;
cout<<endl<<"Number to be search";
cin>>s;
while(low<=high)
{
mid=(low+high)/2;
if(list[mid]==s)
{
f=mid;
break;

}
else
{

if(list[mid]<s)
low=mid+1;
else
high=mid-1;
}
}
if(f==0)
cout<<endl<<"Searching element is not present";
else
cout<<endl<<"searching element is present at position"<<f+1;
}
int main()
{
BINARY p;
return 0;
}
Program 2
#include<io stream>
using namespace std;
class BUBBLE
{
short int list[30];
unsigned short int N;
void sort();
void show();
public:
BUBBLE();
};
BUBBLE::BUBBLE()
{
cout<<"\nHow many elements do you want to enter ";
cin>>N;
cout<<endl<<"Enter elements";
for(int i=0;i<N;i++)
cin>>list[i];
sort();
show();
}

void BUBBLE::sort()
{
for(int i=0;i<N-1;i++)
{
for(int j=0;j<N-1-i;j++)
{
if(list[j]>list[j+1])
{
int temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
}
}
}

void BUBBLE::show()
{
cout<<endl<<"Sorted list by using Bubble sort technique in Ascending order:- ";
for(int i=0;i<N;i++)
cout<<" "<<list[i];
}
int main()
{
BUBBLE p;
return 0;
}
Program 3
#include<iostream>
using namespace std;
class SELECT
{
short int list[30];
unsigned short int N;
void sort();
void show();
public:
SELECT();
};
SELECT::SELECT()
{
cout<<"\nHow many elements ";
cin>>N;
cout<<endl<<"Enter elements";
for(int i=0;i<N;i++)
cin>>list[i];
sort();
show();
}

void SELECT::sort()
{
for(int i=0;i<N-1;i++)
{
int low=list[i];
int p=i;
for(int j=i+1;j<N;j++)
{
if(list[j]<low)
{
low=list[j];
p=j;
}
}
list[p]=list[i];
list[i]=low;
}
}

void SELECT::show()
{
cout<<endl<<"list in Ascending order using selection sort technique:- ";
for(int i=0;i<N;i++)
cout<<" "<<list[i];
}
int main()
{
SELECT p;
return 0;
}

Program 4
#include<iostream>
using namespace std;
class INSERT
{
short int list[30];
unsigned short int N;
void sort();
void show();
public:
INSERT();
};
INSERT::INSERT()
{
cout<<"\nHow many elements ";
cin>>N;
cout<<endl<<"Enter elements";
for(int i=0;i<N;i++)
cin>>list[i];
sort();
show();
}

void INSERT::sort()
{
for(int i=1;i<=N-1;i++)
{
int t=list[i];
int j=i-1;
while(t<=list[j]&&j>=0)
{
list[j+1]=list[j];
j--;

}
list[j+1]=t;
}
}

void INSERT::show()
{
cout<<endl<<"Sorted array using INSERTION sort technique in Ascending order:- ";
for(int i=0;i<N;i++)
cout<<" "<<list[i];
}
int main()
{
INSERT p;
return 0;
}
Program 5
#include<iostream>
using namespace std;
class MERGE
{
short int A[50],B[50],C[50];
unsigned short int M,N;
void process();
public:
MERGE();
};
MERGE::MERGE()
{
cout<<"\nHow many elements in array A ";
cin>>M;
cout<<endl<<"How many elements in array B";
cin>>N;
cout<<endl<<"Enter elements of array A in Ascending order";
for(int i=0;i<M;i++)
cin>>A[i];
cout<<endl<<"Enter elements of array B in Ascending order";
for(int i=0;i<N;i++)
cin>>B[i];
process();
}

void MERGE::process()
{
int i=0,j=0,k=0;
while(i<M&&j<N)
{
if(A[i]<B[j])
C[k++]=A[i++];
else
C[k++]=B[j++];
}
while(i<M)
C[k++]=A[i++];
while(j<N)
C[k++]=B[j++];
cout<<"After mergimg the array C is:-";
for(int i=0;i<M+N;i++)
cout<<" "<<C[i];
}

int main()
{
MERGE p;
return 0;
}
Program 6
#include<iostream>
using namespace std;
class MATRIX
{
short int M,N,Q;
int A[10][10],B[10][10],C[10][10];
void product();
public:
MATRIX();
};
MATRIX::MATRIX()
{
cout<<"Order of Matrix A";
cin>>M>>N;
cout<<endl<<"Number of column in matrix b";
cin>>Q;
cout<<endl<<"Enter elements in matrix A"<<endl;
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
cin>>A[i][j];

}
cout<<endl<<"Enter elements in matrix B"<<endl;
for(int i=0;i<N;i++)
{
for(int j=0;j<Q;j++)
{
cin>>B[i][j];

}
product();
}

void MATRIX::product()
{
for(int i=0;i<M;i++)
{
for(int j=0;j<Q;j++)
{
C[i][j]=0;
for(int k=0;k<N;k++)
C[i][j]+=A[i][k]*B[k][j];
}
}
cout<<endl<<"The resultant array is "<<endl;
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
cout<<" "<<C[i][j];
cout<<endl;
}
}
int main()
{
MATRIX p;
return 0;
}

Program 7
#include<iostream>
using namespace std;
class linkedlist
{
struct node
{
int roll;
char name[30];
float percent;
node *next;
}*START;
public:
linkedlist();
void creat();
void display();
};
linkedlist::linkedlist()
{
START=NULL;

}
void linkedlist::creat()
{
node *temp,*prev;
int n;
cout<<endl<<"How many student details:-";
cin>>n;
for(int i=0;i<n;i++)
{
temp=new node;
cout<<"Enter Roll Number";
cin>>temp->roll;
cout<<"Enter Name";
cin>>temp->name;
cout<<"enter Percent";
cin>>temp->percent;
temp-> next =NULL;
if(START==NULL)
{
START=prev=temp;

}
else
{
prev-> next =temp;
prev=temp;
}

}
}
void linkedlist::display()
{
node *temp;
temp=START;
if (START==NULL)
cout<<endl<<"linked list is empty";
else
{
while(temp!=NULL)
{
cout<<temp->roll<<"\t\t"<<temp->name<<"\t"<<temp->percent<<endl;
temp=temp->next;
}
}
}
int main()
{

//system("CLS");
int choice;
linkedlist a;
do
{
cout<<"\n1. To Create a Linked list";
cout<<"\n2. To Display linked list";
cout<<"\n3. Exit";
cout<<"\nEnter your choice";
cin>>choice;
switch(choice)
{
case 1:
a.creat();
break;
case 2:
system("CLS");
cout<<endl<<"Roll Number"<<"\t"<<"Name"<<"\t"<<"Percent"<<endl;
a.display();
case 3:
exit(0);
default:
cout<<"\nWrong choice";

}
char c;
cout<<"\ndo u want to continue......(y/n)";
cin>>c;
if(c=='y')
continue;
else exit(0);

}while(1);
return 0;
}

Program 8
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

class STACK
{
struct node
{
char name[30];
node *link;
}*Top;
public:
void push();
void pop();
void display();
STACK();
};
STACK::STACK()
{
Top=NULL;
}
void STACK::push()
{
node *temp;
temp=new node;
cout<<"Enter Word:";
cin>>temp->name;
temp->link=Top;
Top=temp;
}
void STACK::display()
{

if(Top==NULL)
cout<<"\nThe Stack is Empty!!\n";
else
{
node *temp=Top;

cout<<"\nThe content of stack is:-\n";


while(temp!=NULL)
{
cout<<temp->name;
if(temp->link!=NULL)
cout<<" <-";
temp=temp->link;
}
}
}
void STACK::pop()
{

if(Top==NULL)
cout<<"Stack Is Empty!";
else
{
node *temp=Top;
Top=Top->link;
delete temp;
display();
}

}
int main()
{
STACK obj;
while(1)
{
short int n;
cout<<"\n1.Push"<<endl<<"2.Pop"<<endl<<"3.Display";
cout<<"\nyour choice";
cin>>n;
switch(n)
{
case 1: obj.push();
break;
case 2: obj.pop();
break;
case 3: system("CLS");
obj.display();
break;

default: cout<<"Invalid Choice!!\n";


}
char c;
cout<<"\ndo u want to continue......(y/n)";
cin>>c;
if(c=='y')
continue;
else exit(0);
}
}

Program 9
#include<iostream>
using namespace std;

class QUEUE
{
struct node
{
char name[30];
node *link;
}*rear,*Front;

public:
QUEUE(){Front=rear=NULL;}
void Append();
void Delete();
void Display();
};
void QUEUE::Append()
{
node *temp;
temp=new node;
cout<<"Enter any Charcter:";
cin>>temp->name;
if(Front==NULL)
Front=rear=temp;
rear->link=temp;
temp->link=NULL;
rear=temp;
}

void QUEUE::Delete()
{
if(Front==NULL)
cout<<"The queue is Empty!\n";
else
{
node *temp=Front;
Front=Front->link;
delete temp;
Display();
}
}
void QUEUE::Display()
{
if(Front==NULL)
cout<<"\nThe Queue is Empty!!\n\n";
else
{
node *temp=Front;
cout<<"\nThe content of the Queue:-\n";
while(temp!=NULL)
{
cout<<temp->name;
if(temp->link!=NULL)
cout<<"->";
temp=temp->link;
}
}
}

int main()
{
QUEUE obj;
while(1)
{
short int n;
cout<<"\
n1.Append"<<endl<<"2.Delete"<<endl<<"3.Display()"<<endl<<"4.Exit"<<endl;
cout<<"Your choice";
cin>>n;
switch(n)
{
case 1: system("CLS");
obj.Append();
break;
case 2: system("CLS");
obj.Delete();
break;
case 3:system("CLS");
obj.Display();
break;
case 4: exit(0);
default: cout<<"Invalid Choice!!\n";
break;
}
char c;
cout<<"\ndo u want to continue......(y/n)";
cin>>c;
if(c=='y')
continue;
else exit(0);
}
}

Program 10
#include<iostream>
#include<fstream>
#include<stdlib.h>

using namespace std;

class BILL
{
unsigned short int item_code,quantity;
char item_name[30];
float unit_price,total;
public:
void get_data();
void put_data();
};
void BILL::get_data()
{
cout<<endl<<"Enter item code";
cin>>item_code;
cout<<"Enter item name";
cin>>item_name;
cout<<"Quantity";
cin>>quantity;
cout<<"Unit price";
cin>>unit_price;

}
void BILL::put_data()
{
cout<<endl<<item_code<<"\t\t"<<item_name<<"\t\t"<<quantity<<"\t\
t"<<unit_price<<"\t\t"<<quantity*unit_price;

}
int main()
{
BILL b;
fstream file;
file.open("MARKET.DAT",ios::binary|ios::out);
int n;
cout<<endl<<"How many Item purchased";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<endl<<"Enter the detail of item:- "<<i+1;
b.get_data();
file.write((char*)&b,sizeof(b));

}
file.close();
system("CLS");
cout<<endl<<"ITEM CODE"<<"\t"<<"ITEM NAME"<<"\t"<<"QUANTITY"<<"\t"<<"UNIT
PRICE"<<"\t"<<"TOTAL";
file.open("market.dat",ios::binary|ios::in);
if(!file)
{
cout<<endl<<"File does not exist";
exit(0);
}

file.read((char*)&b,sizeof(b));
while(file)
{
b.put_data();
file.read((char*)&b,sizeof(b));
}

file.close();
return 0;
}

You might also like