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
29
30
31
32
33
34
35
36
#include<iostream>
#include<conio.h>
using namespace std;

int main() { 

	int arr_1 [10] ={1, 4, 7, 15, 2, 44, 3, 22, 21, 6};
	int i, temp;
	int swp;
	
	for (i =0; i<10; i++)
	{
		cout<<arr_1[i]<<endl;
	} 
	do
	{
		swp=1;
		for (i=0;i<9;i++)
		{if (arr_1[i]>arr_1[i+1]) 
			{
				temp=arr_1[i];
				arr_1[i]=arr_1[i+1];
				arr_1[i+1]=temp;
				swp = 2;
			}
		}
	} while (swp=2);

	for (i=0;i<10;i++)
		{
		cout<<arr_1[i]<<endl;
		}

	getch();
	return 0;
}



I am trying to run the above in VC++2010 but it doesn't appear to run the do-while loop. can somone help.. thanks
The do-while looks fine.

Although, the getch(); should be _getch();.

See if that works.


If not, could you be more specific?
Last edited on
while (swp=2);

This line should make it infinite loop, is the program finishing, or hanging? You probably intend for it to be while (swp==2);
right thanks.

forgot that = is the assignment operator and not for conditional logic.
works now with while (swp==2);
Last edited on
Topic archived. No new replies allowed.