By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,009 Members | 2,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,009 IT Pros & Developers. It's quick & easy.

Ordering Numbers in C

P: 2
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.
Feb 12 '08 #1
Share this Question
Share on Google+
5 Replies


gpraghuram
Expert 100+
P: 1,275
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.

If you use an existing library try to use qsort which is part of the C library(stdio/stdlib)


Raghuram
Feb 12 '08 #2

Ganon11
Expert 2.5K+
P: 3,652
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.
Feb 12 '08 #3

P: 47
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.
Feb 12 '08 #4

P: 2
Its still an early intro lvl in C, so i couldnt use any functions and had to use the A<B<C<D ect method. worked out pretty good w/ four ints. thanks all
Feb 14 '08 #5

P: 1
Get a site which includes many math games for free.
ordering numbers games
to the main site:
Online maths games
Aug 1 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.