bubble sort

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
const int size = 5;

int main()
{	
	int i, j, a, tmp = 0;
	int table[size];

	for(i=0; i <size; i++)
	{
		cout << "enter num: ";
		cin >> table[i];
	}

	for(a=0; a <size; a++)
		for(j=0; j<size; j++)
	{
		if(table[a] > table[a+1])
		{	tmp = table[a];
                        table[a] = table[a+1];
                        table[a+1] = tmp;	
		}
	}
	
	for(j=0; j<size; j++)
		cout << table[i];

	return 0;
}


Can you please tell me what's the wrong in my code?
I debug it but the "table[size] was corrupted" that is the message which is show me again and again..
I want to sort an array that's all! please help!
table's valid array indices are 0-4 for an array of size 5.

The variable a loops from 0 to 4. You then access on lines 17 and 19 table[ a + 1 ], meaning elements 1-5. 5 is beyond the array bounds.
if i change the size in
for(j=0; j<size-1; j++)
it is now ok?
well...wouldn't running it be a good way to find out? =)
ok its works! thank you! :)
Topic archived. No new replies allowed.