Important Programs of DSA
Important Programs of DSA
Himashree N R 1RV22CS069 4B
1. Binary search(iterative method)-
2. // Binary Search in C
3.
4. #include <stdio.h>
5.
6. int binarySearch(int array[], int x, int low, int high) {
7. // Repeat until the pointers low and high meet each other
8. while (low <= high) {
9. int mid = low + (high - low) / 2;
10.
11. if (array[mid] == x)
12. return mid;
13.
14. if (array[mid] < x)
15. low = mid + 1;
16.
17. else
18. high = mid - 1;
19. }
20.
21. return -1;
22. }
23.
24. int main(void) {
25. int n,arr[10],i;
26. int x;
27. printf("\nEnter the size of the array:");
28. scanf("%d",&n);
29. printf("\nEnter the elements of the array\n");
30. for(i=0;i<n;i++)
31. scanf("%d",&arr[i]);
32. printf("\nEnter the number to be searched:");
33. scanf("%d",&x);
34. int result = binarySearch(arr, x, 0, n - 1);
35. if (result == -1)
36. printf("\nElement not found\n");
37. else
38. printf("\nElement found at position %d\n", result + 1);
39. return 0;
40.
41. }
Binary search in recursive method-
// Binary Search in C
#include <stdio.h>
return -1;
}
int main(void) {
int n,arr[10],i;
int x;
printf("\nEnter the size of the array:");
scanf("%d",&n);
printf("\nEnter the elements of the array\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("\nEnter the number to be searched:");
scanf("%d",&x);
int result = binarySearch(arr, x, 0, n - 1);
if (result == -1)
printf("\nElement not found\n");
else
printf("\nElement found at position %d\n", result + 1);
return 0;
}
2. LINEAR SEARCH(iterative method)-
// Linear Search in C
#include <stdio.h>
int main() {
int n,arr[10],i;
printf("\nEnter the value of n");
scanf("%d",&n);
printf("\nEnter the array elements");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("\nEnter the element to be searched");
int x;
scanf("%d",&x);
int result = search(arr, n, x);
if (result == -1)
printf("\nElement not found");
else
printf("\nElement found at index %d", result);
return 0;
}
Linear Search(recursive method)-
// Driver code
int main()
{
int n,arr[10],i;
printf("\nEnter the value of n");
scanf("%d",&n);
printf("\nEnter the array elements");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("\nEnter the element to be searched");
int x;
scanf("%d",&x);
int result = linearSearch(arr, n, x);
if (result == -1)
printf("\nElement not found");
else
printf("\nElement found at index %d", result);
return 0;
}
3. Bubble Sort-
// Swap function
void swap(int* arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// Driver code
int main()
{
int n,arr[10],i;
printf("Enter the size of the array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("\nSorted array is");
bubbleSort(arr, n);
printArray(arr, n);
return 0;
}
return result;
}
// Driver code
int main()
{
int n;
printf("\nEnter the value of n:");
scanf("%d",&n);
printf("\nThe factorial of %d is %d", n, factorial(n));
return 0;
}
// Driver code
int main()
{
int n;
printf("\nEnter the value of n:");
scanf("%d",&n);
printf("\nThe factorial of %d is %d", n, factorial(n));
return 0;
}
// function that handles the first two terms and calls the
// recursive function
void printFib(int n)
{
// when the number of terms is less than 1
if (n < 1) {
printf("Invalid number of terms\n");
}
// when the number of terms is 1
else if (n == 1) {
printf("%d ", 0);
}
// when the number of terms is 2
else if (n == 2) {
printf("%d %d", 0, 1);
}
// number of terms greater than 2
else {
printf("%d %d ", 0, 1);
fib(n);
}
return;
}
// driver code
int main()
{
int n;
printf("\nEnter the value of n:");
scanf("%d", &n);
printFib(n);
return 0;
}
// driver code
int main()
{
int n;
printf("\nEnter the value of n:");
scanf("%d", &n);
printFib(n);
return 0;
6. Selection Sort-
#include <stdio.h>
int main() {
int n;
double arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);
return 0;
}
8. Polynomial Multiplication-
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
int coeff;
int exp;
struct node *next;
};
struct node *insert(struct node *head,int co,int ex)
{
struct node *temp;
struct node *new=(struct node *)malloc(sizeof(struct node));
new->coeff=co;
new->exp=ex;
new->next=NULL;
if(head==NULL || ex>head->exp)
{
new->next=head;
head=new;
}
else{
temp=head;
while(temp->next!=NULL && temp->next->exp!=ex)
{
temp=temp->next;
}
new->next=temp->next;
temp->next=new;
}
return head;
}
struct node *create(struct node *head)
{
int n,i;
int coeff,exp;
printf("\nEnter the number of terms in the polynomial:");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("\nEnter the coefficient for the term %d",i+1);
scanf("%d",&coeff);
printf("\nEnter the exponent for the term %d",i+1);
scanf("%d",&exp);
head=insert(head,coeff,exp);
}
return head;
}
void print(struct node *head)
{
struct node *temp;
temp=head;
while(temp->next!=NULL)
{
printf("\n%dx^%d+",temp->coeff,temp->exp);
temp=temp->next;
}
printf("\n%dx^%d+",temp->coeff,temp->exp);
}
void multi(struct node *head1,struct node *head2)
{
9. Double hashing-
#include <stdio.h>
#include <stdlib.h>
#define TABLE_SIZE 10
int h[TABLE_SIZE]={NULL};
void insert()
{
int i,h1,h2,key,index,flag=0;
printf("\nEnter the key to be inserted:");
scanf("%d",&key);
h1=key%TABLE_SIZE;
h2=7-(key%7);
for(i=0;i<TABLE_SIZE;i++)
{
index=(h1=i*h2)%TABLE_SIZE;
if(h[index]==NULL)
{
h[index]=key;
break;
}
}
if(i==TABLE_SIZE)
{
printf("\nValue cannot be inserted");
}
}
void search()
{
int i,index,h1,h2,key,flag=0;
printf("\nEnter the key to be searched:");
scanf("%d",&key);
h1=key%TABLE_SIZE;
h2=7-(key%7);
for(i=0;i<TABLE_SIZE;i++)
{
index=(h1+i*h2)%TABLE_SIZE;
if(h[index]==key)
{
printf("\nKey found at index %d",index);
}
}
if(i==TABLE_SIZE)
{
printf("\nValue doesn't found");
}
}
void display()
{
int i;
printf("\nElements of the hash table are\n");
for(i=0;i<TABLE_SIZE;i++)
{
printf("%d found at the position %d",h[i],i);
}
}
main()
{
int opt,i;
while(1)
{
printf("\nPress 1.Insert\t2.Search\t3.Display\t4.Exit\n");
printf("\nEnter the option:");
scanf("%d",&opt);
switch(opt)
{
case 1:
{
insert();
break;
}
case 2:
{
seach();
break;
}
case 3:
{
display();
break;
}
case 4:
exit(0);
}
}
}
#include<stdio.h>
#define max 10
for(i=0;i<index ;i++)
{
if(HT[i] == -1)
{
HT[i] =key;
printf("\nSuccesful Insertion");
return;
}
}
for(i=0;i<index ;i++)
{
if(HT[i] == key)
{
HT[i] =-1;
printf("\nSuccesful Deletion");
return;
}
}
while(1)
{
printf("\nEnter the choice \n1 : insert \n2 : delete\n3 : display\n4
: exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\nEnter the key to be inerted :");
scanf("%d",&key);
insert_LinerProbing(HT,key);
break;
case 2: printf("\nEnter the key to be inerted :");
scanf("%d",&key);
Delete_LinerProbing(HT,key);
break;
case 3: display(HT);
break;
default:exit(0);
}
11. Heapsort-
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int heap=0,j,key,k,i;
for (i=n/2;i>=1;i--)
{
k=i;
key=a[i]; // v is the key value
heap=0;
while (!heap && 2*k<=n)
{
j=2*k;
if (j<n)
{
if (a[j]<a[j+1])
j=j+1;
}
if (key>=a[j])
heap=1; // heapification ends
else
{
a[k]=a[j]; // shift a[j] up ie to parents position
k=j;
}
}//end of while
a[k]=key;
}//end of for
}
//Intial Heap
heapify(a,n);
// sorting logic
for (i=n; i>1 ;i--) //uxing max deletion logic
{
t=a[i];
a[i]=a[1];
a[1]=t;
heapify(a,i-1);
}
}
int main()
{
int a[100],n,i;
printf("Enter no. of elements:");
scanf("%d",&n);
printf("\nEnter %d elements:\n",n);
for (i=1;i<=n;i++)
scanf("%d",&a[i]);
HeapSort(a,n);
printf("\nSorted array is \n\n");
for (i=1;i<=n;i++)
printf("%d ",a[i]);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* BST node */
struct node
{
int data;
struct node *left_child;
struct node *right_child;
};
q= getnode();
q->data =x;
if(root == NULL)
return q;
p= root;
while(p !=NULL)
{
parent = p;
if(x == p->data )
{
printf("Duplicate Entry");
free(q);
return root;
}
if( x < p->data)
p=p->left_child;
else
p=p->right_child;
}
if(x < parent->data)
parent->left_child = q;
else
parent->right_child = q;
return root;
}
NODE BSTDelete(NODE tp, int x)
{
int y;
NODE r;
if (tp == NULL)
{
printf(" Element %d Not Presenst",x);
return NULL; //tp
}
if(x == tp->data )
{
//Case 1
if( tp->left_child == NULL && tp->right_child==NULL)
{
free(tp);
return NULL;
}
//Case 2 : left subtree is NULL
if(tp->left_child == NULL)
{
r= tp->right_child;
free(tp);
return r;
}
//Case 2 : right subtree is NULL
if(tp->right_child == NULL)
{
r= tp->left_child;
free(tp);
return r;
}
// Case 3: both left && right NOT NULL
y = InOrderSucc(tp);
tp->data= y;
tp->right_child= BSTDelete(tp->right_child,y);
return tp;
}
if( x < tp->data)
tp->left_child = BSTDelete(tp->left_child,x);
else
tp->right_child = BSTDelete(tp->right_child,x);
return tp;
}
void InOrder( NODE p)
{
if( p ) // p != NULL
{
InOrder(p->left_child);
printf("%d\t ",p->data);
InOrder(p->right_child);
}
}
void PreOrder( NODE p)
{
if( p != NULL)
{
printf("%d\t",p->data);
PreOrder(p->left_child);
PreOrder(p->right_child);
}
}
void PostOrder( NODE p)
{
if( p)
{
PostOrder(p->left_child);
PostOrder(p->right_child);
printf("%d\t",p->data);
}
}
int main()
{
int choice,x;
NODE root=NULL;
printf("\n1:Inser\n2:Delette\n3:Inorder Display\n4:Preorder
Display\n5:Postorder Display\n6:exit ");
printf("\nEnter the Choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter the element to be Inserted ");
scanf("%d",&x);
root=BSTInsert(root,x);
break;
case 2:printf("\nEnter the element to be Deteted ");
scanf("%d",&x);
root=BSTDelete(root,x);
break;
case 3 :printf("\nTraversal of BST : Inorder");
InOrder(root);
break;
case 4 :printf("\nTraversal of BST : Preorder");
PreOrder(root);
break;
case 5 :printf("\nTraversal of BST : Postorder");
PostOrder(root);
break;
case 6:exit(0);
} //switch
}//while
return 0;
}//main
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
#include<stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}