Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
46 views5 pages

Experiment 01

The document describes three experiments to find the median of two sorted arrays: 1) Using merging - it merges two input arrays into a single sorted output array and finds the median 2) Using merge sort - it sorts the concatenated array using merge sort and finds the median 3) Using binary search - it first merges the two sorted arrays, then uses binary search to find the target element's index in the merged sorted array.

Uploaded by

Viney Chhillar
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)
46 views5 pages

Experiment 01

The document describes three experiments to find the median of two sorted arrays: 1) Using merging - it merges two input arrays into a single sorted output array and finds the median 2) Using merge sort - it sorts the concatenated array using merge sort and finds the median 3) Using binary search - it first merges the two sorted arrays, then uses binary search to find the target element's index in the merged sorted array.

Uploaded by

Viney Chhillar
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/ 5

Experiment-01

Aim: Given two sorted arrays nums1 and nums2 of size m and
n respectively, return the median of the two sorted arrays

Using merging

#include <stdio.h>
int main()
{
int n1,n2,n3;
int a[1000], b[1000], c[1000];
printf("Enter the size of first array: ");
scanf("%d",&n1);
printf("Enter the array elements: ");
for(int i = 0; i < n1; i++)
scanf("%d", &a[i]);
printf("Enter the size of second array: ");
scanf("%d",&n2);
printf("Enter the array elements: ");
for(int i = 0; i < n2; i++)
scanf("%d", &b[i]);
n3 = n1 + n2;
for(int i = 0; i < n1; i++)
c[i] = a[i];
for(int i = 0; i < n2; i++)
c[i + n1] = b[i];

printf("The merged array: ");


for(int i = 0; i < n3; i++)
printf("%d ", c[i]);

printf("\nFinal array after sorting: ");


for(int i = 0; i < n3; i++){
int temp;
for(int j = i + 1; j < n3; j++) {
if(c[i] > c[j]) {
temp = c[i];
c[i] = c[j];
c[j] = temp;
}
}
}
for(int i = 0; i < n3 ; i++)
printf(" %d ",c[i]);
return 0;
}

Using Merge Sort

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

int main() {
// array1
int array1[] = {1,41,6,80,98};
//array2
int array2[] = {22,33,5,47,10};

int len = sizeof(array1) / sizeof(array1[0]) + sizeof(array2) /


sizeof(array2[0]);
int array3[len];
for (int i = 0, j = 0, k = 0; k < len;) {

if (array1[i] < array2[j])


array3[k++] = array1[i++];
else
array3[k++] = array2[j++];
}

printf("The sorted Array in ascending order : ");


for (int k = 0; k < len; k++) {
printf("%d ", array3[k]);
}

return 0;
}

Using Binary Search

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

int binarySearch(int arr[], int size, int target) {


int left = 0, right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return -1;
}

void mergeSortedArrays(int arr1[], int size1, int arr2[], int size2,


int merged[]) {
int i = 0, j = 0, k = 0;

while (i < size1 && j < size2) {


if (arr1[i] < arr2[j]) {
merged[k++] = arr1[i++];
} else {
merged[k++] = arr2[j++];
}
}

while (i < size1) {


merged[k++] = arr1[i++];
}

while (j < size2) {


merged[k++] = arr2[j++];
}
}

int main() {
int arr1[] = {3, 8, 12, 15, 20};
int size1 = sizeof(arr1) / sizeof(arr1[0]);

int arr2[] = {6, 10, 13, 18};


int size2 = sizeof(arr2) / sizeof(arr2[0]);

int merged[size1 + size2];

mergeSortedArrays(arr1, size1, arr2, size2, merged);


printf("Merged Sorted Array: ");
for (int i = 0; i < size1 + size2; i++) {
printf("%d ", merged[i]);
}
printf("\n");

int target = 15;


int index = binarySearch(merged, size1 + size2, target);

if (index != -1) {
printf("Found %d at index %d.\n", target, index);
} else {
printf("%d not found.\n", target);
}

return 0;
}

You might also like