0% found this document useful (0 votes)
52 views

C Prog

This document describes the heap sort algorithm. It begins by defining functions like adjust() and heapify() to maintain the heap property during the sorting process. It initializes an array hsort[] to store the elements, takes input n for the number of elements, and calls the heapify() function. Heapify() calls adjust() in a for loop to build the max heap. Adjust() moves elements down the tree to maintain the max heap property. Finally, it repeatedly extracts the maximum element from the root and replaces it with the last element, calling adjust() to maintain the heap, sorting the array in-place.

Uploaded by

thiru12741550
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)
52 views

C Prog

This document describes the heap sort algorithm. It begins by defining functions like adjust() and heapify() to maintain the heap property during the sorting process. It initializes an array hsort[] to store the elements, takes input n for the number of elements, and calls the heapify() function. Heapify() calls adjust() in a for loop to build the max heap. Adjust() moves elements down the tree to maintain the max heap property. Finally, it repeatedly extracts the maximum element from the root and replaces it with the last element, calling adjust() to maintain the heap, sorting the array in-place.

Uploaded by

thiru12741550
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/ 8

Merge Sort }

}
#include<iostream.h> template<class t>
template<class t> void sort<t>::mergesort(int low,int
class sort mid,int high)
{ {
t a[10]; t b[10];
public: int h,i,j,k;
void getdata(int); h=low;
void merge(int,int); i=low;
void mergesort(int,int,int); j=mid+1;
void display(int); while((h<=mid)&&(j<=high))
}; {
template<class t> if(a[h]<=a[j])
void sort<t>::getdata(int n) {
{ b[i]=a[h];
int i; h=h+1;
cout<<"\n enter the array elements:"; }
for(i=0;i<n;i++) else
cin>>a[i]; {
} b[i]=a[j];
template<class t> j=j+1;
void sort<t>::display(int n) }
{ i=i+1;
int i; }
cout<<"\n the sorted array is \n"; if(h>mid)
for(i=0;i<n;i++) {
cout<<a[i]<<"\t"; for(k=j;k<=high;k++)
} {
template<class t> b[i]=a[k];
void sort<t>::merge(int low,int high) i=i+1;
{ }
int mid; }
if(low<high) else
{ {
mid=(low+high)/2; for(k=h;k<=mid;k++)
merge(low,mid); {
merge(mid+1,high); b[i]=a[k];
mergesort(low,mid,high); i=i+1;
} private:
} node<t> *first,*last;
for(k=low;k<=high;k++) public:
{ polynomial();
a[k]=b[k]; void display();
} void insertend(t c,t e);
} polynomial<t>
main() operator+(polynomial<t> &b);
{ };
int n; //constructor
cout<<"\n merge sort using templates"; template<class t>
sort<int>n1; polynomial<t>::polynomial()
sort<float>n2; {
cout<<"\n enter the array size"; first=NULL;
cin>>n; last=NULL;
n1.getdata(n); }
n1.merge(0,n-1); //display function
n1.display(n); template<class t>
n2.getdata(n); void polynomial<t>::display()
n2.merge(0,n-1); {
n2.display(n); node<t> *temp=first;
} while(temp!=NULL)
{
Polynomial Arithmetic cout<<temp->coef<<"x^"<<temp->exp<<" ";
#include<iostream.h> temp=temp->next;
template<class t> }
class polynomial; }
template<class t> template<class t>
class node void polynomial<t>::insertend(t c,t e)
{ {
friend class polynomial<t>; node<t> *temp=new node<t>;
private: temp->coef=c;
t coef; temp->exp=e;
t exp; temp->next=NULL;
node<t> *next; if(first==NULL)
}; {
template<class t> first=temp;
class polynomial last=temp;
{ }
else }
{ while(bpos!=NULL)
last->next=temp; {
last=temp; c.insertend(bpos->coef,bpos->exp);
} bpos=bpos->next;
} }
template<class t> return c;
polynomial<t> }
polynomial<t>::operator+(polynomial<t> main()
&b) {
{ polynomial<int> a,b,c;
node<t> *apos=first; a.insertend(3,5);
node<t> *bpos=b.first; a.insertend(4,3);
polynomial<t> c; a.insertend(2,1);
while(apos!=NULL && bpos!=NULL) cout<<"\nThe polynomial a is ....\n";
{ a.display();
if(apos->exp==bpos->exp) b.insertend(7,5);
{ b.insertend(5,4);
t sum=apos->coef+bpos->coef; b.insertend(10,0);
if(sum!=0) cout<<"\nThe polu=ynomial b is.....\n";
c.insertend(sum,apos->exp); b.display();
apos=apos->next; c=a+b;
bpos=bpos->next; cout<<"\nAfter adding two polynomials the
} result is \n";
else if(apos->exp > bpos->exp) c.display();
{ }
c.insertend(apos->coef,apos->exp);
apos=apos->next;
} Hashing
else #include<iostream.h>
{ template<class T>
c.insertend(bpos->coef,bpos->exp); class Hash
bpos=bpos->next; {
} private:
} T *arr;//hash table
while(apos!=NULL) int size;
{ public:
c.insertend(apos->coef,apos->exp); void insert(T key);
apos=apos->next; int search(T key);
int hash(T key); else
void display(); return -1;
Hash(int s); }
}; template<class T>
template<class T> void Hash<T>::display()
Hash<T>::Hash(int s) {
{ int i;
size=s; for(i=0;i<size;i++)
arr=new T[size]; cout<<" "<<arr[i];
int i; }
for(i=0;i<size;i++) main()
arr[i]=0; {
} Hash <int> ht(10);
template<class T> int ch,i,key;
int Hash<T>::hash(T key)
{ do
return key%size; {
} cout<<"\n****MAIN MENU****";
template<class T> cout<<"\n1.Insert";
void Hash<T>::insert(T key) cout<<"\n2.search";
{ cout<<"\n3.Display";
int i; cout<<"\n4.Exit";
i=hash(key); cout<<"\nEnter choice:";
if(arr[i]==0) cin>>ch;
{ switch(ch)
arr[i]=key; {
cout<<"\n Elements are inserted case 1:
successfully"; cout<<"Enter a key value:";
} cin>>key;
else ht.insert(key);
cout<<"A collision has occured"; break;
} case 2:
template<class T> int i,key;
int Hash<T>::search(T key) cout<<"\nEnter the no to be searched";
{ cin>>key;
int i; i=ht.search(key);
i=hash(key); if(i==-1)
if(arr[i]==key) cout<<"\nElement not found";
return i; else
cout<<"\nElement found at index "<<i; cout<<"\nSORTED ELEMENT\t\t";
break; for(i=1;i<=n;i++)
case 3: cout<<hsort[i]<<endl;
ht.display(); getch();
break; }
case 4: void heapify()
cout<<"\nEnd of program"; {
default:"\nEnter the choice b/w 1and int i;
4"; for(i=n/2;i>=1;i--)
} adjust(i,n);
}while(ch!=4); }
} void adjust(int i,int n)
{
Heap Sort int j,element;
#include<iostream.h> j=2*i;
#include<conio.h> element=hsort[i];
int hsort[25],n,i; while(j<=n)
void adjust(int,int); {
void heapify(); if((j<n)&&(hsort[j]<hsort[j+1]))
void main() j=j++;
{ if(element>=hsort[j])
int temp; break;
clrscr(); hsort[j/2]=hsort[j];
cout<<"\n\t\t\t\tHEAP SORT"; j=2*j;
cout<<"\n\t\t\t\t**** ****\n\n\n"; }
cout<<"\nenter no of elements:"; hsort[j/2]=element;
cin>>n; }
cout<<"\nenter elements to be
sorted\t\t"; AIM: Write a C++ program to
for(i=1;i<=n;i++) implementation of operations on AVL Trees
cin>>hsort[i];
heapify(); #include<iostream.h>
for(i=n;i>=2;i--) #include<conio.h>
{ #include<alloc.h>
temp=hsort[1]; # include<stdlib.h>
hsort[1]=hsort[i]; class node
hsort[i]=temp; {
adjust(1,i-1); public:
} int data;
struct node *left,*right; switch(choice)
int ht; {
}; case 1:
class AVL cout<<"\n\nEnter the number of elements:
{ ";
node *root; cin>>n;
int height(node *); cout<<"\n\nEnter tree data: ";
node *rotate_right(node *); for(i=0;i<n;i++)
node *rotate_left(node *); {
node *RR(node *); cin>>x;
node *LL(node *); root=A.insert(root,x);
node *LR(node *); }
node *RL(node *); break;
int BF(node *); case 2:
public: cout<<"\n\nEnter the data to be insert:
AVL() ";
{ cin>>x;
root=NULL; A.insert(root,x);
} break;
node *insert(node *,int); case 3:
node *delet(node *,int); cout<<"\n\nEnter a data which u have to
void preorder (node *); delete: ";
void inorder(node *); cin>>x;
}; A.delet(root,x);
void main() break;
{ case 4:
AVL A; cout<<"\n\nPreorder Display:\n\n";
int x,n,i,choice; A.preorder(root);
node *root=NULL; cout<<"\n\nInorder Display:\n\n";
clrscr(); A.inorder(root);
cout<<"\n\n\t\t\t\t\t\t\t\t\t\t\t\t.... break;
CREATION OF AVL TREE "; }
do }while(choice!=5);
{ getch();
cout<<"\n\n MENU: "; }
cout<<"\n\n\t1.Create\n\n\t2.Insert\n\n\t node *AVL::insert(node *T,int x)
3.Delete\n\n\t4.Display\n\n\t5.Exit "; {
cout<<"\n\nEnter the choice: "; if(T==NULL)
cin>>choice; {SCIENCE AND ENGINEERING 73 {
T=new node; T->data=x; T->left=NULL; T- if(BF(T)==2)
>right=NULL; } if(BF(T->left)>=0)
else { T=LL(T);
if(x>T else
->data) T=LR(T); }
{T else if(x<T->data)
-
>right=insert(T {T->left=delet(T->left,x);
->right,x); if(BF(T)==-2)
if(BF(T->right)<=0)
if(BF(T)== T=RR(T);
-2) else
T=RL(T); }
{ else {
if(x>T if(T->right!=NULL)
->right {
->data) p=T->right;
while(p->left!=NULL)
T=RR(T); p=p->left;
else T->data=p->data;
T=RL(T); }}
else if(x<T->data) T->right=delet(T->right,p->data);
{T->left=insert(T->left,x);
if(BF(T)==2) if(BF(T)==2)
{ if(BF(T->left)>=0)
if(x<T->left->data) T=LL(T);
T=LL(T); else
else T=LR(T); }
T=LR(T); }}} else
T->ht=height(T); return(T->left);
return(T); } }
T->ht=height(T);
node *AVL::delet(node *T,int x) { return(T); }
node *p; int AVL::height(node *T) {
if(T==NULL) int LH,RH;
return(0); if(T==NULL)
else return(0);
if(x>T->data) if(T->left==NULL)
{T->right=delet(T->right,x); LH=0;
else x->ht=height(x);
LH=1+T->left->ht; y->ht=height(y);
if(T->right==NULL) return(y); }
RH=0; node *AVL::LL(node *T) {
else T=rotate_right(T);
RH=1+T->right->ht; return(T); }
if(LH>RH) node *AVL::RR(node *T) {
return (LH); T=rotate_left(T);
else return(T); }
return (RH); } node *AVL::LR(node *T) {T-
int AVL::BF(node *T) { >left=rotate_left(T->left);
int LH,RH; T=rotate_right(T);
if(T==NULL) return(T); }
return(0); node *AVL::RL(node *T) {T-
if(T->left==NULL) >right=rotate_right(T->right);
LH=0; T=rotate_left(T);
else return(T); }
LH=1+T->left->ht; void AVL::preorder(node *root) {
node *T=root;
if(T->right==NULL) if(T!=NULL)
RH=0; {
else cout<<"\n"<<T->data<<" [BF= "<<BF(T)<<"]
RH=1+T->right->ht; ";
preorder(T->left);
return(LH-RH); preorder(T->right);
} }
node *AVL::rotate_left(node *x) { }
node *y; void AVL::inorder(node *root)
y=x->right; {
x->right=y->left; node *T=root;
y->left=x; if(T!=NULL)
x->ht=height(x); {
y->ht=height(y); inorder(T->left);
return(y); } cout<<" \n"<<T->data<<" [BF=
node *AVL::rotate_right(node *x) { "<<BF(T)<<"]";
node *y; inorder(T->right);
y=x->left; }
x->left=y->right; }
y->right=x;

You might also like