Like Tree1Likes
  • 1 Post By DavX

Various sorting algorithms with variable length data

This is a discussion on Various sorting algorithms with variable length data within the C++ Programming forums, part of the General Programming Boards category; Populating Arrays... <25000 elements> Sorting ERROR: There are too many items in the file. (ARRAY_SIZE = 25000) SUMMARY RESULTS Elapsed ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    9

    Various sorting algorithms with variable length data

    Populating Arrays... <25000 elements>
    Sorting
    ERROR: There are too many items in the file. (ARRAY_SIZE = 25000)
    SUMMARY RESULTS
    Elapsed Time <BubbleSort> : 1303 milliseconds
    ERROR: There are too many items in the file. (ARRAY_SIZE = 25000)
    Elapsed Time <Selection Sort >: 733 milliseconds
    ERROR: There are too many items in the file. (ARRAY_SIZE = 25000)
    Elapsed Time <Insertion Sort >: 473 milliseconds

    this is the output i am getting for 25000 elements

    I need to resolve the issue i am having with 25000 elements. i keep getting too many file error. here is part of my code. i can paste more code if it is necessary. thank you for your time and help

    Code:
    #define INFILE1    "NumFile100K.txt"
    #define INFILE2    "NumFile25K.txt"
    #define INFILE3    "NumFile500.txt"
    #define INFILE4    "NumFile5K.txt"
    
    #define ARRAY_SIZE1 100000
    #define ARRAY_SIZE2 25000
    #define ARRAY_SIZE3 500
    #define ARRAY_SIZE4 5000
    
    int* read(int , string, int* );
    
    
    void BubbleSort(int*, int);
    void SelectionSort(int*,int);
    void InsertionSort(int*,int);
    
    
    int main() 
    {
        clock_t beginTime = 0; 
        clock_t endTime = 0;
        clock_t elapsedTime = 0;
        long secondsElapsed = 0;
        long milsElapsed = 0;
        // wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
        cout<<" Populating Arrays... <500 elements> "<<endl;
        cout<<" Sorting "<<endl;
    
        int list500[500];
        read(ARRAY_SIZE3,INFILE3 ,list500);
        beginTime = clock();
        BubbleSort(list500, ARRAY_SIZE3);
        endTime = clock();
        cout << " SUMMARY RESULTS" << endl;
        elapsedTime = endTime - beginTime;
        cout << " Elapsed Time <BubbleSort> " <<setw(6)<< ": " << elapsedTime<<" milliseconds"<<endl;
    
        read(ARRAY_SIZE3,INFILE3 ,list500);
        beginTime = clock();
        SelectionSort(list500, ARRAY_SIZE3);
        endTime = clock();
        elapsedTime = endTime - beginTime;
        cout << " Elapsed Time <Selection Sort >: " << elapsedTime<<" milliseconds"<<endl;
    
        read(ARRAY_SIZE3,INFILE3 ,list500);
        beginTime = clock();
        InsertionSort(list500, ARRAY_SIZE3);
        endTime = clock();
        elapsedTime = endTime - beginTime;
        cout << " Elapsed Time <Insertion Sort >: " << elapsedTime<<" milliseconds"<<endl;
    
        //wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
        cout<<endl<<endl<<" Populating Arrays... <5000 elements> "<<endl;
        cout<<" Sorting "<<endl;
    
        int list5000[5000];
        read(ARRAY_SIZE4,INFILE4 ,list5000);
        beginTime = clock();
        BubbleSort(list5000, ARRAY_SIZE4);
        endTime = clock();
        cout << " SUMMARY RESULTS" << endl;
        elapsedTime = endTime - beginTime;
        cout << " Elapsed Time <BubbleSort> " <<setw(6)<< ": " << elapsedTime<<" milliseconds"<<endl;
    
        read(ARRAY_SIZE4,INFILE4 ,list5000);
        beginTime = clock();
        SelectionSort(list5000, ARRAY_SIZE4);
        endTime = clock();
        elapsedTime = endTime - beginTime;
        cout << " Elapsed Time <Selection Sort >: " << elapsedTime<<" milliseconds"<<endl;
    
        read(ARRAY_SIZE4,INFILE4 ,list5000);
        beginTime = clock();
        InsertionSort(list5000, ARRAY_SIZE4);
        endTime = clock();
        elapsedTime = endTime - beginTime;
        cout << " Elapsed Time <Insertion Sort >: " << elapsedTime<<" milliseconds"<<endl;
    
        //wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
        cout<<endl<<endl<<" Populating Arrays... <25000 elements> "<<endl;
        cout<<" Sorting "<<endl;
    
        int list25000[25000];
        read(ARRAY_SIZE2,INFILE2 ,list25000);
        beginTime = clock();
        BubbleSort(list25000, ARRAY_SIZE2);
        endTime = clock();
        cout << " SUMMARY RESULTS" << endl;
        elapsedTime = endTime - beginTime;
        cout << " Elapsed Time <BubbleSort> " <<setw(6)<< ": " << elapsedTime<<" milliseconds"<<endl;
    
        read(ARRAY_SIZE2,INFILE2 ,list25000);
        beginTime = clock();
        SelectionSort(list25000, ARRAY_SIZE2);
        endTime = clock();
        elapsedTime = endTime - beginTime;
        cout << " Elapsed Time <Selection Sort >: " << elapsedTime<<" milliseconds"<<endl;
    
        read(ARRAY_SIZE2,INFILE2 ,list25000);
        beginTime = clock();
        InsertionSort(list25000, ARRAY_SIZE2);
        endTime = clock();
        elapsedTime = endTime - beginTime;
        cout << " Elapsed Time <Insertion Sort >: " << elapsedTime<<" milliseconds"<<endl;
    
        //wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
        cout<<endl<<endl<<" Populating Arrays... <100000 elements> "<<endl;
        cout<<" Sorting "<<endl;
    
        int list100000[100000];
        read(ARRAY_SIZE1,INFILE1 ,list100000);
        beginTime = clock();
        BubbleSort(list100000, ARRAY_SIZE1);
        endTime = clock();
        cout << " SUMMARY RESULTS" << endl;
        elapsedTime = endTime - beginTime;
        cout << " Elapsed Time <BubbleSort> " <<setw(6)<< ": " << elapsedTime<<" milliseconds"<<endl;
    
        read(ARRAY_SIZE1,INFILE1 ,list100000);
        beginTime = clock();
        SelectionSort(list100000, ARRAY_SIZE1);
        endTime = clock();
        elapsedTime = endTime - beginTime;
        cout << " Elapsed Time <Selection Sort >: " << elapsedTime<<" milliseconds"<<endl;
    
        read(ARRAY_SIZE1,INFILE1 ,list100000);
        beginTime = clock();
        InsertionSort(list100000, ARRAY_SIZE1);
        endTime = clock();
        elapsedTime = endTime - beginTime;
        cout << " Elapsed Time <Insertion Sort >: " << elapsedTime<<" milliseconds"<<endl;
        return 0;
    
    }
    
    int* read(int size , string file, int*storageArray)
    {
        int i = 0;
        
        for(i = 0; i < size; i++)
            storageArray[i] = 0;
        
        //  open the file
        ifstream inputHandle(file, ios::in);
        
        // check if the file opened
        if(inputHandle.is_open() == true) {
            // if the file opened, read integers until EOF (end of file) is encountered
            i = 0;
            while( !inputHandle.eof() ) {
                if(i < size) {
                    inputHandle >> storageArray[i];
                    
                    i++;
                }
                else {
                    cout << "ERROR: There are too many items in the file. (ARRAY_SIZE = " << size << ")" << endl;
                    break;
                }
            }
             inputHandle.close();
        }
    
        return storageArray;
    
    }
    Last edited by name12; 12-14-2012 at 08:28 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,711
    I suggest you think about a few more functions, you have quite a bit of duplicate code that could be eliminated.

    I also recommend you use std::vector instead of the arrays, or use dynamic memory instead of the static arrays.

    Please post the read() function and probably a sample of your input file.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    i have posted the read function now.

    can you give me an example code on how to do it.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    I have attached the files
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,711
    i keep getting too many file error.
    Then you have too many entries in your file.

    Edit: Probably being caused by using eof() to control your entry loop. This will usually result in having one extra item, because eof() isn't set until after you try to read past the end of the file. I recommend you use the actual read and the size of your array to control the reading loop.

    In future please don't edit your prior post to change the code, this makes following the posts harder.

    can you give me an example code on how to do it.
    An example of what? You seem to know how to create and use functions. For vectors start here: Vector. And for dynamic memory.

    Jim
    Last edited by jimblumberg; 12-14-2012 at 08:41 AM.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Take a close look at line 13733 of NumFile25k.txt - you have some non-numeric (possibly not visible in your text editor) characters.
    Beacuse your read loop was barfing over the input your eof() catch was never stopping the loop.
    This is a good example of why input verification is so important (and so difficult to get right)
    Salem likes this.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    i am not able to see anything at line 13733 of NUmfile25k. is there way i can see it in visual studio

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    OK, i fixed the value at line 13733, but i still keep getting error for 25000 elements. please help

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,652
    What is your current code? Are you still incorrectly using eof() to control the loop?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Sorting algorithms
    By pureflip428 in forum C++ Programming
    Replies: 3
    Last Post: 12-08-2012, 11:23 PM
  2. Question about sorting algorithms
    By lios1984 in forum C Programming
    Replies: 4
    Last Post: 01-15-2012, 11:03 PM
  3. Sorting algorithms
    By ToNy_ in forum C Programming
    Replies: 9
    Last Post: 12-14-2011, 09:27 AM
  4. Sorting Algorithms
    By ashir30000 in forum C++ Programming
    Replies: 9
    Last Post: 05-21-2009, 09:27 AM
  5. recommendations on sorting algorithms
    By btq in forum C++ Programming
    Replies: 4
    Last Post: 10-14-2002, 11:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21