DS Practical With Index

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 50

Sr no.

Program name Date Signature


1 Design program to find sum of N number
2 Design Program to find Factorial of N
3 Design Program to find greatest amongst three given number

4 Implementation of traversing technique in array


5 Implementation of insertion technique in array
6 Implementation of deletion technique in array
7 Implementation of PUSH and POP operation on stack
8 Implementation of insertion and deletion technique in queue

9 Implementation of List data structure using i) array ii) singly


linked list.
10 Implementation of recursive technique for finding factorial
of an integer.
11 Implement stack using i) array ii) singly linked list
12 Implement Queue using i) array ii) singly linked list
13 Implementation of data insertion in Binary Search trees.
14 Implementation of data deletion in Binary Search trees.
15 Implementation of search in Binary Search trees.
16 Implementation of Linear search
17 Implementation of Binary Search using arrays.
18 Implementation of Bubble sort
19 Implementation of Selection sort
20 Implementation of Insertion sort
Program 1

Design program to find sum of two number

#include<stdio.h>

int main()

int A,B,sum=0;

//ask user to enter the numbers

printf("Enter two number A and B:\n");

//Read two number form the user ||A=2,B=3

scanf("%d%d",&A,&B);

//calculate the addtion of A and B

//using'+' operator

sum=A+B;

//print the sum

printf("sum of A and B is:%d",sum);

return 0;

Output : Enter two number A and B:

10

20

sum of A and B is:30

Program 2

Design program to find Factorial of N

#include<stdio.h>

//Function to find factorial of given number

unsigned int factorial(unsigned int n)

if(n==0)

return 1;

return n*factorial(n-1);

}
//driver code

int main()

int num=5;

printf("factorail of %d is %d",num,factorial(num));

return 0;

Output : factorail of 5 is 120

Program 3

Design program to find greatest amongst three given number

#include<stdio.h>

int main()

int A,B,C;

printf("Enter the number A,B and C:");

scanf("%d%d%d",&A,&B,&C);

if(A>=B && A>=C)

printf("%d is the largest number.",A);

if(B>=A && B>= C)

printf("%d is the largest number.",B);

if(C>=A&&C>=B);

printf("%d is the largest number.".C);

return 0;

Output : Enter the number A,B and C:67

56

45

67 is the largest number.45 is the largest number.


Program 4

Implementation of traversing technique in array

#include<stdio.h>

void printArray(int* arr,int n)

int i;

printf("Array:");

for(i=0;i<n;i++){

printf ("%d",arr[i]);

printf("\n");

int main()

{ int arr[]={2,-1,5,6,0,-3};

int n = sizeof(arr)/sizeof(arr[0]);

printArray(arr, n);

return 0;

Output: Array:2-1560-3

Program 5

Implementation of insertion technique in array

#include<stdio.h>

int main()

{ int arr[100]={0};

int i,x,pos,n=10;

// initial array of size 10

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

arr[i]=i+1;

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

printf("%d",arr[i]);

printf("\n");
//element to be inserted

x=50;

pos=5;

//increase the size by 1

n++;

//shift element forward

for(i=n-1;i>=pos;i--)

arr[i]=arr[i-1];

//print the updated array

for(i=0;i,n;i++)

printf("%d",arr[i]);

printf("\n");

return 0;

Output: 12345678910

123455678910

PROGRAM 6

Implementation of deletion technique in array

#include <stdio.h>

#include <conio.h>

// create user defined function update elements

addElement (int *arr, int num, int pos)

int i;

// use for loop to update the index

for (i = pos-1; i < num - 1; i++)

arr[i] = arr[i+1];//

// create disp() function to print the input array elements


disp( int arr[], int num)

int i;

// use for loop to print the resultant array

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

printf( "\n arr[%d] = %d", i, arr[i]);

int main ()

// declaration of the array

int arr[100];

int i, num, pos, result;

printf (" Enter the size of the array: ");

scanf (" %d", &num);

printf (" \n Enter the %d elements in an array: \n", num);

// use for loop to input the elements one by one

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

printf (" arr[%d] = ", i);

scanf (" %d", &arr[i]);

// define the position of the element to be deleted from an array

printf (" Enter the position of the element you want to delete from an array: ");

scanf (" %d", &pos);

// check whether thr defined pos is less than equal to size of array (num) and larger than

if (pos <= num && pos > 0)

printf (" Array before deletion:\n");

// call disp() function print the array


disp (arr, num);

// call addElement() function

addElement(arr, num, pos);

printf (" \n Array after deletion: \n");

// disp() function to print the updated array

disp( arr, num-1);

} return 0; }

Output: Enter the size of the array: 10

Enter the 10 elements in an array:

arr[0] = 23

23arr[1] = 34

arr[2] = 50

arr[3] = 67

arr[4] = 34

arr[5] = 56

arr[6] = 56

arr[7] = 56

arr[8] = 23

arr[9] = 67

Enter the position of the element you want to delete from an array: 89

Program 7

Implementation of PUSH and POP operation on stack

#include<stdio.h>

int stack[100],choice,n,top,x,i;

void push(void);

void pop(void);

void display(void);

int main()

//clrscr();

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:

printf("\n\t EXIT POINT ");

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");

Output : Enter the size of STACK[MAX=100]:68

STACK OPERATIONS USING ARRAY

1.PUSH

2.POP

3.DISPLAY

4.EXIT

Enter the Choice:2

Stack is under flow

Enter the Choice:1

Enter a value to be pushed:68

Enter the Choice:3

The elements in STACK

68

Press Next Choice

Enter the Choice:

Program 8

Implementation of Insertion and deletion technique in queue

#include <stdio.h>
#include<stdlib.h>

#define MAX 50

void insert();

void delete();

void display();

int queue_array[MAX];

int rear = - 1;

intfront = - 1;

int main()

int choice;

while (1)

printf("1.Insert element to queue \n");

printf("2.Delete element from queue \n");

printf("3.Display all elements of queue \n");

printf("4.Quit\n");

printf("Enter your choice : ");

scanf("%d", &choice);

switch(choice)

case 1:

insert();

break;

case 2:

delete();

break;

case 3:

display();

break;

case 4:

exit(1);

default:

printf("Wrong choice n");


}

void insert()

int item;

if(rear == MAX - 1)

printf("Queue Overflow \n");

else

if(front== - 1)

front = 0;

printf("Inset the element in queue : ");

scanf("%d", &item);

rear = rear + 1;

queue_array[rear] = item;

void delete()

if(front == - 1 || front > rear)

printf("Queue Underflow \n");

return;

else

printf("Element deleted from queue is : %d\n", queue_array[front]);

front = front + 1;

void display()

int i;
if(front == - 1)

printf("Queue is empty \n");

else

printf("Queue is : \n");

for(i = front; i <= rear; i++)

printf("%d ", queue_array[i]);

printf("\n");

OutPut :

Enter your choice : 2

Element deleted from queue is : 10

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice : 2

Element deleted from queue is : 20

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice : 2

Element deleted from queue is : 3

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice : 2

Queue Underflow

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue


4.Quit

Enter your choice : 4

Program 9

i) Implementation of Array

i)

#include <stdio.h>

int main()

int arr[10];

int i;

printf("Input 10 elements in the array :\n");

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

scanf("%d", &arr[i]);

printf("\nElements in array are: ");

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

printf("%d ", arr[i]);

printf("\n");

Output:

Input 10 elements in the array :

5
6

Elements in array are: 1 1 2 3 4 5 6 7 8 9

Program 9

ii) Implementation of Singly Linked List

#include<stdlib.h>

#include<stdio.h>

struct Node{

int data;

struct Node *next;

};

void deleteStart(struct Node** head){

struct Node* temp = *head;

// If head is NULL it means Singly Linked List is empty

if(*head == NULL){

printf("Impossible to delete from empty Singly Linked List");

return;

// move head to next node

*head = (*head)->next;

printf("Deleted: %d\n", temp->data);

free(temp);

void insertStart(struct Node** head, int data){


// dynamically create memory for this newNode

struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));

// assign data value

newNode->data = data;

// change the next node of this newNode

// to current head of Linked List

newNode->next = *head;

//re-assign head to this newNode

*head = newNode;

printf("Inserted %d\n",newNode->data);

void display(struct Node* node){

printf("\nLinked List: ");

// as linked list will end when Node is Null

while(node!=NULL){

printf("%d ",node->data);

node = node->next;

printf("\n");

int main()

struct Node* head = NULL;

// Need '&' i.e. address as we need to change head

insertStart(&head,100);

insertStart(&head,80);

insertStart(&head,60);

insertStart(&head,40);

insertStart(&head,20);
// No Need for '&' as not changing head in display operation

display(head);

deleteStart(&head);

deleteStart(&head);

display(head);

return 0;

OutPut :

Inserted 100

Inserted 80

Inserted 60

Inserted 40

Inserted 20

Linked List: 20 40 60 80 100

Deleted: 20

Deleted: 40

Linked List: 60 80 100

Program 10

Implementation of recursive technique for finding factorial of an

integer.

#include<stdio.h>

int find_factorial(int);

int main() {

int num, fact;

//Ask user for the input and store it in num

printf("\nEnter any integer number:");

scanf("%d",&num);

fact =find_factorial(num);
//Displaying factorial of input number

printf("\nfactorial of %d is: %d",num, fact);

return 0;

int find_factorial(int n)

if(n==0)

return(1);

//Function calling itself:recursion

return(n*find_factorial(n-1));

Output :

Enter any integer number:5

factorial of 5 is: 120

Program 11

i) Implement Stack Using Array

#include<stdio.h>

int stack[100],choice,n,top,x,i;

void push(void);

void pop(void);

void display(void);

int main()

//clrscr();

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:

printf("\n\t EXIT POINT ");

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");

Output:

Enter the size of STACK[MAX=100]:10

STACK OPERATIONS USING ARRAY

1.PUSH

2.POP

3.DISPLAY

4.EXIT

Enter the Choice:1

Enter a value to be pushed:52

Enter the Choice:1

Enter a value to be pushed:30

Enter the Choice:3

The elements in STACK

30

52

Press Next Choice

Enter the Choice:2

The popped elements is 30

Enter the Choice:2

The popped elements is 52

Enter the Choice:3

The STACK is empty

Enter the Choice:4

EXIT POINT
Program No 11

ii) Implement Stack Using Singly Linked List

#include <stdio.h>

#include <stdlib.h>

struct node {

int data;

struct node *next;

}*top;

/*

Initialize an empty stack

*/

void initialize() {

top = NULL;

/*

Checks if Stack is empty or not

*/

int isEmpty() {

if (top == NULL)

return 1;

else

return 0;

/*

Returns the top element of Stack

*/

int peek() {

return top->data;

/* Countstack elements */

int getStackSize(struct node *head){

/* Input Validation */

if (head == NULL) {

printf("Error : Invalid stack pointer !!!\n");


return;

int length = 0;

while(head != NULL){

head = head->next;

length++;

return length;

/*

Push an Element in Stack

*/

void push(int num) {

struct node *temp;

temp =(struct node *)malloc(1*sizeof(struct node));

temp->data = num;

if (top == NULL) {

top = temp;

top->next = NULL;

} else {

temp->next = top;

top = temp;

/*

Pop Operation: Removes Top Element of the Stack

*/

void pop() {

struct node *temp;

if (isEmpty(top)) {

printf("\nStack is Empty\n");

return;

} else {

temp = top;
top = top->next;

printf("Removed Element : %d\n", temp->data);

free(temp);

/*

Prints the linked list representation of a stack

*/

void printStack(struct node *nodePtr) {

while (nodePtr != NULL) {

printf("%d", nodePtr->data);

nodePtr = nodePtr->next;

if(nodePtr != NULL)

printf("-->");

printf("\n");

void main() {

/* Initialize Stack */

initialize();

/* Push Elements in stack */

push(1);

push(2);

push(3);

push(4);

/* Prints Size of Stack */

printf("Stack Size : %d\n", getStackSize(top));

/* Printing top element of Stack */

printf("\nTop Element : %d\n", peek());

/* Printing Stack */

printf("Stack as linked List\n");

printStack(top);

/* Removing elements from stack */

pop();
pop();

pop();

pop();

pop();

printStack(top);

return;

Output :

Stack Size : 4

Top Element : 4

Stack as linked List

4-->3-->2-->1

Removed Element : 4

Removed Element : 3

Removed Element : 2

Removed Element : 1

Stack is Empty

Program No 12

i) Implement Queue Using Array

#include <stdio.h>

#define MAX 50

void insert();

void delete();

void display();

int queue_array[MAX];

int rear = - 1;

intfront = - 1;

main()

int choice;

while (1)

printf("1.Insert element to queue \n");


printf("2.Delete element from queue \n");

printf("3.Display all elements of queue \n");

printf("4.Quit\n");

printf("Enter your choice : ");

scanf("%d", &choice);

switch (choice)

case 1:

insert();

break;

case 2:

delete();

break;

case 3:

display();

break;

case 4:

exit(1);

default:

printf("Wrong choice \n");

} /* End of switch */

}/* End of while */

}/* End of main() */

void insert()

int add_item;

if (rear == MAX - 1)

printf("Queue Overflow \n");

else

if (front == - 1)

/*If queue is initially empty */

front = 0;

printf("Inset the element in queue : ");


scanf("%d", &add_item);

rear = rear + 1;

queue_array[rear] = add_item;

}/* End of insert() */

void delete()

if (front == - 1 || front > rear)

printf("Queue Underflow \n");

return ;

else

printf("Element deleted from queue is : %d\n", queue_array[front]);

front = front + 1;

}/* End of delete() */

void display()

int i;

if (front == - 1)

printf("Queue is empty \n");

else

printf("Queue is : \n");

for (i = front; i <= rear; i++)

printf("%d ", queue_array[i]);

printf("\n");

}/* End of display() */

OutPut:

1.Insert element to queue

2.Delete element from queue


3.Display all elements of queue

4.Quit

Enter your choice

Inset the element in queue

: 10

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice

Inset the element in queue

: 50

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice

Queue is

10 50

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice

Element deleted from queue is


: 10

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice

Element deleted from queue is

: 50

1.Insert element to queue

2.Delete element from queue

3.Display all elements of queue

4.Quit

Enter your choice

Program No 12

ii) Implement Queue Using Array

#include<stdio.h>

#include<stdlib.h>

struct node

int data;

struct node *next;

};

struct node *front;

struct node *rear;

void insert();

void delete();

void display();

void main ()

{
int choice;

while(choice != 4)

printf("\nMain Menu\n");

printf("\n1.insert an element \n2.Delete an element \n3.Display the

queue\n4.Exit\n");

printf("\nEnter your choice :");

scanf("%d",& choice);

switch(choice)

case 1:

insert();

break;

case 2:

delete();

break;

case 3:

display();

break;

case 4:

exit(0);

break;

default:

printf("\nEnter valid choice??\n");

void insert()

struct node *ptr;

int item;

ptr = (struct node *) malloc (sizeof(struct node));

if(ptr == NULL)

{
printf("\nOVERFLOW\n");

return;

else

printf("\nEnter Element \n");

scanf("%d",&item);

ptr -> data = item;

if(front == NULL)

front = ptr;

rear = ptr;

front -> next = NULL;

rear -> next = NULL;

else

rear -> next = ptr;

rear = ptr;

rear->next = NULL;

void delete ()

struct node *ptr;

if(front == NULL)

printf("\nUNDERFLOW\n");

return;

else

ptr = front;
front = front -> next;

free(ptr);

void display()

struct node *ptr;

ptr = front;

if(front == NULL)

printf("\nEmpty queue\n");

else

{ printf("\nQueue Element are :\n");

while(ptr != NULL)

printf("\n%d\n",ptr -> data);

ptr = ptr -> next;

OutPut :

Main Menu

1.insert an element

2.Delete an element

3.Display the queue

4.Exit

Enter your choice :1

Enter Element

10

Main Menu

1.insert an element
2.Delete an element

3.Display the queue

4.Exit

Enter your choice :1

Enter Element

20

Main Menu

1.insert an element

2.Delete an element

3.Display the queue

4.Exit

Enter your choice :3

Queue Element are :

10

20

Main Menu

1.insert an element

2.Delete an element

3.Display the queue

4.Exit

Enter your choice :

Program No 13

Implementation of data insertion in Binary Search Trees

#include<stdio.h>

#include<stdlib.h>

void insert(int);

struct node

int data;

struct node *left;

struct node *right;


};

struct node *root;

void main ()

int choice,item;

do

printf("\nEnter the item which you want to insert?\n");

scanf("%d",&item);

insert(item);

printf("\nPress 0 to insert more ?\n");

scanf("%d",&choice);

}while(choice == 0);

void insert(int item)

struct node *ptr, *parentptr , *nodeptr;

ptr = (struct node *) malloc(sizeof (struct node));

if(ptr == NULL)

printf("can't insert");

else

ptr -> data = item;

ptr -> left = NULL;

ptr -> right = NULL;

if(root == NULL)

root = ptr;

root -> left = NULL;

root -> right = NULL;

else
{

parentptr = NULL;

nodeptr = root;

while(nodeptr != NULL)

parentptr = nodeptr;

if(item < nodeptr->data)

nodeptr = nodeptr -> left;

else

nodeptr = nodeptr -> right;

if(item < parentptr -> data)

parentptr -> left = ptr;

else

parentptr -> right = ptr;

printf("Node Inserted");

OutPut :

Enter the item which you want to insert?

10

Node Inserted

Press 0 to insert more ?

Enter the item which you want to insert?


10

Node Inserted

Press 0 to insert more ?

Program No 14

Implementation of data deletion in Binary Search Trees

#include<stdio.h>

#include<malloc.h>

struct node{

int data;

struct node* left;

struct node* right;

};

struct node* createNode(int data){

struct node *n; // creating a node pointer

n = (struct node *) malloc(sizeof(struct node)); // Allocating memory in the heap

n->data = data; // Setting the data

n->left = NULL; // Setting the left and right children to NULL

n->right = NULL; // Setting the left and right children to NULL

return n; // Finally returning the created node

void preOrder(struct node* root){

if(root!=NULL){

printf("%d ", root->data);

preOrder(root->left);

preOrder(root->right);

void postOrder(struct node* root){


if(root!=NULL){

postOrder(root->left);

postOrder(root->right);

printf("%d ", root->data);

void inOrder(struct node* root){

if(root!=NULL){

inOrder(root->left);

printf("%d ", root->data);

inOrder(root->right);

int isBST(struct node* root){

static struct node *prev = NULL;

if(root!=NULL){

if(!isBST(root->left)){

return 0;

if(prev!=NULL && root->data <= prev->data){

return 0;

prev = root;

return isBST(root->right);

else{

return 1;

struct node * searchIter(struct node* root, int key){

while(root!=NULL){

if(key == root->data){

return root;
}

else if(key<root->data){

root = root->left;

else{

root = root->right;

return NULL;

void insert(struct node *root, int key){

struct node *prev = NULL;

while(root!=NULL){

prev = root;

if(key==root->data){

printf("Cannot insert %d, already in BST", key);

return;

else if(key<root->data){

root = root->left;

else{

root = root->right;

struct node* new = createNode(key);

if(key<prev->data){

prev->left = new;

else{

prev->right = new;

}
struct node *inOrderPredecessor(struct node* root){

root = root->left;

while (root->right!=NULL)

root = root->right;

return root;

struct node *deleteNode(struct node *root, int value){

struct node* iPre;

if (root == NULL){

return NULL;

if (root->left==NULL&&root->right==NULL){

free(root);

return NULL;

//searching for the node to be deleted

if (value < root->data){

root-> left = deleteNode(root->left,value);

else if (value > root->data){

root-> right = deleteNode(root->right,value);

//deletion strategy when the node is found

else{

iPre = inOrderPredecessor(root);

root->data = iPre->data;

root->left = deleteNode(root->left, iPre->data);

return root;

}
int main(){

// Constructing the root node - Using Function (Recommended)

struct node *p = createNode(5);

struct node *p1 = createNode(3);

struct node *p2 = createNode(6);

struct node *p3 = createNode(1);

struct node *p4 = createNode(4);

// Finally The tree looks like this:

// 5

// / \

// 3 6

// / \

// 1 4

// Linking the root node with left and right children

p->left = p1;

p->right = p2;

p1->left = p3;

p1->right = p4;

inOrder(p);

printf("\n");

deleteNode(p, 3);

inOrder(p);

return 0;

Output :

13456

1456
Program No 15

Implementation of Search in Binary Search Trees

#include<stdio.h>

#include<malloc.h>

struct node{

int data;

struct node* left;

struct node* right;

};

struct node* createNode(int data){

struct node *n; // creating a node pointer

n = (struct node *) malloc(sizeof(struct node));// Allocating memory in the heap

n->data = data; // Setting the data

n->left = NULL; // Setting the left and right children to NULL

n->right = NULL; // Setting the left and right children to NULL

return n; // Finally returning the created node

void preOrder(struct node* root){

if(root!=NULL){

printf("%d ", root->data);

preOrder(root->left);

preOrder(root->right);

void postOrder(struct node* root){

if(root!=NULL){

postOrder(root->left);

postOrder(root->right);

printf("%d ", root->data);

void inOrder(struct node* root){

if(root!=NULL){
inOrder(root->left);

printf("%d ", root->data);

inOrder(root->right);

int isBST(struct node* root){

static struct node *prev = NULL;

if(root!=NULL){

if(!isBST(root->left)){

return 0;

if(prev!=NULL && root->data <= prev->data){

return 0;

prev = root;

return isBST(root->right);

else{

return 1;

struct node * search(struct node* root, int key){

if(root==NULL){

return NULL;

if(key==root->data){

return root;

else if(key<root->data){

return search(root->left, key);

else{
return search(root->right, key);

int main(){

// Constructing the root node - Using Function (Recommended)

struct node *p = createNode(5);

struct node *p1 = createNode(3);

struct node *p2 = createNode(6);

struct node *p3 = createNode(1);

struct node *p4 = createNode(4);

// Finally The tree looks like this:

// 5

// / \

// 3 6

// / \

// 1 4

// Linking the root node with left and right children

p->left = p1;

p->right = p2;

p1->left = p3;

p1->right = p4;

struct node* n = search(p, 10);

if(n!=NULL){

printf("Found: %d", n->data);

else{

printf("Element not found");

return 0;

}
OutPut:

Element not found

Program No 16

Implementation of Linear Search

#include<stdio.h>

int main()

int a[20],i,x,n;

printf("How many elements :");

scanf("%d",&n);

printf("Enter array elements:\n");

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

scanf("%d",&a[i]);

printf("Enter element to search:");

scanf("%d",&x);

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

if(a[i]==x)

break;

if(i<n)

printf("Element found at index %d",i);

else

printf("Element not found");

return 0;

OutPut :

How many elements :5

Enter array elements:


10

20

30

40

50

Enter element to search:50

Element found at index 4

Program No : 17

Implementation of Binary Search Using Array

#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 :

Enter number of elements

Enter 5 integers

10

20

30

40

50

Enter value to find

20

20 found at location 2.

Program No : 18

Implementation of Bouble Sort

#include <stdio.h>

int main(){

int arr[50], num, x, y, temp;

printf("Please Enter the Number of Elements you want in the array: ");

scanf("%d", &num);

printf("Please Enter the Value of Elements: ");


for(x = 0; x < num; x++)

scanf("%d", &arr[x]);

for(x = 0; x < num - 1; x++){

for(y = 0; y < num - x - 1; y++){

if(arr[y] > arr[y + 1]){

temp = arr[y];

arr[y] = arr[y + 1];

Page 58 of 62

arr[y + 1] = temp;

printf("Array after implementing bubble sort: ");

for(x = 0; x < num; x++){

printf("%d ", arr[x]);

return 0;

OutPut :

Please Enter the Number of Elements you want in the array: 10

10

Please Enter the Value of Elements: 10


20

30

40

50

60

70

80

90

100

Array after implementing bubble sort: 10 20 30 40 50 60 70 80 90 100

Program No : 19

Implementation of Selection Sort

#include <stdio.h>

int main() {

int arr[10]={6,12,0,18,11,99,55,45,34,2};

int n=10;

int i, j, position, swap;

for (i = 0; i < (n - 1); i++) {

position = i;

for (j = i + 1; j < n; j++) {

if (arr[position] > arr[j])

position = j;

if (position != i) {

swap = arr[i];

arr[i] = arr[position];

arr[position] = swap;

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

printf("%d\t", arr[i]);

return 0;
}

OutPut :

0 2 6 11 12 18 34 45 55 99

Program No : 20

Implementation of Insertion Sort

// Insertion sort in C

#include <stdio.h>

// Function to print an array

void printArray(int array[], intsize) {

for (int i = 0; i < size; i++) {

printf("%d ", array[i]);

printf("\n");

void insertionSort(int array[], int size) {

for (int step = 1; step < size; step++) {

int key = array[step];

int j = step - 1;

// Compare key with each element on the left of it until an elementsmaller than

// it is found.

// For descending order, change key<array[j] to key>array[j].

while (key < array[j] && j >= 0) {

array[j + 1] = array[j];

--j;

array[j + 1] = key;

// Driver code

int main() {

int data[] = {9, 5, 1, 4, 3};


intsize = sizeof(data) / sizeof(data[0]);

insertionSort(data, size);

printf("Sorted array in ascending order:\n");

printArray(data,size);

OutPut :

Sorted array in ascending order:

13459

You might also like