Merging and sorting of C language

Source: Internet
Author: User

A sequence of two ascending (or descending) data sequences is merged into one that is still sorted in the original order.


On the code:

#include <stdio.h> #include <stdlib.h> #define m 6#define n 4int Main () {   int a[m]={-3,6,19,26,68,100}, b[ N]={8,10,12,22}; int i,j,k,c[m+n]; int l;  i=j=k=0;  printf ("element of array A: \ n");  for (l = 0; l < m; l++)  {  printf ("%d  ", A[l]);} printf ("Elements of the \NB array: \ n"); for (l = 0; l < n; l++)  {  printf ("%d  ", B[l]);} printf ("\ n" of the merged array element: \ n "), while (i<m && j<n)/    * Stores the smaller number in a, B array sequentially in the C array */    {if (A[i]<b[j]) {c[k ]=a[i]; i++;}     else {c[k]=b[j]; j + +;}     k++; }while (i>=m && j<n)/   * If all the data in a is complete, store all the remaining number B in C */{c[k]=b[j]; k++; j + +;} while (J>=n && I<M)   /* If all the data in B is complete, store the remaining number of a in C */{  c[k]=a[i]; k++; i++;} for (i=0;i<m+n;i++)    printf ("%d  ", C[i]); return 0;}
Execution Result:




Merging and sorting of C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.