Merging of arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dontknow
    New Member
    • Apr 2010
    • 2

    Merging of arrays

    Hello, I have 2 arrays unsorted, and I need to merge them together and then sorted. But I really don´t know, how to do it. Could you please help me? Thanks.
    Code:
    //Array 1:
    int i=0; 
    int *array=new int[14];
    int y = 14;
    int x = 0;
    ifstream in("somefile.txt", ios::in |ios::binary); 
    while(in >> array[x] && x < y)
    {x++;}
    for(i = 0; i < 14; i++)
    { cout << array[i] << endl;}
    
    //Array 2:
    int j=0; 
    int *arra=new int[14];
    int z = 14;
    int h = 0;
    ifstream in("somefile2.txt", ios::in |ios::binary); 
    while(in >> arra[h] && h < z)
    {h++;}
    for(j = 0; j < 14; j++)
    { cout << arra[j] << endl;}
    Last edited by Frinavale; Apr 27 '10, 09:34 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    Sorting

    The topic of sorting in computer science is considerable and there are many different methods on how to best sort an array in terms of speed and memory. The wikipedia article (below) provides generic details and psuedocode on many of the sorting algorithms that are in use today. I would begin by looking over the article and picking one that you think you can implement.


    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      I think that you would need to sort each container before merging

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Are you guaranteed to get the same length of elements in both arrays?

        Either way, since you have two seperate arrays already, sort them seperately before merging them for better efficiency.

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          Given that a string is an array:
          Code:
          string s="12487369";
          string s2="59214603";
          s3=s+s2="1248736959214603";
          sort(s3);
          s3="0112233445667899";
          //now convert s3 to integers using atoi() inside a for loop with its condition specified in terms of s3.length().

          Comment

          Working...