sorting

Pages: 12
1.)
When I said put an input outside the loop, I meant an input before the loop. You get your first number, then set the low and high variables to that first input.

Inside the loop, you'll set low to the number if it's lower than low, high to the number if it's higher than high. You want to put this in the loop. The loop will have to be adjusted for this extra input.

2.)
1
2
3
4
5
6
7
8
string names[10];

//get input

//sorting
string temp = names[x];
names[x] = names[y];
names[y] = temp;


That allows you to swap the values in x and y, but that's unnecessary. There are sorting functions that you can use on the array.
ok i'll reply back as soon as i got back to my work desk

im currently off of it as of now so i cant try it.
at number 1.)

puting an input before the loop will it be like this?

1
2
3
4
5

cout<<"enter number";
cin>>n1;
for(i=0;i<a1;i++)
//the same code from the previous post 


but doin that will defy the loop

like lets say he input that he will input 5 numbers ofcourse he will enter 5 but because there is another input it will be a total of 6.

how would i avoid that?


and as for number 2.

lets skip that till later.
I'm assuming you're already done with this problem as I haven't been on in a while, but the solution is easy.

1
2
3
cout << "Enter number:  ";
cin >> n1;
for(int i = 0; i < a1 - 1; i++)
There are many sorting algorithms. I like to use a recursive one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
template<class type1, class type2> //list type, object contained type
type1 sort_t(type1& t1)
{
    type2 listob;
    if(t1.size() < 2) //nothing to sort
    {
         return t1;
    }
    listob = *t1.begin(); //dereference the beginning iterator and assign it to our temp storage
    t1.erase(t1.begin());
    for(type1::iterator it = t1.begin(); it != t1.end(); it++)
    {
         if(*it < listob)
         {
              swap(listob, *it);
         }
     }
     //now we have taken out the item of least value.
     sort_t(t1);
     try
     {
           t1.push_back(listob);
     }
     catch(...)
     {
           try
           {
                t1 += listob;
           }
           catch(...)
           {
                return {};
           }
    }
    return t1;
}


This should work with either a string or a vector, but no garuntees can be made (do to member functions of those objects being different for appending items).

if you're using an array, you can modify the code above.
Last edited on
Topic archived. No new replies allowed.
Pages: 12