Vous êtes sur la page 1sur 12

INDEX

Divesh Puri
7CS3,538
A2305207145

Signatur Remark
SN Date Program Name e s
 WAP to obtain GCD of two numbers
1   recursively    
 WAP to implement Binary Search
2   Recursively    
3    WAP to implement Tower of Hanoi problem    
4    WAP to implement Quick Search Algorithm    
5    WAP to implement Merge Sort Algorithm    
6    WAP to implement PRIM’s Algorithm    
7    WAP to implement KRUSKAL’s Algorithm    
WAP to demonstrate 4-Queen’s problem
8   Algorithm    
 WAP to demonstrate 8-Queen’s problem
9   Algorithm    
10     WAP to implement Dijkstra’s Algorithm    
11    WAP to demonstrate DES Technique    
12    WAP to demonstrate BFS Technique    
       
       
       
       
       
       
       
       
Program No. # 1
#include<iostream>

using namespace std;

int gcd(int a,int b)


{
if(b==0)
return a;
else
return(b,a%b);
}

int main()
{
int a,b;
cout<<"Enter two numbers for GCD : ";
cout<<"\n1. ";
cin>>a;
cout<<"\n2. ";
cin>>b;

if(a<0 || b<0)
cout<<"The GCD is : "<<(-1)*gcd(a,b);
else if(a<0 && b<0 || a>0 && b>0)
cout<<"The GCD is : "<<gcd(a,b);

return 0;
}
/*
OUTPUT

path>gcd.out
Enter two numbers for GCD
1. 30

2. 45
The GCD is : 30

path>gcd.out
Enter two numbers for GCD
1. -30

2. -45
The GCD is : 30

path>gcd.out
Enter two numbers for GCD
1. -30

2. 45
The GCD is : 30

path>gcd.out
Enter two numbers for GCD
1. -30

2. -45
The GCD is : 30

*/
Program No. #2
#include<iostream>

using namespace std;

int main()
{
int a[10],i,item;
void binary(int x[10],int beg,int end,int y);
cout<<"Binary Search for 10 numbers "<<endl;

for(i=0;i<10;i++)
{
cout<<"enter the "<<i+1<<"no.:";
cin>>a[i];
}

cout<<"\nEnter no. to be found:";


cin>>item;

binary(a,0,9,item);

return 0;
}

void binary(int x[10],int beg,int end,int y)


{
int mid,z;

if(beg<=end)
{
mid=(beg+end)/2;
if(x[mid]==y)
{
cout<<"number found"<<endl;
}
if(y<x[mid])
{
end=mid-1;
binary(x,beg,end,y);
}
else
{
beg=mid+1;
binary(x,beg,end,y);
}
}
else
cout<<"Number not found";
}

/*
OUTPUT
path>g++ bin_search_re.cpp -o bin_search_re.out
path>bin_search_re.out
Binary Search for 10 numbers
enter the 1no.:1
enter the 2no.:2
enter the 3no.:3
enter the 4no.:4
enter the 5no.:5
enter the 6no.:6
enter the 7no.:7
enter the 8no.:8
enter the 9no.:9
enter the 10no.:0

Enter no. to be found:10


Number not found

path>bin_search_re.out
Binary Search for 10 numbers
enter the 1no.:1
enter the 2no.:2
enter the 3no.:3
enter the 4no.:4
enter the 5no.:5
enter the 6no.:6
enter the 7no.:7
enter the 8no.:8
enter the 9no.:9
enter the 10no.:0

Enter no. to be found:1


Number found
*/
Program No. #3
#include<iostream>
#include<math.h>

using namespace std;

void hanoi(int disk,char from,char to,char aux)


{

if(disk == 1)
{
cout<<"Move disk from "<<from<<" to "<<to<<endl;
}
else
{
hanoi(disk-1,from,aux,to);
cout<<"Move disk from "<<from<<" to "<<to<<endl;
hanoi(disk-1,aux,to,from);
}
}

int main()
{
int disk;
double moves;

cout<<"Enter the number of Disks : ";


cin>>disk;

moves = pow(2,disk)-1;
cout<<"\nThe number of moves are : "<<moves<<endl;

hanoi(disk,'A','C','B');

return 0;
}

/*
OUTPUT

path>g++ tower_of_hanoi.cpp -o tower_of_hanoi

path>./tower_of_hanoi

Enter the number of Disks : 3

The number of moves are : 7


Move disk from A to C
Move disk from A to B
Move disk from C to B
Move disk from A to C
Move disk from B to A
Move disk from B to C
Move disk from A to C

path>./tower_of_hanoi
Enter the number of Disks : 4

The number of moves are : 15


Move disk from A to B
Move disk from A to C
Move disk from B to C
Move disk from A to B
Move disk from C to A
Move disk from C to B
Move disk from A to B
Move disk from A to C
Move disk from B to C
Move disk from B to A
Move disk from C to A
Move disk from B to C
Move disk from A to B
Move disk from A to C
Move disk from B to C
/*
Program No. #4
#include<iostream.h>

using namespace std;

int split(int a[],int lower,int upper)


{
int p,q,t;
p=lower+1;
q=upper;
int i=a[lower];
while(q>=p)
{
while(a[p]<i)
p++;
while(a[q]>i)
q--;
if(q>=p)
{
t=a[p];
a[p]=a[q];
a[q]=t;
p++;
q--;
}
}
t=a[lower];
a[lower]=a[q];
a[q]=t;
return q;
}

void quicksort(int a[],int lower,int upper)


{
int i;
if(upper>lower)
{
i=split(a,lower,upper);
quicksort(a,lower,i-1);
quicksort(a,i+1,upper);
}
}
int main()
{
int num[10];
int i;
cout<<"Enter 10 numbers :\n";
for(i=0;i<10;i++)
{
cout<<"Enter the "<<i+1<<" element : ";
cin>>num[i];
}
quicksort(num,0,9);
cout<<"\nAfter Sorting :\n";
for(i=0;i<10;i++)
cout<<num[i]<<"\n";
return 0;
}
OUTPUT
path>Quick_Sort
Enter 10 numbers:
Enter the 1 element : 99
Enter the 2 element : -1
Enter the 3 element : -50
Enter the 4 element : 4
Enter the 5 element : 88
Enter the 6 element : 5
Enter the 7 element : 5
Enter the 8 element : 1
Enter the 9 element : 0
Enter the 10 element : 43

After Sorting :
-50
-1
0
1
4
5
5
43
88
99

path>
Program No. #5
#include<iostream>

using namespace std;

void merge(int numbers[], int temp[], int left, int mid, int right)
{
int i, left_end, num_elements, tmp_pos;
left_end = mid - 1;
tmp_pos = left;
num_elements = right - left + 1;

while ((left <= left_end) && (mid <= right))


{
if (numbers[left] <= numbers[mid])
{
temp[tmp_pos] = numbers[left];
tmp_pos = tmp_pos + 1;
left = left +1;
}

else
{
temp[tmp_pos] = numbers[mid];
tmp_pos = tmp_pos + 1;
mid = mid + 1;
}
}

while (left <= left_end)


{
temp[tmp_pos] = numbers[left];
left = left + 1;
tmp_pos = tmp_pos + 1;
}

while (mid <= right)


{
temp[tmp_pos] = numbers[mid];
mid = mid + 1;
tmp_pos = tmp_pos + 1;
}

for (i=0; i <= num_elements; i++)


{
numbers[right] = temp[right];
right = right - 1;
}
}
void m_sort(int numbers[], int temp[], int left, int right)
{
int mid;

if (right > left)


{
mid = (right + left) / 2;
m_sort(numbers, temp, left, mid);
m_sort(numbers, temp, mid+1, right);
merge(numbers, temp, left, mid+1, right);
}
}

void mergeSort(int numbers[], int temp[], int array_size)

{
m_sort(numbers, temp, 0, array_size - 1);

int main()
{
int array[50],temp[50],n,i;

cout<<"Enter the size of the array : ";


cin>>n;

for(i=0;i<n;i++)
{
cout<<"Enter the "<<i+1<<" element : ";
cin>>array[i];
}

mergeSort(array,temp,n);

cout<<"The Array after Sorting\n\n";


for(i=0;i<n;i++)
{
cout<<"The element at "<<i+1<<" is : "<<array[i]<<endl;
}

return 0;
}
OUTPUT
path>g++ MergeSort.cpp -o MergeSort

path>MergeSort
Enter the size of the array : 8
Enter the 1 element : 5
Enter the 2 element : 2
Enter the 3 element : 4
Enter the 4 element : 6
Enter the 5 element : 1
Enter the 6 element : 3
Enter the 7 element : 2
Enter the 8 element : 6
The Array after Sorting

The element at 1 is : 1
The element at 2 is : 2
The element at 3 is : 2
The element at 4 is : 3
The element at 5 is : 4
The element at 6 is : 5
The element at 7 is : 6
The element at 8 is : 6

path>

Vous aimerez peut-être aussi