Swap 3 ints

I have to make two function that swap values a,b,c
in first function just swap 2 values a,b if a > b
I got it as
1
2
3
4
5
6
7
8
9
10
11
void sort2(int& a, int& b)
{
	if (a > b)
	{
		int temp;
		temp = a;
		a = b;
		b = temp;
	}
	
}


Now I have to do for three values a, b, c and have to arrange them in
ascending order

1
2
3
4
5
6
7
8
9
void sort3(int& a, int& b, int& c)
{
   /*How do i do the swap and arrange them
     in ascending order
   */ 


	
}


please help
Last edited on
Just do the same again but with b and c. Call the function that swaps a and b. then the b and c one, then do both again.

I'm sure there's a better way of doing, that would allow the number of variables you can do this with to be increased but my way should work I think.
Also I think what you are trying to do is called a bubble sort. Google it.
If you can swap two elements, you can swap infinite elements.

All you need to do is pair-wise sort your elements in an iterative fashion.
So, for an array of elements e0 -> en, do pairwise comparisons of (e0,e1), (e1,e2) etc until you've reached the last element.

Do mind: if a swap is successful, you need to compare the swapped element to the one BEFORE that. Don't forget to take a step back!
Topic archived. No new replies allowed.