Ds Lab 8 To End

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Ex.No:9.

a Linear Search

#include <stdio.h>
void main()
{
int num;
int i, keynum, found = 0;
printf("Enter the number of elements ");
scanf("%d", &num);
int array[num];
printf("Enter the elements one by one \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}

printf("Enter the element to be searched ");


scanf("%d", &keynum);
for (i = 0; i < num ; i++)
{
if (keynum == array[i] )
{
found = 1;
break;
}
}
if (found == 1)
printf("Element is present in the array at position %d",i+1);
else
printf("Element is not present in the array\n");
}

Output:
Ex.No:9 b Binary Search

#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);
return 0;
}
Output:
Ex.No:10.a Depth First Search

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{

struct node *next;


int vertex;
}node;
node *G[20];
//heads of linked list
int visited[20];
int n;
void read_graph();
//create adjacency list
void insert(int,int);
//insert an edge (vi,vj) into adjacency list
void DFS(int);
void main()
{
int i;
read_graph();
//initialised visited to 0
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
node *p;
printf("\n%d",i);
p=G[i];
visited[i]=1;
while(p!=NULL)
{
i=p->vertex;
if(!visited[i])
DFS(i);
p=p->next;
}
}
void read_graph()
{
int i,vi,vj,no_of_edges;
printf("Enter number of vertices:");
scanf("%d",&n);
//initialise G[] with a null
for(i=0;i<n;i++)
{
G[i]=NULL;
//read edges and insert them in G[]
printf("Enter number of edges:");
scanf("%d",&no_of_edges);
for(i=0;i<no_of_edges;i++)
{
printf("Enter an edge(u,v):");
scanf("%d%d",&vi,&vj);
insert(vi,vj);
}
}
}
void insert(int vi,int vj)
{
node *p,*q;
//acquire memory for the new node
q=(node*)malloc(sizeof(node));
q->vertex=vj;
q->next=NULL;
//insert the node in the linked list number vi
if(G[vi]==NULL)
G[vi]=q;
else
{
//go to end of the linked list
p=G[vi];
while(p->next!=NULL)
p=p->next;
p->next=q;
}
}

OUTPUT:
Ex.No:10.b Breadth Firts Search

#include<stdio.h>
#include<stdlib.h>
#define MAX 100
#define initial 1
#define waiting 2
#define visited 3
int n;
int adj[MAX][MAX];
int state[MAX];
void create_graph();
void BF_Traversal();
void BFS(int v);
int queue[MAX], front = -1,rear = -1;
void insert_queue(int vertex);
int delete_queue();
int isEmpty_queue();
int main()
{
create_graph();
BF_Traversal();
return 0;
}
void BF_Traversal()
{
int v;
for(v=0; v<n; v++)
state[v] = initial;
printf("Enter Start Vertex for BFS:\n");
scanf("%d", &v);
BFS(v);
}
void BFS(int v)
{
int i;
insert_queue(v);
state[v] = waiting;
while(!isEmpty_queue())
{
v = delete_queue( );
printf("%d",v);
state[v] = visited;
for(i=0; i<n; i++)
{

if(adj[v][i]== 1 && state[i] == initial)


{
insert_queue(i);
state[i]= waiting;
}
}
}
printf("\n");
}
void insert_queue(int vertex)
{
if(rear == MAX-1)
printf("Queue Overflow\n");
else
{
if(front == -1)
front= 0;
rear = rear+1;
queue[rear] = vertex ;
}
}
int isEmpty_queue()
{
if(front == -1 || front > rear)
return 1;
else
return 0;
}
int delete_queue()
{
int delete_item;
if(front == -1 || front > rear)
{
printf("Queue Underflow\n");
exit(1);
}
delete_item = queue[front];
front = front+1;
return delete_item;
}
void create_graph()
{
int count,max_edge,origin,destin;
printf("Enter number of vertices : ");
scanf("%d",&n);
max_edge = n*(n-1);
for(count=1; count<=max_edge; count++)
{
printf("Enter edge %d( -1 -1 to quit ) : ",count);
scanf("%d%d",&origin,&destin);
if((origin == -1)&& (destin == -1))
break;
if(origin>=n ||destin>=n || origin<0 || destin<0)
{
printf("Invalid edge!\n");
count--;
}
else
{
adj[origin][destin]= 1;
}
}
}
Output:
Ex.No:11 HASHING

#include<stdio.h>
#include<conio.h>
#include<math.h>
struct data
{
int key;
int value;
};

struct hashtable_item
{
int flag;
struct data *item;

};
struct hashtable_item *array;
int max = 7;
int size = 0;
int prime = 3;

int hashcode1(int key)


{
return (key % max);
}

int hashcode2(int key)


{
return (prime - (key % prime));
}

void insert(int key, int value)


{
int hash1 = hashcode1(key);
int hash2 = hashcode2(key);

int index = hash1;

/* create new data to insert */


struct data *new_item = (struct data*) malloc(sizeof(struct data));
new_item->key = key;
new_item->value = value;

if (size == max)
{
printf("\n Hash Table is full, cannot insert more items \n");
return;
}
/* probing through other array elements */
while (array[index].flag == 1) {
if (array[index].item->key == key)
{
printf("\n Key already present, hence updating its value \n");
array[index].item->value = value;
return;
}
index = (index + hash2) % max;
if (index == hash1)
{
printf("\n Add is failed \n");
return;
}
printf("\n probing \n");

array[index].item = new_item;
array[index].flag = 1;
size++;
printf("\n Key (%d) has been inserted \n", key);

/* to remove an element from the array */


void remove_element(int key)
{
int hash1 = hashcode1(key);
int hash2 = hashcode2(key);
int index = hash1;

if (size == 0)
{
printf("\n Hash Table is empty \n");
return;
}

/* probing through other elements */


while (array[index].flag != 0)
{
if (array[index].flag == 1 && array[index].item->key == key)
{
array[index].item = NULL;
array[index].flag = 2;
size--;
printf("\n Key (%d) has been removed \n", key);
return;
}
index = (index + hash2) % max;
if (index == hash1)
{
break;
}
}

printf("\n Key (%d) does not exist \n", key);

int size_of_hashtable()
{
return size;
}

/* displays all elements of array */


void display()
{
int i;
for (i = 0; i < max; i++)
{
if (array[i].flag != 1)
{
printf("\n Array[%d] has no elements \n", i);
}
else
{
printf("\n Array[%d] has elements \n Key (%d) and Value (%d) \n", i,
array[i].item->key, array[i].item->value);
}
}
}

/* initializes array */
void init_array()
{
int i;
for(i = 0; i < max; i++)
{
array[i].item = NULL;
array[i].flag = 0;
}
prime = get_prime();
}

/* returns largest prime number less than size of array */


int get_prime()
{
int i,j;
for (i = max - 1; i >= 1; i--)
{
int flag = 0;
for (j = 2; j <= (int)sqrt(i); j++)
{
if (i % j == 0)
{
flag++;
}
}
if (flag == 0)
{
return i;
}
}
return 3;

void main()
{
int choice, key, value, n, c;
clrscr();

array = (struct hashtable_item*) malloc(max * sizeof(struct hashtable_item));


init_array();

do {
printf("Implementation of Hash Table in C with Double Hashing.\n\n");
printf("MENU-: \n1.Inserting item in the Hash Table"
"\n2.Removing item from the Hash Table"
"\n3.Check the size of Hash Table"
"\n4.Display Hash Table"
"\n\n Please enter your choice-:");

scanf("%d", &choice);

switch(choice)
{

case 1:

printf("Inserting element in Hash Table\n");


printf("Enter key and value-:\t");
scanf("%d %d", &key, &value);
insert(key, value);

break;

case 2:
printf("Deleting in Hash Table \n Enter the key to delete-:");
scanf("%d", &key);
remove_element(key);

break;

case 3:

n = size_of_hashtable();
printf("Size of Hash Table is-:%d\n", n);

break;

case 4:

display();
break;
default:
printf("Wrong Input\n");
}
printf("\n Do you want to continue-:(press 1 for yes)\t");
scanf("%d", &c);

}while(c == 1);
}

OUTPUT:
Enter no of elements:
6
Enter Key values:
11
21
22
32
43
44
output:
Index Hashtablevalue:
------------------------:
Index=1
Index=1
Index=2
Index=2
Index=3
Index=4
0 0
1 11
2 21
3 22
4 32
5 43
6 44
7 0
8 0
9 0
Ex.no:12 AVL Tree

#include<stdio.h>
typedef struct node
{
int data;
struct node *left,*right;
int ht;
}node;

node *insert(node *,int);


node *Delete(node *,int);
void preorder(node *);
void inorder(node *);
int height( node *);
node *rotateright(node *);
node *rotateleft(node *);
node *RR(node *);
node *LL(node *);
node *LR(node *);
node *RL(node *);
int BF(node *);

int main()
{
node *root=NULL;
int x,n,i,op;

do
{
printf("\n1)Create:");
printf("\n2)Insert:");
printf("\n3)Delete:");
printf("\n4)Print:");
printf("\n5)Quit:");
printf("\n\nEnter Your Choice:");
scanf("%d",&op);

switch(op)
{
case 1: printf("\nEnter no. of elements:");
scanf("%d",&n);
printf("\nEnter tree data:");
root=NULL;
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);
}
break;
case 2: printf("\nEnter a data:");
scanf("%d",&x);
root=insert(root,x);
break;

case 3: printf("\nEnter a data:");


scanf("%d",&x);
root=Delete(root,x);
break;

case 4: printf("\nPreorder sequence:\n");


preorder(root);
printf("\n\nInorder sequence:\n");
inorder(root);
printf("\n");
break;
}
}while(op!=5);

return 0;
}

node * insert(node *T,int x)


{
if(T==NULL)
{
T=(node*)malloc(sizeof(node));
T->data=x;
T->left=NULL;
T->right=NULL;
}
else
if(x > T->data) // insert in right subtree
{
T->right=insert(T->right,x);
if(BF(T)==-2)
if(x>T->right->data)
T=RR(T);
else
T=RL(T);
}
else
if(x<T->data)
{
T->left=insert(T->left,x);
if(BF(T)==2)
if(x < T->left->data)
T=LL(T);
else
T=LR(T);
}

T->ht=height(T);

return(T);
}

node * Delete(node *T,int x)


{
node *p;

if(T==NULL)
{
return NULL;
}
else
if(x > T->data) // insert in right subtree
{
T->right=Delete(T->right,x);
if(BF(T)==2)
if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);
}
else
if(x<T->data)
{
T->left=Delete(T->left,x);
if(BF(T)==-2) //Rebalance during windup
if(BF(T->right)<=0)
T=RR(T);
else
T=RL(T);
}
else
{
//data to be deleted is found
if(T->right!=NULL)
{ //delete its inorder succesor
p=T->right;

while(p->left!= NULL)
p=p->left;

T->data=p->data;
T->right=Delete(T->right,p->data);

if(BF(T)==2)//Rebalance during windup


if(BF(T->left)>=0)
T=LL(T);
else
T=LR(T);\
}
else
return(T->left);
}
T->ht=height(T);
return(T);
}

int height(node *T)


{
int lh,rh;
if(T==NULL)
return(0);

if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;

if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;

if(lh>rh)
return(lh);

return(rh);
}

node * rotateright(node *x)


{
node *y;
y=x->left;
x->left=y->right;
y->right=x;
x->ht=height(x);
y->ht=height(y);
return(y);
}

node * rotateleft(node *x)


{
node *y;
y=x->right;
x->right=y->left;
y->left=x;
x->ht=height(x);
y->ht=height(y);

return(y);
}

node * RR(node *T)


{
T=rotateleft(T);
return(T);
}

node * LL(node *T)


{
T=rotateright(T);
return(T);
}

node * LR(node *T)


{
T->left=rotateleft(T->left);
T=rotateright(T);

return(T);
}

node * RL(node *T)


{
T->right=rotateright(T->right);
T=rotateleft(T);
return(T);
}

int BF(node *T)


{
int lh,rh;
if(T==NULL)
return(0);

if(T->left==NULL)
lh=0;
else
lh=1+T->left->ht;

if(T->right==NULL)
rh=0;
else
rh=1+T->right->ht;
return(lh-rh);
}

void preorder(node *T)


{
if(T!=NULL)
{
printf("%d(Bf=%d)",T->data,BF(T));
preorder(T->left);
preorder(T->right);
}
}

void inorder(node *T)


{
if(T!=NULL)
{
inorder(T->left);
printf("%d(Bf=%d)",T->data,BF(T));
inorder(T->right);
}
}

Output:
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:1
Enter no. of elements:4
Enter tree data:7 12 4 9
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:4
Preorder sequence:
7(Bf=-1)4(Bf=0)12(Bf=1)9(Bf=0)
Inorder sequence:
4(Bf=0)7(Bf=-1)9(Bf=0)12(Bf=1)
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:3
Enter a data:7
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:4
Preorder sequence:
9(Bf=0)4(Bf=0)12(Bf=0)
Inorder sequence:
4(Bf=0)9(Bf=0)12(Bf=0)
1)Create:
2)Insert:
3)Delete:
4)Print:
5)Quit:
Enter Your Choice:5

Ex.No:13 a Insertion sort


#include<stdio.h>
int main()
{
int data[100],n,temp,i,j;
printf("Enter number of terms(should be less than 100): ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&data[i]);
}
for(i=1;i<n;i++)
{
temp = data[i];
j=i-1;
while(temp<data[j] && j>=0)
/*To sort elements in descending order, change temp<data[j] to temp>data[j] in above
line.*/
{
data[j+1] = data[j];
--j;
}
data[j+1]=temp;
}
printf("In ascending order: ");
for(i=0; i<n; i++)
printf("%d\t",data[i]);
return 0;
}

Output:

Ex.No:13 b. Selection sort


#include<stdio.h>
#include<conio.h>
void main(){
int size,i,j,temp,list[100];
clrscr();
printf("Enter the size of the List: ");
scanf("%d",&size);
printf("Enter %d integer values: ",size);
for(i=0; i<size; i++)
scanf("%d",&list[i]);
for(i=0; i<size; i++){
for(j=i+1; j<size; j++){
if(list[i] > list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}
printf("List after sorting is: ");
for(i=0; i<size; i++)
printf(" %d",list[i]);
getch();
}

Output:

Ex.No:13.C Merge Sort


#include<stdio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
int main()
{
int a[30],n,i;
printf("\nEnter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");

for(i=0;i<n;i++)
scanf("%d",&a[i]);

mergesort(a,0,n-1);

printf("\nSorted array is :");


for(i=0;i<n;i++)
printf("%d ",a[i]);

return 0;
}

void mergesort(int a[],int i,int j)


{
int mid;

if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid); //left recursion
mergesort(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}

void merge(int a[],int i1,int j1,int i2,int j2)


{
int temp[50]; //array used for merging
int i,j,k;
i=i1; //beginning of the first list
j=i2; //beginning of the second list
k=0;

while(i<=j1 && j<=j2) //while elements in both lists


{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1) //copy remaining elements of the first list
temp[k++]=a[i++];

while(j<=j2) //copy remaining elements of the second list


temp[k++]=a[j++];

//Transfer elements from temp[] back to a[]


for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}

OUTPUT
Enter no of elements:6
Enter array elements:3
6
7
2
8
9

Sorted array is :2 3 6 7 8 9

Ex.No:13.b Bubble Sort


#include <stdio.h>
#include<conio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
getch();
}

Output:

Ex.No:14 Binary Heap


#include<stdio.h>
int size=0,curr;
void swap(int *a,int *b) {
int temp=*b;
*b=*a;
*a=temp;
}
void heapify(int array[],int size,int i){
if(size==1){
printf("\nSingle element in the list!");
}
else{
int largest=i;
int l=(2*i)+1;
int r=(2*i)+2;
if(l<size && array[l]>array[largest])
largest=l;
if(r<size && array[r]>array[largest])
largest=r;
if(largest!=i) {
swap(&array[i],&array[largest]);
heapify(array,size,largest);
}
}
}
void insert(int array[],int newNum){
if(size==0){
array[0]=new
printf("Element inserted!!!");
size+=1;
}
else{
array[size]=newNum;
printf("Element inserted!!!");
size+=1;
for(int i=size-1;i>=0;i--){
heapify(array,size,i);
}
}
}
void delete(int array[],int num){
if(size==0) {
printf("Queue is Empty"); }
for(int i=0;i<size;i++){
if(num==array[i]) {
curr=i;
break; }
}
swap(&array[curr],&array[size-1]);
size-=1;
printf("Element deleted");
for(int i=size-1;i>=0;i--){
heapify(array,size,i);
}
}
void display(int array[],int size){
if(size==0) {
printf("Queue is Empty"); }
for(int i=0;i<size;i++){
printf("%d ",array[i]);
}
}
void peek(int array[]){
printf("%d",array[0]);
}
void extractMax(int array[]){
printf("%d",array[0]);
swap(&array[0],&array[size-1]);
size-=1;
for(int i=size-1;i>=0;i--){
heapify(array,size,i);
}
}
int main() {
int array[20],choice,data;
while(choice!=6){
printf("****MAX HEAP****");
printf("\n1.Insert 2.Delete 3.Display 4.Peek 5.ExtractMax 6.Quit");
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice){
case 1:
printf("Enter the element to be inserted : ");
scanf("%d",&data);
insert(array,data);
break;
case 2:
printf("Enter the element to be deleted : ");
scanf("%d",&data);
delete(array,data);
break;
case 3:
printf("The elements are : ");
display(array,size);
break;
case 4:
printf("The peek element is : ");
peek(array);
break;
case 5:
printf("The maximum element is : ");
extractMax(array);
break;
case 6:
return 0;
default:
printf("Invalid choice!");
}
printf("\n\n");
}
}

OUTPUT :

****MAX
HEAP****
1.Insert 2.Delete 3.Display 4.Peek 5.ExtractMax
6.Quit
Enter your choice :
1
Enter the element to be inserted :
4
Element
inserted!!!

****MAX
HEAP****
1.Insert 2.Delete 3.Display 4.Peek 5.ExtractMax
6.Quit
Enter your choice :
1
Enter the element to be inserted :
7
Element
inserted!!!

****MAX
HEAP****
1.Insert 2.Delete 3.Display 4.Peek 5.ExtractMax
6.Quit
Enter your choice :
1
Enter the element to be inserted :
8
Element
inserted!!!
****MAX
HEAP****
1.Insert 2.Delete 3.Display 4.Peek 5.ExtractMax
6.Quit
Enter your choice :
1
Enter the element to be inserted :
3
Element inserted!!!

****MAX
HEAP****
1.Insert 2.Delete 3.Display 4.Peek 5.ExtractMax
6.Quit
Enter your choice :
1
Enter the element to be inserted :
10
Element
inserted!!!

****MAX
HEAP****
1.Insert 2.Delete 3.Display 4.Peek 5.ExtractMax
6.Quit
Enter your choice :
3
The elements are : 10 8 7 3
4

****MAX
HEAP****
1.Insert 2.Delete 3.Display 4.Peek 5.ExtractMax
6.Quit
Enter your choice :
2
Enter the element to be deleted :
7
Element
deleted

****MAX
HEAP****
1.Insert 2.Delete 3.Display 4.Peek 5.ExtractMax
6.Quit
Enter your choice :
3
The elements are : 10 8 4 3
****MAX
HEAP****
1.Insert 2.Delete 3.Display 4.Peek 5.ExtractMax
6.Quit
Enter your choice :
4
The peek element is :
10

****MAX
HEAP****
1.Insert 2.Delete 3.Display 4.Peek 5.ExtractMax
6.Quit
Enter your choice :
5
The maximum element is :
10

****MAX
HEAP****
1.Insert 2.Delete 3.Display 4.Peek 5.ExtractMax
6.Quit
Enter your choice :
3
The elements are : 8 3 4

You might also like