ascending..

how to create a program that can sort it in ascending...

ex: input a num: 12 31 4 1 8
output: 1 4 8 12 31


thanks! for those who give the whole program.. heheh
Um... I'd rather not give the full solution, and for a good reason. How are you supposed to learn that way?

I'll give you a psudocode implementation of a sorting algorithm, namely one called Bubble sort. It's not very fast, but it's simple and easy to implement.

1
2
3
4
5
6
7
8
9
10
11
procedure bubbleSort( A : list of sortable items )
  do
    swapped = false
    for each i in 1 to length(A) - 1 inclusive do:
      if A[i-1] > A[i] then
        swap( A[i-1], A[i] )
        swapped = true
      end if
    end for
  while swapped
end procedure


See if you can translate this into C++, and you'll be well on your way to creating that program!

-Albatross
If you don't need to do it yourself, use either the STL sort(v.begin(),v.end()) template, or the qsort function.
@albatross.. okei i'll try.. thanks!!

@rocketboy.. im not so familiar in the STL, im still begginer.. heheh thanks!!
@albatross... i cant translate it into c++... maybe i'll study it first.. huhu T.T
Topic archived. No new replies allowed.