0% found this document useful (0 votes)
108 views25 pages

Parth DS SEM 3

The program implements insertion sort to sort an integer array in ascending order. It takes an integer array as input, then iterates through the array and inserts each element into its sorted position. It shifts elements in the sorted section of the array to make space for the new element being inserted. This continues until the entire array is sorted. The sorted array is then printed as output. The program performs the insertion sort algorithm in O(n^2) time complexity.

Uploaded by

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

Parth DS SEM 3

The program implements insertion sort to sort an integer array in ascending order. It takes an integer array as input, then iterates through the array and inserts each element into its sorted position. It shifts elements in the sorted section of the array to make space for the new element being inserted. This continues until the entire array is sorted. The sorted array is then printed as output. The program performs the insertion sort algorithm in O(n^2) time complexity.

Uploaded by

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

Q1.

Write a program to implement stacks using C


#include <stdio.h>
#define MAXSIZE 5
struct stack
{
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;
void push(void);
int pop(void);
void display(void);
void main ()
{
int choice;
int option = 1;
s.top = -1;
printf ("STACK OPERATION\n");
while (option)
{
printf ("
1 : PUSH
\n");
printf ("
2 : POP
\n");
printf ("
3 : DISPLAY
\n");
printf ("
4 : EXIT
\n");

printf ("Enter your choice\n");


scanf ("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
return;
}
fflush (stdin);
printf ("Do you want to continue the program(0 or 1)?\n");
scanf ("%d", &option);
}
}
void push () //to add an element to the stack
Parth Sharma

SG-14330

Page 1

{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("Stack is Full\n");
return;
}
else
{
printf ("Enter the element to be pushed\n");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}
int pop () // If you want to delete element
{
int num;
if (s.top == - 1)
{
printf ("Stack is Empty\n");
return (s.top);
}
else
{
num = s.stk[s.top];
printf ("poped element is = %dn", s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}
/* Function to display the status of the stack */
void display ()
{
int i;
if (s.top == -1)
{
printf ("Stack is empty\n");
return;
}
else
{
printf ("\n The status of the stack is \n");
for (i = s.top; i >= 0; i--)
{
printf ("%d\n", s.stk[i]);
}
}
printf ("\n");
}
Parth Sharma

SG-14330

Page 2

Output:

Q2. Write a Program to


implement stacks using linked list in C
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
Parth Sharma

SG-14330

Page 3

}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main()
{
int no, ch, e;
printf("\n Press 1 to Push");
printf("\n Press 2 to Pop");
printf("\n Press 3 to Top");
printf("\n Press 4 to Empty");
printf("\n Press 5 to Exit");
printf("\n Press 6 to Dipslay");
printf("\n Press 7 to Stack Count");
printf("\n Press 8 to Destroy stack");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
Parth Sharma

SG-14330

Page 4

case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
void create()
{
top = NULL;
}
void stack_count()
{
printf("\n No. of elements in stack : %d", count);
}
void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
void display()
{
top1 = top;
if (top1 == NULL)
{
Parth Sharma

SG-14330

Page 5

printf("Stack is empty");
return;
}
while (top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
void pop()
{
top1 = top;
if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
int topelement()
{
return(top->info);
}
void
{
if (top

empty()
== NULL)
printf("\n Stack is empty");

else
printf("\n Stack is not empty with %d
count);

elements",
}
void
{
top1 =
while
{
top1 =

destroy()
top;
(top1 != NULL)
top->ptr;
free(top);
top1;

top =
Parth Sharma

SG-14330

Page 6

top1 = top1->ptr;
}
free(top1);
top = NULL;
printf("\n All stack elements destroyed");
count = 0;
}
OUTPUT:

Q3. Write a program to Insert an element into an Array


#include<stdio.h>
void main()
{
int a[10],i,num,item,position;
printf("\nEnter the number of elements of the array \n");
scanf("\n%d",&num);
printf("\nEnter the element \n");
for(i=0;i<num;i++)
{
scanf("\n%d",&num);
}
printf("\nEnter the element which you want to insert \n");
scanf("\n%d",&item);
printf("\nEnter the position on which you want to insert the element \n");
scanf("\n%d",&position);
Parth Sharma

SG-14330

Page 7

for(i=position;i<num;i++)
{
a[i]=a[i+1];
}
a[position]=item;
num=num+1;
printf("\nThe final result is");
for(i=0;i<num;i++)
{
printf(" %d",a[i]);
}
}

OUTPUT:

Q4. Write a Program to implement queues using stacks


#include <stdio.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
void insert();
void delete();
void display();
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)
{
Parth Sharma

SG-14330

Page 8

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;
}
}
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
{
Parth Sharma
SG-14330

Page 9

printf("Queue is : \n");
for (i =

front; i <= rear; i++)


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

}
}

OUTPUT:

Q5.Write a Program to demonstrate use of circular queue in C


#include<stdio.h>
#define max 3
int front=0,rear=-1,q[10];
void main()
{
int ch;
void insert();
void delet();
void display();
printf("\nCircular Queue\n");
printf("1 for insert\n");
printf("2 for delete\n");
printf("3 for display\n");
printf("4 for exit\n");
while(1)
{
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delet();
break;
case 3:display();
break;
case 4:exit(1);
default:printf("Invalid option\n");
Parth Sharma

SG-14330

Page 10

}
}
}
void insert()
{
int x;
if((front==0&&rear==max-1)||(front>0&&rear==front-1))
printf("Queue is overflow\n");
else
{
printf("Enter element that you want to insert:");
scanf("%d",&x);
if(rear==max-1&&front>0)
{
rear=0;
q[rear]=x;
}
else
{
if((front==0&&rear==-1)||(rear!=front-1))
q[++rear]=x;
}
}
}
void delet()
{
int a;
if((front==0)&&(rear==-1))
{
printf("Queue is in underflow\n");
exit(1);
}
if(front==rear)
{
a=q[front];
rear=-1;
front=0;
}
else
if(front==max-1)
{
a=q[front];
front=0;
}
else a=q[front++];
printf("Deleted element is:%d\n",a);
}
void display()
{
int i,j;
Parth Sharma

SG-14330

Page 11

if(front==0&&rear==-1)
{
printf("Queue is underflow\n");
exit(1);
}
if(front>rear)
{
for(i=0;i<=rear;i++)
printf("\t%d",q[i]);
for(j=front;j<=max-1;j++)
printf("\t%d",q[j]);
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
else
{
for(i=front;i<=rear;i++)
{
printf("\t%d",q[i]);
}
printf("\nrear is at %d\n",q[rear]);
printf("\nfront is at %d\n",q[front]);
}
printf("\n");
}

OUTPUT:

Parth Sharma

SG-14330

Page 12

Q6. Write a program to implement queues using linked list


#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*rear,*temp,*front,*front1;
void deq();
void empty();
void display();
int frontelement();
void enq(int data);
void create();
void queuesize();
Parth Sharma

SG-14330

Page 13

int count = 0;
void main()
{
int no, ch, e;
printf("\n Press 1 for Enque");
printf("\n Press 2 for Deque");
printf("\n Press 3 for Front element");
printf("\n Press 4 for Empty");
printf("\n Press 5 for Exit");
printf("\n Press 6 for Display");
printf("\n Press 7 for Queue size");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
Parth Sharma

SG-14330

Page 14

}
void create()
{
front = rear = NULL;
}
void queuesize()
{
printf("\n Queue size : %d", count);
}
void enq(int data)
{
if (rear == NULL)
{
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
}
else
{
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
}
count++;
}
void deq()
{
front1 = front;
if (front1 == NULL)
{
printf("\n Error: Trying to display elements from empty queue");
return;
}
else
if (front1->ptr != NULL)
{
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
front = front1;
Parth Sharma

SG-14330

Page 15

}
else
{
printf("\n Dequed value : %d", front->info);
free(front);
front = NULL;
rear = NULL;
}
count--;
}
void display()
{
front1 = front;
if ((front1 == NULL) && (rear == NULL))
{
printf("Queue is empty");
return;
}
while (front1 != rear)
{
printf("%d ", front1->info);
front1 = front1->ptr;
}
if (front1 == rear)
printf("%d", front1->info);
}
int frontelement()
{
if ((front != NULL) && (rear != NULL))
return(front->info);
else
return 0;
}
/* Display if queue is empty or not */
void empty()
{
if ((front == NULL) && (rear == NULL))
printf("\n Queue empty");
else
printf("Queue not empty");
}
OUTPUT:

Parth Sharma

SG-14330

Page 16

Q7.WAP to sort an array of integers in

ascending order using insertion sort


method
#include<stdio.h>
#include<conio.h>
void insertionSortMethod(int a[], int n);
void main()
{
int i, n, a[50];
printf("\nEnter Size of Array: ");
scanf("%d",&n);
printf("\n Enter %d elements of array \n\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insertionSortMethod(a, n);
printf("\n\n Sorted list of elements\n\n");
for(i=0;i<n;i++)
printf("%d\t", a[i]);
printf("\n");
}/*End of main Function*/
void insertionSortMethod(int a[], int n)
{
int k, temp, j;
for(k=1;k<=n-1;k++){
temp=a[k];
j=k-1;
while((temp<a[j])&&(j>=0)) {
a[j+1]=a[j];
j=j-1;
}
Parth Sharma

SG-14330

Page 17

a[j+1]=temp;
}
}
OUTPUT:

Q8. WAP to sort an array of integers in ascending order using selection sort
method.
#include<stdio.h>
//#include<conio.h>
void selectionSortMethod(int a[], int n);
void main()
{
int i, n, a[50];
printf("\nEnter Size of Array: ");
scanf("%d",&n);
printf("\n Enter %d elements of array \n\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selectionSortMethod(a, n);
printf("\n\n Sorted list of elements\n\n");
for(i=0;i<n;i++)
printf("%d\t", a[i]);
printf("\n");
}/*End of main Function*/
void selectionSortMethod(int a[], int n)
{
int i, loc, j, small;
for (i=1;i<n;i++){
small=a[i-1];
Parth Sharma

SG-14330

Page 18

loc=i-1;
for(j=i;j<n;j++){
if(a[j]<small){
small=a[j];
loc=j;
}
}
if(loc!=(i-1))
{
int temp=a[i-1];
a[i-1]=a[loc];
a[loc]=temp;
}
}
}
OUTPUT:

Parth Sharma

SG-14330

Page 19

Q9. WAP to demonstrate the implementation of various operations on a circular


queue represented using a linear array.
#include<stdio.h>
//#include<conio.h>
#define MAX 15
typedef enum {false, true} boolean;
int front =-1, rear=-1;
int elements[MAX];
boolean isEmpty();
boolean isFull();
void enqueue(int);
int dequeue();
int peep();
void main()
{
int choice, element;
do
{
printf("\n Options available \n");
printf("+++++++\n\n");
printf("1.Enqueue\n");
printf("2.Dequeue\n");
printf("3.Peep\n");
printf("Exit \n\n");
Parth Sharma

SG-14330

Page 20

printf("Enter your choice (1-4): ");


scanf("%d",&choice);
switch(choice)
{
case 1: if(isFull ())
{
printf("Queue Full");
printf("\n\n Press any key to continue");
}
else
{
printf("Enter value: ");
scanf("%d", &element);
enqueue(element);
}
break;
case 2:if(isEmpty())
{
printf("Queue Full..");
printf("\n\n Press any key to continue");
}
else
{
printf("Value Dequeued is %d \n", dequeue());
printf("\nPress any key to continue....");
}
break;
case 3: if (isEmpty())
{
printf("Queue full....");
printf("\n\n Press any key to continue");
}
else
{
printf("Element at front of queue is %d\n" ,peep());
printf("\n Press any key to continue....");
}
}
Parth Sharma

SG-14330

Page 21

}
while (choice !=4);
}//End of main function
boolean isEmpty()
{
if (front==-1)
return true;
else
return false;
}
boolean isFull()
{
if (((front==0)&&(rear==MAX-1)) || (front==rear+1))
return true;
else
return false;
}
void enqueue (int value)
{
if (front==-1)
front =rear=0;
else if (rear==MAX-1)
rear=0;
else
rear++;
elements[rear]=value;
}
int dequeue()
{
int temp;
temp=elements[front];
if(front==rear)
front=rear=-1;
else if (front==MAX-1)
front=0;
else
front++;
Parth Sharma

SG-14330

Page 22

return temp;
}
int peek()
{
return elements[front];
}

Q10. WAP to sort an array of integers in ascending order using merge sort
method.
#include<stdio.h>
#include<conio.h>
void mergeSortMethod(int a[], int beg, int end);
void mergingSortedSubArrays(int a[50], int lb, int lr, int rb, int rr);
void main()
{
int i, n, a[50];
printf("\nEnter Size of Array: ");
scanf("%d",&n);
printf("\n Enter %d elements of array \n\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selectionSortMethod(a, n);
printf("\n\n Sorted list of elements\n\n");
for(i=0;i<n;i++)
printf("%d\t", a[i]);
printf("\n");
Parth Sharma

SG-14330

Page 23

}/*End of main Function*/


void mergeSortMethod(int a[], int beg, int end)
{
int mid;
if (beg<end)
{
mid= (beg+end)/2;
mergeSortMethod(a, beg, end);
mergeSortMethod(a, mid+1, end);
mergingSortedSubArrays(a, beg, mid, mid+1, end);
}
}
void mergingSortedSubArrays(int a[50], int lb, int lr, int rb, int rr)
{
int na, nb, nc, k, c[50];
na=lb;
nb=rb;
nc=lb;
while ((na<=lr)&&(nb<=rr))
{
if (a[na]<a[nb])
c[nc]=a[na++];
else
c[nc]=a[nb++];
nc++;
}
if(na>lr)
{
while(nb<=rr)
c[nc++]=a[nb++];
}
else
{
while(na<=lr)
c[nc++]=a[na++];
}
for(k=lb;k<=rr;k++)
a[k]=c[k];
}
OUTPUT:
Parth Sharma

SG-14330

Page 24

Parth Sharma

SG-14330

Page 25

You might also like