# Arranging numbers in ascending order

• 12-18-2001
Aero
Arranging numbers in ascending order
How would I use C++ to arrange a set of 5 inputted values in ascending order and then cout the order? So far I have an idea of using an array, order[5], and then assigning the lowest number to order[0], second lowest number to order[1], etc. Then I would cout order[0], order[1], etc. I am having trouble on using C++ to find the second lowest number, the middle value, and the second highest number. Any help would be grateful.

Is there a faster way to do this besides with arrays?
• 12-18-2001
Leeman_s
yes
bubble sort maybe. here is what to do, its the basic idea:

//count is how many values were entered
int doagain=1;
do
{
doagain=0;
for(int i;i<=count;i++)
{
if(order(i)>order(i+1))
{
int temp=order(i);
order(i)=order(i+1);
order(i+1)=temp;
doagain=1
}
}
}while (doagain=1);
• 12-18-2001
Aero
I didn't learn bubble sort yet. I'm a beginner in C++. I know basics and for and while loops and arrays. Basically, I learned all the stuff up to Lesson 9 of the cprogramming.com tutorials.
• 12-19-2001
Unregistered
What you want is a sorting method, and since bubble sort is the simplest of them that would be the place to start. If you don't know how it works then break it down line by line to figure it out. If you haven't learned sorting yet, now is as good a time to start because it's one of the basic things that you learn when learning a language.
• 12-20-2001
Unregistered
Okay I figured it out the long way without sorts:

Code:

```int avg, a, b, c, d, e, f=0, v, w, x, y, z; cout<<"Input 5 numbers: "; cin>>a>>b>>c>>d>>e; v=a; w=b; x=c; y=d; z=e; cout<<"Here are the numbers in ascending order: "<<endl; while (f<5) {     v--;     w--;     x--;     y--;     z--;     if (v==0)     {           cout<<a<<endl;           f++;     }     if (w==0)     {           cout<<b<<endl;           f++;     }     if (x==0)     {           cout<<c<<endl;           f++;     }     if (y==0)     {           cout<<d<<endl;           f++;     }     if (z==0)     {           cout<<e<<endl;           f++;     } }```
• 12-20-2001
Aero
Oops I forgot to login. Oh well, that works right?
• 12-21-2001
Mecnels
Selection Sort
int help,j,k,num[5];

for(j=0;j<4;j++)
for(k=4;k>j;k--)
if(num[j]>num[k])
{
help=num[j];
num[j]=num[k];
num[k]=help;
}

/* Selection sort is faster than bubble sort. Only quick sort is faster. */
• 12-22-2001
durga101
Sorting
You are doing nothing but sorting. So, its best you pick up some sorting algorithms like bubblesort, insertion or selection.

Or advanced ones like Binary tree an Heap Sort.

I appreciate the idea of using while loop to do the soring (for only the idea, not for practical application.)