[go: up one dir, main page]

0% found this document useful (0 votes)
27 views8 pages

To merge two sorted arrays into one sorted

The document contains multiple C programs demonstrating fundamental data structures and algorithms, including merge sort, stack, queue, linear search, and binary search. Each program allows user interaction for performing operations such as inserting, deleting, and displaying elements. The code is structured with functions for each operation, ensuring modularity and clarity.

Uploaded by

Aniket Kumre
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)
27 views8 pages

To merge two sorted arrays into one sorted

The document contains multiple C programs demonstrating fundamental data structures and algorithms, including merge sort, stack, queue, linear search, and binary search. Each program allows user interaction for performing operations such as inserting, deleting, and displaying elements. The code is structured with functions for each operation, ensuring modularity and clarity.

Uploaded by

Aniket Kumre
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/ 8

// To merge two sorted arrays into one sorted

#include<stdio.h>
#include<conio.h>

void mergesort(int a[], int, int);


void merge(int[], int, int, int);
void main()
{
int a[20], i, n;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
mergesort(a, 0, n - 1);
printf("Data After Merge Sort");
for (i = 0; i < n; i++)
printf("\n%d", a[i]);
getch();
}
void mergesort(int a[], int lb, int ub)
{
int mid;
if (lb < ub)
{
mid = (lb + ub) / 2;
mergesort(a, lb, mid);
mergesort(a, mid + 1, ub);
merge(a, lb, mid + 1, ub);
}
}
void merge(int a[], int lb, int mid, int ub)
{
int k, p1, p2, p3, b[20];
p1 = lb;
p3 = lb;
p2 = mid;
while ((p1 < mid) && (p2 <= ub))
{
if (a[p1] <= a[p2])

b[p3++] = a[p1++];

else
b[p3++] = a[p2++];
}
while (p1 < mid)
{
b[p3++] = a[p1++];
}
while (p2 <= ub)
{
b[p3++] = a[p2++];
}
for (k = lb; k < p3; k++)
{
a[k] = b[k];
}
}

// Write a program to 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");
}

// Write a program to implement Queue using array


#include<stdio.h>
#include <stdlib.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;
}

// Write a program to perform linear and binary search


#include <stdio.h>

int main()
{
int n, a[5], i, b, key, low, high, mid;
printf("Which case you want: ");
scanf("%d", &n);
switch (n)
{
case 1:
printf("Linear Search\n");
printf("Enter the values:\n");
for (i = 0; i < 10; i++)
{
scanf("%d", &a[i]);
}
printf("Which value you want to search: ");
scanf("%d", &b);
for (i = 0; i < 10; i++)
{
if (a[i] == b)
{
break;
}
}
if (i < 10)
{
printf("Search is found at %d location",i);
}
else
{
printf("Search is not found");
}
break;

case 2:
printf("Binary Search\n");
printf("Enter the value of number: ");
scanf("%d", &n);
printf("Enter %d integers n:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter the value to find: \n");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low + high) / 2;
while (low <= high)
{
if (a[mid] < key)
low = mid + 1;
else if (a[mid] == key)
{
printf("\nThe number, %d found at Position
%d", key, mid + 1);
break;
}
else

high = mid - 1;

mid = (low + high) / 2;


}
if (low > high)
printf("\nThe number, %d is not found in given
Array", key);
}

return 0;
}

You might also like