BuBBle alghortim

Hello guyz. I have 1 trouble here about explanation of Bubble alghorithm. I have some code here and need verbal explanation step by step.
Here is Function of bubblesort:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void bubblesort(int arr[], int n) {
	bool swapped;
		for(int i = 0; i < n-1; i++) {
			swapped = false;
			for( int j = 0; j < n-i-1; j++) {
				if(arr[j] > arr[j+1]) {
					swap(&arr[j],&arr[j+1]);
						swapped = true;
					}
				}
				if (swapped == false)
					break;
			}
}


than you.
Last edited on
Do you mean that you cannot explain anything of it yourself?
google online; there are step by step watch it go web pages that explain the thing.


@keskiverto

yeap
Here is a different version:
1
2
3
4
5
6
7
8
9
void bubblesort( int arr[], int n ) {
	for (int i = 0; i < n-1; i++ ) {
		for ( int j = 0; j < n-i-1; j++ ) {
			if ( arr[j] > arr[j+1] ) {
				swap( &arr[j], &arr[j+1] );
			}
		}
	}
}

Can you tell how the two versions differ?
Topic archived. No new replies allowed.