Please use C program C program please C program only using r
Please use C program
C program please
C program only using recursive array...
Write a program that generates two arrays with double numbers (randomly). Then, write a function to sort each array utilizing RECURSIVE selection sort (http://www.tutorialspoint.com/data_structures_algorithms/selection_sort_algorithm.htm will give you an idea about selection sort). Then write a function that will merge the contents of the two sorted (ascending order) arrays, storing the result in a third array (still in ascending order). The function should not assume that both input arrays are the same length, but can assume that one array does not contain two copies of the same value. The resultant third array should also contain no duplicate values. Utilize pass by reference among the functions. Your code must utilize functions for the best possible structure.
Solution
#include <stdio.h>
//Merge sort
int merge(int a[], int m, int b[], int n, int sorted[])
{
int i, j, k;
j = k = 0;
for (i = 0; i < m + n;)
{
if (j < m && k < n)
{
//Compares for small value
if (a[j] < b[k])
{
sorted[i] = a[j];
j++;
}
//Compares for big value
else if(a[j] > b[k])
{
sorted[i] = b[k];
k++;
}
//Compares for equal value
else if(a[j] == b[k])
{
sorted[i] = b[k];
k++; j++;
}
i++;
}
//for rest of the element
else if (j == m)
{
for (; i < m + n;)
{
sorted[i] = b[k];
k++;
i++;
}
}
else
{
for (; i < m + n;)
{
sorted[i] = a[j];
j++;
i++;
}
}
}
return i;
}
//Selection sort
void selection(int list[], int i, int j, int size, int flag)
{
int temp;
if (i < size - 1)
{
if (flag)
{
j = i + 1;
}
if (j < size)
{
if (list[i] > list[j])
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
selection(list, i, j + 1, size, 0);
}
selection(list, i + 1, 0, size, 1);
}
}
int main()
{
int list1[30], list2[30], sorted[30], size, temp, i, j, len;
printf(\"\ Enter the size of the list1: \");
scanf(\"%d\", &size);
printf(\"\ Enter the elements in list1:\ \");
for (i = 0; i < size; i++)
{
scanf(\"%d\", &list1[i]);
}
selection(list1, 0, 0, size, 1);
printf(\"\ The Selection sort list1 in ascending order is\ \");
for (i = 0; i < size; i++)
{
printf(\"%d \", list1[i]);
}
printf(\"\ Enter the elements in list2:\ \");
for (i = 0; i < size; i++)
{
scanf(\"%d\", &list2[i]);
}
selection(list2, 0, 0, size, 1);
printf(\"\ The Selection sort list1 in ascending order is\ \");
for (i = 0; i < size; i++)
{
printf(\"%d \", list2[i]);
}
len = merge(list1, size, list2, size, sorted); // sort the array
printf(\"\ Merge Sorted array:\ \"); // print sorted array
for(i=0; i<len-1; i++)
printf(\"%d \",sorted[i]);
return 0;
}
Output:
Enter the size of the list1: 5
Enter the elements in list1:
11
22
33
44
55
The Selection sort list1 in ascending order is
11 22 33 44 55
Enter the elements in list2:
10
20
30
40
55
The Selection sort list1 in ascending order is
10 20 30 40 55
Merge Sorted array:
10 11 20 22 30 33 40 44 55


