# recursive insertion sort.

• 03-22-2002
Alien_Freak
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);

}
}
• 03-22-2002
Alien_Freak
Duh!
let me know if i need to post the full source, it could help.
• 03-23-2002
Nick
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.
• 03-23-2002
Denethor2000
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];

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;
}