Thread: Insertion sort... Please help!

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    Insertion sort... Please help!

    So for an assignment I'm supposed to have my insertion sort program take numbers from a file (in this case infile.txt) and sort them using insertion sort then output the array.

    This is what I currently have:
    Code:
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
       ifstream fin("infile.txt");
       int a[6], length, number, i;
            for(int j=1;j<length;j++)
            {
            number=a[j];
            i=j-1;
                    while(a[i]>number && i>=0)
                    {
                            a[i+1]=a[i];
                            i--;
                    }
            a[i+1]=number;
            }
    
       cout << a[i] << "\n";
       return 0;
    }
    When the infile.txt file lists the numbers:
    5
    6
    98
    7
    5
    6
    1
    8
    6
    4

    However, as an output I am just getting the number 13... Does anyone know why this would be and how I would go about fixing it?
    THANK YOU SO MUCH!!!!!!!!!!!

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    im not good at programming much and im not familiar with file i/o but where are you recording the numbers from the file

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    you need to fill int a[6], length, number, i, with some values before you can use them. (eg. by taking input from fin)

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    and im guessing you would get a different number besides 13 everytime, maybe idk or no output

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Quote Originally Posted by cyberfish View Post
    you need to fill int a[6], length, number, i, with some values before you can use them. (eg. by taking input from fin)
    Do you mean like declare length = to a specific number?

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    i dont know the syntax or anything but get all those numbers from the file stream into int array!

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    I'll have to google that one :-p

  8. #8
    Registered User
    Join Date
    Nov 2007
    Location
    Stoneham,Québec
    Posts
    10
    Ok well I dont really get wut u want to do... But if you wanna read in a file and put the values in an array here is a simple way to do it :
    Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	string filename="file";
    	int numbers;
    	vector <int> array;   
       	
       	ifstream Read;
       	Read.open(filename.c_str());
       
       	if(Read.fail()) 
    	{
          		cerr << "Error couldnt open the file" << filename << endl;
          		exit(EXIT_FAILURE);
       	}
       		
       	while(Read >> numbers) 
       	{
          		array.push_back(numbers);	
       	}
       
       	for(int i=0;i<array.size();i++)
       	{
       		cout << array[i]<<endl;
       	}
    
                  Read.close();
    return 0;
    }
    And about sorting em... I dont get what you want to do...
    Last edited by eezaa; 11-29-2007 at 06:39 PM.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    he is trying to sort those numbers in order using "insertion" method,,there are a couple other methods

    i remember learning about them back in high school, i dont remember if that was the efficient method but w/e

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The first problem is that you're not actually reading anything in from the file.
    The second problem is that you're only outputting one number (you need a loop to output them all).

    The sorting algorithm itself looks correct, from memory.
    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"

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, if you want to sort 10 numbers, you probably need an array bigger than 6?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-02-2008, 06:23 AM
  2. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  3. Insertion Sort on Array of Structs
    By n0r3gr3tz in forum C Programming
    Replies: 3
    Last Post: 04-01-2008, 08:28 AM
  4. Insertion Sort Problem
    By silicon in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2005, 12:30 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM