Dynamic Array Sorting and Searching Lab
Dynamic Array Sorting and Searching Lab
BRANCH – CSE([Link])
#include<stdlib.h>
int main()
int FLAG;
do
scanf("%d",&input);
count++;
more=(int*)realloc(numbers,count*sizeof(int));
if(more!=NULL)
else
for(i=0;i<count-1;i++)
printf("%d,",numbers[i]);
printf("\n");
/* BUBBLE SORT */
for(i=0;i<count-1;i++)
{ FLAG=0;
for (j=0;j<count-1-1;j++)
{ FLAG=1;
c=numbers[j];
numbers[j]=numbers[j+1];
numbers[j+1]=c;
if (FLAG==0){
exit(0);
}
//printing the sorted list
printf("%d,", numbers[i]);
printf("\n");
return 0;
OUTPUT:
LAB EXPERIMENT – 2
if (n >= capacity)
return n;
int i;
arr[i + 1] = arr[i];
arr[i + 1] = key;
return (n + 1);
// Deleting element
int i;
return n - 1;
return -1;
if (key == arr[mid])
return mid;
// Driver code
int main()
int i,size,key,k;
int arr[100];
cin>>size;
for(i=0;i<size;i++)
cin>>arr[i];
cin>>key;
else{
return 0;
⇨ OUTPUT:
LAB EXPERIMENT – 3
⇨ WAP to enter records of Five students, which should contain
fields like roll No., name, CGPI, semester. (a) List all record of all
students having CGPI greater than k. (b) Insert a new record of
student at kth position and print the final record.
#include <stdio.h>
#include<string.h>
struct student
char name[50];
int roll;
float CGPI;
} s[100];
int main()
int i,n,K;
char NAME[50];
int Roll;
float cgpi;
float k;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s", s[i].name);
scanf("%d", &s[i].roll);
scanf("%f", &k);
printf("Displaying Information:\n");
for(i=0;i<n;i++)
if(s[i].CGPI>k){
printf("\tName:%s\n ",s[i].name);
scanf("%d", &K);
scanf("%s", NAME);
scanf("%d", &Roll);
scanf("%f", &cgpi);
n++;
strcpy(s[i].name,s[i - 1].name);
strcpy(s[K - 1].name,NAME);
printf("Displaying Information:\n");
for(i=0;i<n;i++)
printf("\tName:%s\n ",s[i].name);
return 0;
}
OUTPUT:
LAB EXPERIMENT – 4
⇨ Implement linked list and insert and delete an element into the list.
#include<stdlib.h>
#include <stdio.h>
void create();
void display();
void insert_begin();
void insert_end();
void insert_pos();
void delete_begin();
void delete_end();
void delete_pos();
struct node
int info;
};
int main()
int choice;
while(1){
printf("\n--------------------------------------\n");
scanf("%d",&choice);
switch(choice)
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_begin();
break;
case 4:
insert_end();
break;
case 5:
insert_pos();
break;
case 6:
delete_begin();
break;
case 7:
delete_end();
break;
case 8:
delete_pos();
break;
case 9:
exit(0);
break;
default:
break;
return 0;
void create()
if(temp==NULL)
exit(0);
scanf("%d",&temp->info);
temp->next=NULL;
if(start==NULL)
start=temp;
else
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=temp;
void display()
{
if(start==NULL)
printf("\nList is empty:\n");
return;
else
ptr=start;
while(ptr!=NULL)
printf("%d\t",ptr->info );
ptr=ptr->next ;
void insert_begin()
if(temp==NULL)
return;
scanf("%d",&temp->info);
temp->next =NULL;
if(start==NULL)
start=temp;
else
{
temp->next=start;
start=temp;
void insert_end()
if(temp==NULL)
return;
scanf("%d",&temp->info );
temp->next =NULL;
if(start==NULL)
start=temp;
else
ptr=start;
while(ptr->next !=NULL)
ptr=ptr->next ;
ptr->next =temp;
void insert_pos()
int i,pos;
if(temp==NULL)
{
return;
scanf("%d",&pos);
scanf("%d",&temp->info) ;
temp->next=NULL;
if(pos==0)
temp->next=start;
start=temp;
else
for(i=0,ptr=start;i<pos-1;i++) { ptr=ptr->next;
if(ptr==NULL)
return;
temp->next =ptr->next ;
ptr->next=temp;
void delete_begin()
if(ptr==NULL)
printf("\nList is Empty:\n");
return;
}
else
ptr=start;
start=start->next ;
free(ptr);
void delete_end()
if(start==NULL)
printf("\nList is Empty:");
exit(0);
ptr=start;
start=NULL;
free(ptr);
else
ptr=start;
while(ptr->next!=NULL)
temp=ptr;
ptr=ptr->next;
temp->next=NULL;
free(ptr);
}
void delete_pos()
int i,pos;
if(start==NULL)
exit(0);
else
scanf("%d",&pos);
if(pos==0)
ptr=start;
start=start->next ;
free(ptr);
else
ptr=start;
if(ptr==NULL)
return;
temp->next =ptr->next ;
free(ptr);
}
⇨ OUTPUT :
LAB EXPERIMENT -5
⇨ Evaluate a postfix algebraic expression with the help of stack.
#include<iostream>
#include<stdio.h>
#include<ctype.h>
#include<math.h>
#include<stdlib.h>
#define MAX 100
using namespace std;
struct postfix
{
int stack[MAX];
int top,n;
char *s;
}p;
void init_post()
{
[Link]=-1;
}
void init_exp(char *str)
{
p.s=str;
}
void push(int number)
{
[Link]++;
[Link][[Link]]=number;
}
int pop()
{
int number=[Link][[Link]];
[Link]--;
return number;
}
void calculate()
{
int n1, n2, x;
while(*(p.s))
{
if(*(p.s)==' ' || *(p.s)==' ')
{
p.s++;
continue;
}
if(isdigit(*(p.s)))
{
p.n=*(p.s)-'0';
push(p.n);
}
else
{
n1=pop();
n2=pop();
switch(*(p.s))
{
case '+':
x=n2+n1 ;
break;
case '-':
x=n2-n1 ;
break;
case '*':
x=n2*n1 ;
break;
case '/':
x=n2/n1 ;
break;
case '%':
x=n2%n1 ;
break;
case '^':
x=(int)pow(n2,n1) ;
break;
default:
cout<<"Invalid operator !!\n" ;
exit(1);
}
push(x);
}
p.s++;
}
}
void disp_result()
{
p.n=pop();
cout<<"\nResult = "<<p.n;
}
int main()
{
char exp[MAX];
init_post();
cout<<"Enter the postfix formed expression : ";
gets(exp);
init_exp(exp);
calculate();
disp_result();
return 0;
}
⇨ OUTPUT:
LAB EXPERIMENT – 6
⇨ Implement a queue using linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delet();
void display();
int main ()
{
int choice;
while(choice != 4)
{
printf("\n*************************Main Menu*****************************\n");
printf("\
n======================================================
===========\n");
printf("\[Link] an element\[Link] an element\[Link] the queue\
[Link]\n");
printf("\nEnter your choice: \t");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice : \n");
}
}
}
void insert()
{
struct node *ptr;
int item;
⇨ OUTPUT:
⇨ Implement a queue using arrays.
#include<iostream>
#include <stdio.h>
#define MAX 50
void insert();
void delet();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("\[Link] element to queue \n");
printf("\[Link] element from queue \n");
printf("\[Link] all elements of queue \n");
printf("\[Link] \n");
printf("\nEnter your choice :\t ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong 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("\nInset the element in queue :\t ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */
void delet()
{
if (front == - 1 || front > rear)
{
printf("\nQueue Underflow \n");
return ;
}
else
{
printf("\nElement deleted from queue is :\t %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("\nQueue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
} /* End of display() */
⇨
⇨ OUTPUT:
LAB EXPERIMENT – 7
⇨ Implement a binary tree and implement any traversal technique as you
like.
#include <stdio.h>
#include <stdlib.h>
case (INORDER):
// Do an Inorder Traversal
print_tree(traversal, root->left);
printf("%d -> ", root->value);
print_tree(traversal, root->right);
break;
case (POSTORDER):
// Do a postorder Traversal
print_tree(traversal, root->left);
print_tree(traversal, root->right);
printf("%d -> ", root->value);
break;
}
}
int main() {
// Program to demonstrate finding the height of a Binary Tree
root->left->left = create_node(40);
root->left->right = create_node(50);
root->right->left = create_node(60);
root->right->right = create_node(70);
printf("----Preorder Traversal:----\n");
print_tree(PREORDER, root);
printf("\n\n");
printf("----Inorder Traversal:----\n");
print_tree(INORDER, root);
printf("\n\n");
printf("----Postorder Traversal:----\n");
print_tree(POSTORDER, root);
printf("\n\n");
⇨ OUTPUT:
LAB EXPERIMENT- 8
⇨ Implement a binary Search Tree and insert and
delete a node in the BST.
#include <bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node *left;
Node *right;
public:
Node(int x){
data = x;
left = NULL;
right = NULL;
}
};
return tree;
}
string ret_val;
if(tree->data == val)
cout<<val<<" is present in the tree\n";
else if(tree->data > val)
search_BST(tree->left, val);
else
search_BST(tree->right, val);
return 0;
else{
//if left child of the node is empty
if(tree->left == NULL){
Node *temp = tree->right;
free(tree);
tree= temp;
}
//if right child of the node is empty
else if(tree->right == NULL){
Node *temp = tree->left;
tree = temp;
free(temp);
}
//if both left and right child exists for the node
else{
Node *temp = tree->left;
while(temp->right->right!=NULL){ // traverse until you don't reach, the
second last right node of the left child of node to be deleted
temp = temp->right;
}
tree->data = temp->right->data; // update the value to be deleted with
the value of the rightmost node
Node *temp2 = temp->right->left; // pointer to the left child of the last
right node
free(temp->right); // delete the last node
temp->right = temp2; // assign the left child of last right node to
second last right node
}
}
return tree;
}
int main(){
Node *tree;
insert_BST(tree, 9);
cout<<"inorder traversal of the current tree is: ";
inorder(tree);
cout<<endl;
search_BST(tree,9);
tree = delete_BST(tree,5);
cout<<"deleted 5 successfully \n";
search_BST(tree,5);
cout<<"inorder traversal of the current tree is: ";
inorder(tree);
cout<<endl;
return 0;
}
⇨ OUTPUT:
LAB EXPERIMENT – 9
#include <stdio.h>
int heap_size = 0;
int t;
t = *a;
*a = *b;
*b = t;
return (2*index)+1;
return -1;
return 2*index;
return -1;
return index/2;
return -1;
largest = left_child_index;
largest = right_child_index;
}
// largest is not the node, node is not a heap
if (largest != index) {
swap(&A[index], &A[largest]);
max_heapify(A, largest);
int i;
max_heapify(A, i);
return A[1];
A[1] = A[heap_size];
heap_size--;
max_heapify(A, 1);
return maxm;
A[index] = key;
max_heapify(A, index);
heap_size++;
A[heap_size] = -1*INF;
int i;
printf("%d\n",A[i]);
printf("\n");
int main() {
int A[tree_array_size];
insert(A, 20);
insert(A, 15);
insert(A, 8);
insert(A, 10);
insert(A, 5);
insert(A, 7);
insert(A, 6);
insert(A, 2);
insert(A, 9);
insert(A, 1);
print_heap(A);
increase_key(A, 5, 22);
print_heap(A);
decrease_key(A, 1, 13);
print_heap(A);
printf("%d\n\n", maximum(A));
printf("%d\n\n", extract_max(A));
print_heap(A);
printf("%d\n", extract_max(A));
printf("%d\n", extract_max(A));
printf("%d\n", extract_max(A));
printf("%d\n", extract_max(A));
printf("%d\n", extract_max(A));
printf("%d\n", extract_max(A));
printf("%d\n", extract_max(A));
printf("%d\n", extract_max(A));
printf("%d\n", extract_max(A));
return 0;
⇨ OUTPUT:
LAB EXPERIMENT – 10
⇨ Implement a graph and find transpose of a graph where
Transpose of a directed graph G is another directed graph
on the same set of vertices with all of the edges reversed
compared to the orientation of the corresponding edges in
G. That is, if G contains an edge (u, v) then the
converse/transpose/reverse of G contains an edge (v, u) and
vice versa. Implement it with the help of adjacency list and
adjacency matrix.
#include <bits/stdc++.h>
adj[src].push_back(dest);
int main()
{
int v = 5;
vector<int> adj[v];
addEdge(adj, 0, 1);
addEdge(adj, 0, 4);
addEdge(adj, 0, 3);
addEdge(adj, 2, 0);
addEdge(adj, 3, 2);
addEdge(adj, 4, 1);
addEdge(adj, 4, 3);
vector<int> transpose[v];
// graph i.e. b
displayGraph(transpose, v);
return 0;
⇨ OUTPUT:
int main()
{
int arr[] = {10, 7, 8, 9, 1, 2, 11, 15, 20, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}
⇨ OUTPUT:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 1, 3, 4, 10 };
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
⇨ OUTPUT:
*xp = *yp;
*yp = temp;
int i, j, min_idx;
min_idx = i;
min_idx = j;
// Swap the found minimum element with the first element
swap(&arr[min_idx], &arr[i]);
int i;
int main()
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printArray(arr, n);
return 0;
⇨ OUTPUT:
#include <iostream>
using namespace std;
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;
// UTILITY FUNCTIONS
// Function to print an array
void printArray(int A[], int size)
{
for (int i = 0; i < size; i++)
cout << A[i] << " ";
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7, 1, 20, 15, 3 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
⇨ OUTPUT: