Bubble Sort

So I am trying to write a code to take the three numbers, input from the user, and use a bubble sort to sort them in ascending order. Then take the middle value and out-put it. This is what I've got so far, but I can't think how to call the middle value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
int main(void)  
{  
cout << "Please input three numbers: " << endl;
float ValueA, ValueB, ValueC; //-- Create floats for the three inputs.


cout << "Number one: ";
cin >> ValueA;

cout << "Number two: ";
cin >> ValueB;

cout << "Number three: ";
cin >> ValueC;

int temp = -1; //-- Create a temporary storage for swapping numbers.


// Set parameters for the number of times the loop can run.
int end = 3;
int length = 3;
int input[] = {ValueA, ValueB, ValueC};


//-- Create loop to swap numbers in order, and run this loop three times

for(int counter = length - 1; counter > 0; counter--)
{
	for (int index = 0; index < end; index++)
	{
		if (input[index] > input[index + 1])
		{
			temp = input[index + 1];
			input[index +1] = input[index];
			input[index] = temp;
		}
	}
	end--;
}

cout << "The numbers in order are: ";
for(int index = 0; index < 3; index++)
{
	cout << input[index] << ", ";
}

//- Display the middle value
cout << "The middle value is: " < endl;

system("pause");
return 0;  
} // End main    
Last edited on
Are you required to use bubble sort? If not, use std::sort instead:
http://www.cplusplus.com/reference/algorithm/sort/
http://en.cppreference.com/w/cpp/algorithm/sort
Thanks, That worked alot better.
Topic archived. No new replies allowed.