bubble sort error

#include<iostream>
using namespace std;
void swap(int&a,int&b)
{
int temp=a;
a=b;
b=a;

}
void BubbleSort(int a[],int size)
{
bool not_sorted=true;
int j=1,temp;
while(not_sorted){
not_sorted=false;
j++;
for(int i=0;i<size-j;i++)
{
if(a[i]>a[i+1]){
swap(a[i],a[i+1]);
not_sorted=false;
}
}



}
}
int main(){
int a[5]={5,4,3,2,1};
BubbleSort(a,5);
for(int i=0;i<5;i++)
cout<<a[i]<<endl;





return 0;

can u guys know what error?why my output 43215
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void swap(int&a,int&b)
{
    int temp=a;
    a=b;
    b=temp; // not b = a
}
void BubbleSort(int a[],int size)
{
    bool not_sorted=true;
    int j=0; // j needs to start from zero
    ....
    if(a[i]>a[i+1]){
         swap(a[i],a[i+1]);
         not_sorted=true; // not false
    }
....

Last edited on
i still dont understand how my program work
@naaissus

I reported the OP because he continues to troll, just letting you know.

There is a couple of ways to get rid of oxygen thieves like these:

1. Everyone ignores him.
2. Everyone reports him.
THIS IS A SIMPLE VERSION OF YOURS. HOPE IT HELPS...
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
#include<iostream>
using namespace std;
void bubble_sort(int arr[],int limit)
{
	int temp;
	for(int i=0; i<limit; ++i)
	{
		for (int j=i;j<limit;++j)
		{
			if(arr[i]>arr[j])
			{
				temp=arr[i];	
				arr[i]=arr[j];
				arr[j]=temp;
			}
		}
		
	}
}
int main()
{
	int array[10] = {43,2,34,2,12,435,5,4,32,32};
	bubble_sort(array,10);
	for(int i=0; i<10; ++i)
	{
		cout<<array[i]<<endl;
	}
	return 0;
}
Last edited on
Topic archived. No new replies allowed.