English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

4 answers

First make sure they are in an array or some kind of list. Then sort. There are many sorting algorithms such as the 'naive' bubble sort. Or look in the STL for sorting on a datatype. Bubble sort (not good):

for (i=1 to n-1; i++)
for (j=i to 1; j --)
if e(j)>e(j+1) swap e(j),e(j+1)

PS: Sure looks like the bubble sort on wikipedia? To make it decending, just change the > to <.

2007-12-20 20:24:30 · answer #1 · answered by Anonymous · 0 0

I believe the technique is called the bubble sort and you could try looking that up because i can't remember what sequence of code is needed to use it

2007-12-20 20:25:28 · answer #2 · answered by Anonymous · 0 1

You can use an implementation of a sorting algorithm, described here: http://en.wikipedia.org/wiki/Sorting_algorithm , with implementations here: http://en.wikibooks.org/wiki/Algorithm_implementation/Sorting .

Also, you can simply utilize the sort() C++ library methods.

Sample Code:
#include // for sort
int array[] = { ...... };
std::sort(array, array + arraySize);


P.S. Richard, your implementation is for selection sort not bubble sort.

2007-12-20 20:38:30 · answer #3 · answered by msaeed33 3 · 1 0

if you have small numbers you can also sort them by a distribution sort like this

int unsorted[1..n],hash[1..max(a)];
int sorted[1..n];
for i in 1..max(a)
...hash[i]=0;
for i in 1..n do
...hash[unsorted[i]]=1;
j=0;
for i in 1..max(a)
...if hash[i]==1 then
......sorted[j++]=i;

at the end of this procedure you will have all your
sorted numbers in the array "sorted".
it has complexity O(n) (it's very fast) but use it JUST IF
you have small numbers.

Good Luck!

2007-12-20 22:52:17 · answer #4 · answered by Anonymous · 1 0

fedest.com, questions and answers