C + + Merging algorithm instance _c language

Source: Internet
Author: User

This example describes the C + + merge algorithm. Share to everyone for your reference. as follows:

/* Merge algorithm: combine two or more linear tables together to form a new linear table function template of the basic use of the program intention: two of the same type of linear table elements in order, and then combine them into a row of linear table * * * #include <iostream> us
ing namespace std; const int n = 5; 5 elements//output data element template <class t1> void output (T1 out[(2*n)]) {for (int i=0; i< (2*n); i++) {cout<&
  lt;out[i]<< "";
} cout<<endl;
  }//input data element template <class t2> void input (T2 in[n]) {cout<< "Please enter 5 data elements:";
  for (int i=0; i<n; i++) {cin>>in[i]; 
  cout<< "";
} cout<<endl; The//template function enters linear table elements and sorts them template <class t3> void Mysort (T3 a[2*n]) {int temp;//Interchange Data TEMP variable//bubble anchor for (int i=0 ; i<2*n-1;
    i++) {for (int j=0; j<2*n-1-i; J + +) {if (a[j]>a[j+1)) {temp = A[j];
    A[J] = a[j+1]; 
    A[J+1] = temp; 
  "}}//Template functions merge template <class t> void mergelist (t la[n], T lb[n], T lc[(2*n)]) {int i = 0;//as Subscript for La int j = 0; lb subscript int k = 0; LC Subscript//Synthesis of La lb group while (I<n &&J<n) {if (La[i] < lb[j]) {lc[k] = La[i];
    k++;
   LC[K] = Lb[j];
     else {if (la[i] = = Lb[j]) {lc[k] = La[i];
     k++;
    LC[K] = Lb[j];
     else {Lc[k] = lb[j];
     k++;
    LC[K] = La[i];
   }}//subscript downward move i++;
   j + +;
  k++;
  //If the data in LA is not finished, and LA is longer than the LB, insert the remaining elements of La into the Lc here is an extension while (i<=n) {lc[k++] = la[i++];
  //If the data in LB is not finished and the LB is longer than LA, insert the remaining elements of LB into the Lc while (j<=n) {lc[k++] = lb[j++]; 
///To sort the grouped elements mysort (Lc);
 int main () {int a1[n],a2[n], a[(2*n)];
 Double B1[n], b2[n],b[(2*n)];
 Char M1[n], m2[n], m[(2*n)];
 Input data Merge Output/*input (A1); 
 InPut (A2);
 Mergelist (A1,a2,a); OutPut (a);
 */InPut (M1); 
 InPut (m2);
 Mergelist (M1,M2,M); 
 OutPut (m);
 System ("pause");
return 0; 

 }

I hope this article will help you with your C + + programming.

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.