0% found this document useful (0 votes)
40 views47 pages

Data Structures for Students

Uploaded by

Venkatesh Nama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views47 pages

Data Structures for Students

Uploaded by

Venkatesh Nama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Data Structure Lab Record

1. Write a program to read ‘N’ numbers of elements into an array and insert an
element in specified position of that array.
Program Code:

#include<stdio.h>
#include<conio.h>
int main(){
int arr[100] = { 0 };
int i, x, pos, n;
clrscr();
printf("\n Enter Number of array elements: ");
scanf("%d",&n);
printf("\n Enter elements: ");
for (i = 0; i < n; i++)
scanf("%d",&arr[i]);
// print the original array
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
printf("\nElement to be inserted: ");
scanf("%d",&x);
printf("\n Enter position to insert: ");
scanf("%d",&pos);
// increase the size by 1
n++;
// shift elements forward
for (i = n-1; i >= pos; i--)
arr[i] = arr[i - 1];
// insert x at pos
arr[pos - 1] = x;
// print the updated array
printf("\n Array after insertion: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
getch();
return 0;
}

Output:
2. Write Programs to implement the Stack operations using an array

#include<stdio.h>
#include<conio.h>
#define SIZE 100
void push(int);
void pop();
void display();
int stack[SIZE], top = -1;
void main(){
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection, Try again.");
}
}
}

void push(int value){


if(top == SIZE-1)
printf("\nStack is Full, insertion is not possible.");
else{
top++;
stack[top] = value;
printf("\nInsertion success.");
}
}

void pop(){
if(top == -1)
printf("\nStack is Empty, Deletion is not possible.");
else{
printf("\nDeleted : %d", stack[top]);
top--;
}
}

void display(){
if(top == -1)
printf("\nStack is Empty");
else{
int i;
printf("\nStack elements are:\n");
for(i=top; i>=0; i--)
printf("%d\n",stack[i]);
}
}
Output:
3. Write Programs to implement the Queue operations using an array.

Ans:
#include <stdio.h>
# define SIZE 100
void enqueue();
void dequeue();
void show();
int queue[SIZE];
int rear = - 1;
int front = - 1;
main(){
int ch;
while (1) {
printf("1.Enqueue Operation\n");
printf("2.Dequeue Operation\n");
printf("3.Display the Queue\n");
printf("4.Exit\n");
printf("Enter your choice of operations : ");
scanf("%d", &ch);
switch (ch) {
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
show();
break;
case 4:
exit(0);
default:
printf("Incorrect choice \n");
}
}
}

void enqueue(){
int item;
if (rear == SIZE - 1)
printf("Overflow \n");
else{
if (front == - 1)
front = 0;
printf("Element to be inserted in the Queue\n : ");
scanf("%d", &item);
rear = rear + 1;
queue[rear] = item;
}
}
void dequeue(){
if (front == - 1 || front >rear){
printf("Underflow \n");
return ;
}else{
printf("Element deleted from the Queue: %d\n", queue[front]);
front = front + 1;
}
}
void show(){
int i;
if (front == - 1 || front>rear)
printf("Empty Queue \n");
else{
printf("Queue: \n");
for ( i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
}
Output:
4. Write Programs to implement Singly linked list

#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head;

void beginsert ();


void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
int main (){
int choice =0;
while(choice != 9) {
printf("\n\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random
location\n4.Delete from Beginning\n 5.Delete from last\n6.Delete node after
specified location\n7.Search for an element\n8.Show\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice) {
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
return 0;
}

void beginsert() {
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
}

void lastinsert() {
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");

}
}
}
void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
}

}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty\n");
}
else{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
}
}
void last delete() {
struct node *ptr,*ptr1;
if(head == NULL){
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}

else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}
void random_delete() {
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion
\n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++){
ptr1 = ptr;
ptr = ptr->next;

if(ptr == NULL){
printf("\nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void search() {
struct node *ptr;
int item,i=0,flag=1;
ptr = head;
if(ptr == NULL) {
printf("\nEmpty List\n");
} else {
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL) {
if(ptr->data == item) {
printf("item found at location %d ",i+1);
break;
}
i++;
ptr = ptr -> next;
}
if(flag==1){
printf("Item not found\n");
}
}
}
void display()
{
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}
else{
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
}
}

Output:
5. Write Programs to implement doubly linked list

#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev;
struct node *next;
int data;
};
struct node *head;
void insertion_beginning();
void insertion_last();
void insertion_specified();
void deletion_beginning();
void deletion_last();
void deletion_specified();
void display();
void search();
void main (){
int choice =0;
while(choice != 9){
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random
location\n4.Delete from Beginning\n");
printf("5.Delete from last\n6.Delete the node after the given
data\n7.Search\n8.Show\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice){
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beginning();
break;
case 5:
deletion_last();
break;
case 6:
deletion_specified();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void insertion_beginning(){
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value");
scanf("%d",&item);

if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
}
printf("\nNode inserted\n");
}

}
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}

}
printf("\nnode inserted\n");
}
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
temp=head;
printf("Enter the location");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
}
void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}

}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
if(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr, *temp;
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("\nCan't delete\n");
}
else if(ptr -> next -> next == NULL)
{
ptr ->next = NULL;
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
printf("\n printing values...\n");
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void search()
{
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else {
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL) {
if(ptr->data == item) {
printf("\nitem found at location %d ",i+1);
flag=0;
break;
} else{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("\nItem not found\n");
}
}
}
Output:
6. Write Programs to implement the Stack operations using Liked List.

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

// Structure to create a node with data and next pointer


struct Node {
int data;
struct Node *next;
};
struct Node *top = NULL;

// Push() operation on a stack


void push(int value) {
struct Node *newNode;
newNode = (struct Node *)malloc(sizeof(struct Node));
newNode->data = value; // assign value to the node
if (top == NULL) {
newNode->next = NULL;
} else {
newNode->next = top; // Make the node as top
}
top = newNode; // top always points to the newly created node
printf("Node is Inserted\n\n");
}

void pop() {
if (top == NULL) {
printf("\nStack Underflow\n");
return 0;
} else {
struct Node *temp = top;
temp->data = top->data;
top = top->next;
printf(“Element deleted id %d”, temp->data);
free(temp);
}
}

void display() {
struct Node *temp;
// Display the elements of the stack

if (top == NULL) {
printf("\nStack Underflow\n");
} else {
printf("The stack is \n");
temp = top;
while (temp->next != NULL) {
printf("%d--->", temp->data);
temp = temp->next;
}
printf("%d--->NULL\n\n", temp->data);
}
}

int main() {
int choice, value;
printf("\nImplementaion of Stack using Linked List\n");
while (1) {
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}
}

Output:
7. Write Programs to implement the Queue operations using Liked List.
#include <stdio.h>
#include <stdlib.h>

// Structure to create a node with data and next pointer


struct node {
int data;
struct node *next;
};

struct node *front = NULL;


struct node *rear = NULL;

// Enqueue() operation on a queue


void enqueue(int value) {
struct node *ptr;
ptr = (struct node *)malloc(sizeof(struct node));
ptr->data = value;
ptr->next = NULL;
if ((front == NULL) && (rear == NULL)) {
front = rear = ptr;
} else {
rear->next = ptr;
rear = ptr;
}
printf("Node is Inserted\n\n");
}

// Dequeue() operation on a queue


int dequeue() {
if (front == NULL) {
printf("\nUnderflow\n");
return -1;
} else {
struct node *temp = front;
int temp_data = front->data;
front = front->next;
free(temp);
return temp_data;
}
}

// Display all elements of queue


void display() {
struct node *temp;
if ((front == NULL) && (rear == NULL)) {
printf("\nQueue is Empty\n");
} else {
printf("The queue is \n");
temp = front;
while (temp) {
printf("%d--->", temp->data);
temp = temp->next;
}
printf("NULL\n\n");
}
}

void main() {
int choice, value;
clrscr();
printf("\nImplementation of Queue using Linked List\n");
while (choice != 4) {
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
enqueue(value);
break;
case 2:
printf("Deleted element is :%d\n", dequeue());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
}
}

Output:
8. Write a program to convert a given infix expression to postfix expression using
Ans:
#include<stdio.h>
#include<ctype.h>

char stack[100];
int top = -1;

void push(char x)
{
stack[++top] = x;
}

char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}

int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
return 0;
}

void main()
{
char exp[100];
char *e, x;
clrscr();
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;

while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}

while(top != -1)
{
printf("%c ",pop());
}

getch();
}

Output:
9. Write a program for Binary Search Tree operations and its traversals
Ans:
/*
* C program to implement the Binary Search Tree
*/
#include <stdio.h>
#include <stdlib.h>

// structure of a node
struct node
{
int data;
struct node *left;
struct node *right;
};

// globally initialized root pointer


struct node *root = NULL;

// function prototyping
struct node *create_node(int);
void insert(int);
struct node *delete (struct node *, int);
int search(int);
void inorder(struct node *);
void postorder();
void preorder();
struct node *smallest_node(struct node *);
struct node *largest_node(struct node *);
int get_data();

void main(){
int userChoice;
int data;
struct node* result = NULL;

while (1){
printf("\n\n------- Binary Search Tree ------\n");
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Search");
printf("\n4. Get Larger Node Data");
printf("\n5. Get smaller Node data");
printf("\n\n-- Traversals --");
printf("\n\n6. Inorder ");
printf("\n7. Post Order ");
printf("\n8. Pre Oder ");
printf("\n9. Exit");

printf("\n\nEnter Your Choice: ");


scanf("%d", &userChoice);
printf("\n");

switch(userChoice)
{
case 1:
data = get_data();
insert(data);
break;

case 2:
data = get_data();
root = delete(root, data);
break;

case 3:
data = get_data();
if (search(data) == 1){
printf("\nData was found!\n");
}
else{
printf("\nData does not found!\n");
}
break;

case 4:
result = largest_node(root);
if (result != NULL)
{
printf("\nLargest Data: %d\n", result->data);
}
break;

case 5:
result = smallest_node(root);
if (result != NULL)
{
printf("\nSmallest Data: %d\n", result->data);
}
break;

case 6:
inorder(root);
break;
case 7:
postorder(root);
break;

case 8:
preorder(root);
break;

case 9:
exit(0);

default:
printf("\n\tInvalid Choice\n");
break;
}

// creates a new node


struct node *create_node(int data){
struct node *new_node = (struct node *)malloc(sizeof(struct node));

if (new_node == NULL){
printf("\nMemory for new node can't be allocated");
return NULL;
}

new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;

return new_node;
}

// inserts the data in the BST


void insert(int data){
struct node *temp = root;
struct node *prev = NULL;
struct node *new_node = create_node(data);

if (new_node != NULL){
// if the root is empty then make a new node as the root node
if (root == NULL){
root = new_node;
printf("\n* node having data %d was inserted\n", data);
return;
}
// traverse through the BST to get the correct position for insertion
while (temp != NULL){
prev = temp;
if (data > temp->data){
temp = temp->right;
}
else{
temp = temp->left;
}
}
// found the last node where the new node should insert
if (data >prev->data){
prev->right = new_node;
}
else{
prev->left = new_node;
}

printf("\n* node having data %d was inserted\n", data);


}
}

// deletes the given key node from the BST


struct node *delete (struct node *root, int key){
struct node *temp;
if (root == NULL){
return root;
}
if (key < root->data){
root->left = delete (root->left, key);
}
else if (key > root->data)
{
root->right = delete (root->right, key);
}
else
{
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
temp = smallest_node(root->right);
root->data = temp->data;
root->right = delete (root->right, temp->data);
}
return root;

// search the given key node in BST


int search(int key)
{
struct node *temp = root;

while (temp != NULL)


{
if (key == temp->data)
{
return 1;
}
else if (key > temp->data)
{
temp = temp->right;
}
else
{
temp = temp->left;
}
}
return 0;
}

// finds the node with the smallest value in BST


struct node *smallest_node(struct node *root)
{
struct node *curr = root;
while (curr != NULL &&curr->left != NULL)
{
curr = curr->left;
}
return curr;
}

// finds the node with the largest value in BST


struct node *largest_node(struct node *root)
{
struct node *curr = root;
while (curr != NULL &&curr->right != NULL)
{
curr = curr->right;
}
return curr;
}

// inorder traversal of the BST


void inorder(struct node *root)
{
if (root == NULL)
{
return;
}
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}

// preorder traversal of the BST


void preorder(struct node *root)
{
if (root == NULL)
{
return;
}
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}

// postordertravsersal of the BST


void postorder(struct node *root)
{
if (root == NULL)
{
return;
}
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}

// getting data from the user


int get_data()
{
int data;
printf("\nEnter Data: ");
scanf("%d", &data);
return data;
}

Output:
10. Write a program to search an item in a given list using the following Searching
Algorithms
a. Linear Search
b. Binary Search.

/* Linear Search */

#include <stdio.h>

#include<conio.h>

int main(){

int array[100], num, i, n;

clrscr();

printf("Enter number of elements in array\n");

scanf("%d", &n);

printf("Enter %d integer(s)\n", n);

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

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

printf("Enter a number to search\n");

scanf("%d", &num);

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

if (array[i] == num) /* If required element is found *{

printf("%d is present at location %d.\n", num, i+1);

break;

if (i == n)

printf("%d isn't present in the array.\n", num);


getch();

return 0;

Output:

/* Binary Search */

#include <stdio.h>

void main(){

int i, low, high, mid, n, key, array[100];

clrscr();

printf("Enter number of elements: ");

scanf("%d",&n);

printf("Enter %d integer\n", n);

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

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

printf("Enter value to find: ");

scanf("%d", &key);

low = 0;

high = n - 1;
mid = (low+high)/2;

while (low <= high) {

if(array[mid] < key)

low = mid + 1;

else if (array[mid] == key) {

printf("%d found at location %d \n", key, mid+1);

break;

else

high = mid - 1;

mid = (low + high)/2;

if(low > high)

printf(" %d isn't present in the list \n", key);

getch();

Output:
11. Write a program for implementation of the following Sorting Algorithms
a. Bubble Sort
b. Insertion Sort

/* Bubble Sort */

#include <stdio.h>

#include<conio.h>

void main(){

int arr[50], num, i, j, temp;

clrscr();

printf("Enter the Number of Elements you want to sort: ");

scanf("%d", &num);

printf("\nEnter the Value of Elements: ");

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

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

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

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

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

temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}
}
}
printf("Sorted List: ");
for(i = 0; i < num; i++){
printf("%d ", arr[i]);
}
getch();
}

Output:

/* Insertion Sort*/

#include <stdio.h>

#include<conio.h>

void main(){

int a[50], n, i, j, temp;

clrscr();

printf("Enter Number of Elements you want to sort: ");

scanf("%d", &n);

printf("\nEnter Value of Elements: ");

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

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

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


temp = a[i];

j = i - 1;

/* Move the elements greater than temp to one position ahead from their current position*/

while(j>=0 && temp <= a[j])

{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
printf("Sorted List: ");

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

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

getch();

Output:
12. Write a program for implementation of Merge Sort Algorithms

Ans:

#include <stdio.h>

#define max 10

int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 22 };

int b[10];

void merging(int low, int mid, int high) {

int l1, l2, i;

for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {

if(a[l1] <= a[l2])

b[i] = a[l1++];

else

b[i] = a[l2++];

while(l1 <= mid)

b[i++] = a[l1++];

while(l2 <= high)

b[i++] = a[l2++];

for(i = low; i <= high; i++)

a[i] = b[i];
}

void sort(int low, int high) {

int mid;

if(low < high) {

mid = (low + high) / 2;

sort(low, mid);

sort(mid+1, high);

merging(low, mid, high);

} else {

return;

void main() {

int i;

clrscr();

printf("List before sorting\n");

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

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

sort(0, max);
printf("\nList after sorting\n");

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

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

getch();

Output:

You might also like