I need to order four numbers and print in ascending order. I'm having troubles coming up with an algorithm to solve this. I should be able to write the code if I got something to work off of. Thanks.
Ordering Numbers in C
Collapse
X
-
Originally posted by bigmacI need to order four numbers and print in ascending order. I'm having troubles coming up with an algorithm to solve this. I should be able to write the code if I got something to work off of. Thanks.
If you use an existing library try to use qsort which is part of the C library(stdio/stdlib)
Raghuram -
You can also solve this by writing down a comparison tree on your own. For instance, there are only a few possible orderings of 3 elements: A < B < C, A < C < B, B < A < C, B < C < A, C < A < B, and C < B < A. If you compare C to A and find C < A, can the ordering be either the first, second, or third possibility? At this point, you might compare B and A, and come up with 1 or 2 possibilities.
You can do a similar thing for four elements - I believe it is possible to order four elements using only five comparisons.Comment
-
First of all, numbers in an array or just 4 integers/doubles/...(you never know).
If it's an array you have two choises:
a) a bubble sort
b) a qsort (which is better)
A qsort is easier, but if this is for school and you have no experience with declaring functions you might find a bubble sort easier.
Which one you chose is up to you.
But I recommend the qsort.Comment
-
Get a site which includes many math games for free.
ordering numbers games
to the main site:
Online maths gamesComment
Comment