Thread: Why doesn't this sort???

  1. #1
    Village id10t
    Join Date
    May 2008
    Posts
    57

    Why doesn't this sort???

    Hey everyone,

    Please can someone point me in the right direction on why this wont sort (small to large). Just a hint please!

    Code:
    #include <fstream> //file handling
    #include <iostream> //standard
    #include <cstdlib> //for exit
    #include <vector> //for vectors
    
    using namespace std;
    
    int main()
    {
    
    ifstream in_stream1,in_stream2;
    ofstream out_stream;
    char OutFileName[16];
    vector<int> Numbers1,Numbers2;
    int input,UsedSpace,counter;
    
    
    
    
    in_stream1.open("InputFile1.dat");
    in_stream2.open("InputFile2.dat");
    
    if (in_stream1.fail() || in_stream2.fail())
    {
        cout<<"Error opening file."<<endl;
        exit(1);
    }
    
    
    while (in_stream1 >> input || in_stream2 >> input) 
    {
        Numbers1.push_back(input);
        Numbers2.push_back(input);
    }
    
    if (Numbers1.size()>Numbers2.size())
        {
        UsedSpace=Numbers1.size();
        }
        else UsedSpace=Numbers2.size();
    
    for (counter=0;counter<UsedSpace;counter++)
        {
        if (Numbers1[counter]<Numbers2[counter])
            {
            cout<<Numbers1[counter]<<" ";
            }
        else cout<<Numbers2[counter]<<" ";    
    }
    
    in_stream1.close();
    in_stream2.close();
    
    
    return 0;
    }

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Because you have not told the source to sort anything.

    Soma

  3. #3
    Village id10t
    Join Date
    May 2008
    Posts
    57
    Code:
    for (counter=0;counter<UsedSpace;counter++)
        {
        if (Numbers1[counter]<Numbers2[counter])
            {
            cout<<Numbers1[counter]<<" ";
            }
        else cout<<Numbers2[counter]<<" ";
    sorry i may be a complete idiot but isnt this supposed to print the numbers from the two vectors in ascending order?

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    sorry i may be a complete idiot but isnt this supposed to print the numbers from the two vectors in ascending order?
    To be honest... I didn't think the source would do anything at first. It certainly made me do a... many, many take.

    1) Both containers will always have the same contents.
    2) The output only compares the elements in one container with the other and prints the smallest of each examination.

    I'm astonished that you've arrived at this code. It is evil.

    Soma

  5. #5
    Village id10t
    Join Date
    May 2008
    Posts
    57
    Quote Originally Posted by phantomotap View Post

    I'm astonished that you've arrived at this code. It is evil.

    Soma
    LOL, im still really green and find it difficult to tell the difference between the light side and dark side of the force.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Start by getting the code to read in the data correct. I would read each file in with a separate loop.

    Verify that it works by adding code to output each vector without sorting them.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    LOL, im still really green and find it difficult to tell the difference between the light side and dark side of the force.
    Gods help us all... if you produced things of this foul quality as an expert... *shudder*

    Soma

  8. #8
    Village id10t
    Join Date
    May 2008
    Posts
    57
    Ok. I verified that the input from both files is correct, so im pretty sure i have got that part sorted
    Any advice? Should i introduce a third vector and try to sort the other two into it?

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by MarlonDean View Post
    Ok. I verified that the input from both files is correct, so im pretty sure i have got that part sorted
    Any advice? Should i introduce a third vector and try to sort the other two into it?
    Why do you have two vectors to begin with? If having it all sorting in one is what you've wanted all along, then just get rid of the second one and put everything in the first one then std::sort that.
    Or are you trying to do some kind of external Merge Sort?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, if you just want to get it done, there's an algorithm for this: merge. (Using algorithms needs quite good knowledge of C++, though.)

    If you want to implement it yourself, you'll need to keep two indices (I suppose), and pick the item and advance the respective index from one of the containers (the one that currently has the smaller item.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Village id10t
    Join Date
    May 2008
    Posts
    57
    is it possible to have 2 files sort into one vector? It seems you know alot about sorting iMalc (had a look at your homepage)

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by MarlonDean View Post
    is it possible to have 2 files sort into one vector? It seems you know alot about sorting iMalc (had a look at your homepage)
    I was just thinking that you could read items from one file into the vector then close that file and open the second one and continue reading items from that file into the vector. Then you can simply sort the single vector on its own. But if the first two files are already sorted, then merging is definitely what you want.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM
  4. radix sort and radix exchange sort.
    By whatman in forum C Programming
    Replies: 1
    Last Post: 07-31-2003, 12:24 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM