sorting program with a series of if's

hello all,

I have to do a project which takes uses only if statements to sort 3 numbers into biggest to smallest.

I have to admit to having no idea where to even begin planning this program. Much less writing it.

I need help planning this thing. there need to be 3 integers (n1, n2, n3). and we can only use if statements, nothing else.

I could try starting it, but the only thing I can figure out we would need to use ifs to start with...something like:

1
2
3
4
5
6
7
8
9
10
11
12
13

if (n1 > n2)

if (n1 > n3)

if (n2 > n1)

if (n2 > n3)

if (n3 > n1)

if (n3 > n2)
compare n1 to n2 , if n2 is bigger swap them

compare n2 to n3 , if n3 is bigger swap them

just take some test cases, and follow your if statements to get some answers...

such as, 12, 15, 5
Last edited on
To sort three numbers you also have to compare and swap n1 and n2 again at the end. Example:
1 2 3
after first swap
2 1 3
after second swap
2 3 1
still not done
3 2 1
ow, sorry for the late reply. The forum didn't let me know that replies were here. Been working on another project.

how do you swap values?

can you reassign values on the fly? or would you have to go through another set of variables?
A bitwise operation or the easier:
swap(n1, n2);
i'm sorry, what is a bitwise operation?
you can simply make a swap function and use it

void swap(int &n1,int &n2){
int temp = n1;
n1 = n2;
n2 = temp;
}
closed account (zb0S216C)
mattig89ch wrote:
"what is a bitwise operation?"

A bitwise operation modifies the bits of its operand directly. Such operations are fast, but primitive, which all processors support. Bitwise operations are faster than division, multiplication, and addition. However, they can only manipulate integral variables (such as int, short, and long). They are mainly used for comparison, shifting, and logical shifting.

See here for information on bitwise operations: http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/CONCEPT/bitwise.html

Wazzak
Last edited on
Topic archived. No new replies allowed.