Thread: recursive insertion sort.

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    28

    recursive insertion sort.

    hi all,

    before i get any post about how pointless a recursive insertion is...i know it is...but it's for an assignment...and i've been playing around w/ it 'till the point where i can't tell what's wrong w/ it anymore...

    it's doing a semi sort...i think i'm forgetting to take into acct a case ..but either way...here's the code.

    being passed an object of the class list (w/h is a Linked List) and n which is the size of the list.

    most of this stuff is self explanatory...

    insert, inserts in list @ location specified.
    remove...removes @ location specified.

    here's my data,

    20 19 14 23 16 10 18

    and this is my output:

    10 14 16 18 20 19 23


    any help would be appreciated.

    oh yeah.. getvalue returns the value @ location specified.

    (class deals w/ either ints, float, or string but data type shouldn't matters it's a template after all)



    template<class T>
    void insort1(List<T>& list, int n)
    {
    if (n > 1)
    {
    insort1(list, n -1);

    int posn = n -1;

    while (posn >= 0 && (list.getvalue(n) < list.getvalue(posn)) )
    posn--;

    posn++;

    T temp;

    temp = list.getvalue(n);

    list.remove(n,temp);
    list.insert(posn,temp);


    }
    }

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    28

    Duh!

    let me know if i need to post the full source, it could help.
    curiousity killed the cat, but it
    makes for one hell of a programmer.

    Alien.

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Doing it this way might be easier

    First check to see if the node of a list is empty or there is just one element. Then call insort on node->next. Do a sorted insert of node->data into the just sorted list node->next. Then remove node from the list. Or you could do it so that your just swapping pointers around.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    Put all numbers on a different line in a file and do this:
    Code:
    #include <fstream.h>
    #include <iostream.h>
    
    int main()
    {
            int line1, line2, line3, line4, line5, line6, line7;
    		char file[1024];
    
    		cout<<"Please enter the file path:";
    		cin>>file;
    
            ifstream fin(file);
            fin >> line1;
            fin >> line2;
            fin >> line3;
            fin >> line4;
            fin >> line5;
            fin >> line6;
            fin >> line7;
    
            ofstream fout(file);
    
            fout << line6;
            fout << "\n";
            fout << line3;
            fout << "\n";
            fout << line5;
            fout << "\n";
            fout << line7;
            fout << "\n";
            fout << line1;
            fout << "\n";
            fout << line2;
            fout << "\n";
            fout << line4;
    
    		return 0;
       }

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. 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
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Recursive Insertion Sort
    By help me! in forum C Programming
    Replies: 1
    Last Post: 04-02-2003, 03:45 PM