How to write a C Program to merge and sort two arrays in C Programming Language ?
This C Program to merge and sort two arrays.
Solution:
- #include<stdio.h>
- void sort(int a[],int);
- void main()
- {
- int a[20],c[10],i,m,n,j;
- printf("Enter the no.of elements in 1st array- \n");
- scanf("%d",&m);
- printf("Enter the 1st array - \n");
- for(i=0;i<m;i++)
- scanf("%d",&a[i]);
- printf("Enter the no.of elements in the 2nd array");
- scanf("%d",&n);
- printf("Enter the 2nd array");
- for(i=0;i<n;i++)
- scanf("%d",&c[i]);
- sort(a,m);
- printf("1st sorted array - \n");
- for(i=0;i<m;i++)
- printf("%d, ",a[i]);
- sort(c,n);
- printf("2nd sorted array - \n");
- for(i=0;i<n;i++)
- printf("%d, ",c[i]);
- j=m;
- for(i=0;i<n;i++)
- {a[j]=c[i];
- j++;}
- sort(a,m+n);
- printf("Merged and Sorted array - \n");
- for(i=0;i<m+n;i++)
- printf("%d, ",a[i]);
- }
- void sort(int a[],int n)
- {
- int i,j,temp;
- for(i=0;i<n;i++)
- { for(j=i+1;j<n;j++)
- if(a[i]>a[j])
- { temp=a[i];
- a[i]=a[j];
- a[j]=temp;
- }
- }
- }
Learn More :
Merge
Array
- How to pass one dimensional array to function in c.
- Write a c program which passes two dimension array to function.
- C Program Array Index Example
- C Program Array Example: Average
- C Program Array Example: Reverse
- C Program Array Example
- C Program to Array
- C Program Array NxM Elements Geometric/Arithmetic
- C Program To Find The Maximum And Minimum Value In An Array
- C Program To Find Union & Intersection Of Two Array
- C Program To Search A Number Inside The Array
- C Program To Sort An Array In Ascending And Descending Order
- C Program To Sort An Array Of Names In Alphabetical And Reverse Order
- C Program To Copy One Character Array Into Another
- Returns: array of decoded values. [0] - count of values
- C Program to Check if a number is in an array[1000]
- C Program to Find max and min in array using pointer concept
- C Program Sort Array By Segment
- C Program to sort an array using bubble sort
- C Program to find smallest in an array using pointer
- Un-sortiertes Array and Sortiertes Array
- C Program to Array Deserialization
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- Input reads in the array positions and range for random generation.
Sort
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C Program Sort Example
- C Program Shell Sort Using C Programming Language
- C Program Selection Sort Using C Programming Language
- C Program to Bubble Sort Using C Programming Language
- C Program Insertion Sort Using C Programming Language
- Heap Sort Using C Programming Language
- C Program To Sort An Array In Ascending And Descending Order
- C Program To Sort An Array Of Names In Alphabetical And Reverse Order
- C Program Sort Array By Segment
- C Program to sort an array using bubble sort
- Un-sortiertes Array and Sortiertes Array
- matrix sort in C Program
- C program that receives 10 float numbers from the console and sort them in non-ascending order, and prints the result
- C Program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order
- C Program to accept n numbers from user,store these numbers into an array & sort the number of an array
- C Program to Implement Quick Sort
- QuickVSInsertion Sorting C Program