bubble sort

Hi ,I have some problems with making this work properly
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
for(l=n;l>1;l--) flag=0;
	

	for(i=0;i<(l+1);i++)
	{
		if(sort[i] > sort[i+1])
		{
			buf=sort[i+1];
			sort[i+1]=sort[i];
			sort[i]=buf;
	
		}

         flag=1;

	
	}

	if(!flag) exit(1);

Can you pls tell me what's wrong with the code?
1. It's only a fragment (post the rest)
2. It loops setting flag = 0 n times (I think you forgot the {}'s)
3. flag doesn't seem to have any real purpose
4. the "inner" for loop probably loops once too many
5. sort[i+1] probably goes out of bounds
Bubble sort as any quadratic sort has two nested loops. Check this tutorial for guiadance: http://simpleprogrammingtutorials.com/tutorials/sorts/bubble-sort.php
Topic archived. No new replies allowed.