Академический Документы
Профессиональный Документы
Культура Документы
//Class:M.Sc.I(Sem I)
#include <iostream.h>
#include <stdlib.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
// Output:
1 2 3 4 5 6 7 8
/* NAME: Mohd Majid */
/* CLASS:M.Sc.I (Sem I) */
/* Program to search an element in an array using Binary search.*/
# include <iostream.h>
# include <conio.h>
else if(element>array[middle])
start=middle+1;
middle=(start+end)/2;
}
while(start<=end && array[middle]!=element);
if(array[middle]==element)
position=middle;
return position;
}
// Output:
******************************* Binary Search ******************************
The contents of the array are :
Elements : Value:
array [0] 0
array [1] 6
array [2] 9
array [3] 12
array [4] 20
array [5] 23
array [6] 29
array [7] 32
array [8] 47
array [9] 79
#include <iostream.h>
#include<conio.h>
#include<stdio.h>
#define TRUE 1
#define FALSE 0
// Output:
1 2 4 8 5 6 3 7
/* NAME:Mohd Majid */
/* CLASS: M.Sc.I (Sem I)*/
/* Program that sorts an array using Heap Sort method*/
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
const int MAX = 10 ;
class array
{
private :
int arr[MAX] ;
int count ;
public :
array( ) ;
void add ( int num ) ;
void makeheap( ) ;
void heapsort( ) ;
void display( ) ;
} ;
array :: array( )
{
count = 0 ;
for ( int i = 0 ; i < MAX ; i++ )
arr[MAX] = 0 ;
}
void array :: add ( int num )
{
if ( count < MAX )
{
arr[count] = num ;
count++ ;
}
else
cout << "\nArray is full" << endl ;
}
void array :: makeheap( )
{
for ( int i = 1 ; i < count ; i++ )
{
int val = arr[i] ;
int s = i ;
int f = ( s - 1 ) / 2 ;
while ( s > 0 && arr[f] < val )
{
arr[s] = arr[f] ;
s = f ;
f = ( s - 1 ) / 2 ;
}
arr[s] = val ;
}
}
void array :: heapsort( )
{
for ( int i = count - 1 ; i > 0 ; i-- )
{
int ivalue = arr[i] ;
arr[i] = arr[0] ;
int f = 0 ;
int s ;
if ( i == 1 )
s = -1 ;
else
s = 1 ;
if ( i > 2 && arr[2] > arr[1] )
s = 2 ;
while ( s >= 0 && ivalue < arr[s] )
{
arr[f] = arr[s] ;
f = s ;
s = 2 * f + 1 ;
if ( s + 1 <= i - 1 && arr[s] < arr[s + 1] )
s++ ;
if ( s > i - 1 )
s = -1 ;
}
arr[f] = ivalue ;
}
}
void array :: display( )
{
for ( int i = 0 ; i < count ; i++ )
cout << arr[i] << "\t" ;
cout << endl ;
}
void main( )
{
array a ;
clrscr();
a.add ( 11 ) ;
a.add ( 2 ) ;
a.add ( 9 ) ;
a.add ( 13 ) ;
a.add ( 57 ) ;
a.add ( 25 ) ;
a.add ( 17 ) ;
a.add ( 1 ) ;
a.add ( 90 ) ;
a.add ( 3 ) ;
a.makeheap( ) ;
cout << "\nHeap Sort.\n" ;
cout << "\nBefore Sorting:\n" ;
a.display( ) ;
a.heapsort( ) ;
cout << "\nAfter Sorting:\n" ;
a.display( ) ;
}
// Output:
Heap Sort.
Before Sorting:
90 57 25 13 11 9 17 1 2 3
After Sorting:
1 2 3 9 11 13 17 25 57 90
/*NAME:Mohd Majid */
/*CLASS:M.Sc.I (SemI)*/
/* Program for merge sort*/
# include <iostream.h>
# include <conio.h>
// Output:
# include <iostream.h>
# include <conio.h>
// Output:
******************************** Quick Sort ******************************
* Array size = 10
* Data Type = long
# include <iostream.h>
# include <conio.h>
# include<iostream.h>
# include<process.h>
# include<conio.h>
# define SIZE 20
class stack
{
int a[SIZE];
int tos;
public:
stack();
void push(int);
int pop();
int isempty();
int isfull();
};
stack::stack()
{
tos=0;
}
int stack::isempty()
{
return (tos==0?1:0);
}
int stack::isfull()
{
return (tos==SIZE?1:0);
}
void stack::push(int i)
{
if(!isfull())
{
a[tos]=i;
tos++;
}
else
{
cerr<<"Stack overflow error ! Possible Data Loss !";
}
}
int stack::pop()
{
if(!isempty())
{
return(a[--tos]);
}
else
{
cerr<<"Stack is empty! What to pop...!";
}
return 0;
}
void main()
{
stack s;
clrscr();
int ch=1,num;
while(ch!=0)
{
cout<<"Stack Operations Main Menu:\n";
cout<<" 1.Push";
cout<<" \n 2.Pop";
cout<<" \n 3.IsEmpty";
cout<<"\n 4.IsFull";
cout<<"\n 0.Exit";
cout<<"\nEnter Your Choice:" ;
cin>>ch;
switch(ch)
{
case 0:
exit(1);
case 1:
cout<<"Enter the number to push";
cin>>num;
s.push(num);
break;
case 2:
cout<<"Number popped from the stack is: "<<s.pop()<<endl;
break;
case 3:
(s.isempty())?(cout<<"Stack is empty."):(cout<<"Stack is not
empty.");
break;
case 4:
(s.isfull())?(cout<<"Stack is full."):(cout<<"Stack is not full.");
break;
default:
cout<<"Illegal Option.Please try again";
}
}
getch();
}
// Output:
Stack Operations Main Menu:
1.Push
2.Pop
3.IsEmpty
4.IsFull
0.Exit
Enter Your Choice:1
Enter the number to push34
Stack Operations Main Menu:
1.Push
2.Pop
3.IsEmpty
4.IsFull
0.Exit
Enter Your Choice:2
Number popped from the stack is: 34
Stack Operations Main Menu:
1.Push
2.Pop
3.IsEmpty
4.IsFull
0.Exit
Enter Your Choice:3
Stack is empty.Stack Operations Main Menu:
1.Push
2.Pop
3.IsEmpty
4.IsFull
0.Exit