Bubble Sort: Pass 1: 1.1
Bubble Sort: Pass 1: 1.1
The following numbers are stored in an array A: 8, 4, 2, 9, 7, 5 We will apply Bubble Sort to the array A.
Pass 1:
1.1
8 4 2 9 7 5 Compare the first two elements of array A, if first element is greater then the second element, interchange them.
1.2
8 4 2 9 7 5 The position of 8 & 4 is interchanged as 8 is greater then 4.
4 8 2 9 7 5
Compare the next two elements of the array, if the first element is greater then the second element interchange them, else it will remain as it is.
1.3
4 8 2 9 7 5 The position of 8 & 2 is interchanged as 8 is greater then 2.
4 2 8 9 7 5 Compare the next two elements of the array, if the first element is greater then the second element interchange them, else it will remain as it is.
1.4
4 2 8 9 7 5 The position of 8 & 9 will remain as it is as 8 is smaller then 9.
4 2 8 9 7 5
Compare the next two elements of the array, if the first element is greater then the second element interchange them, else it will remain as it is.
1.5
4 2 8 9 7 5 The position of 9 & 7 is interchanged as 7 is smaller then 9.
4 2 8 7 9 5 Compare the next two elements of the array, if the first element is greater then the second element interchange them, else it will remain as it is.
1.6
4 2 8 9 7 5 The position of 7 & 5 is interchanged as 5 is smaller then 7.
Pass 2: 2.1
4 2 8 7 5 9 Compare the next two elements of the array, if the first element is greater then the second element interchange them, else it will remain as it is.
2.2
4 2 8 The position of 4 & 2 is interchanged as 4 is greater then 2.
7
5 9
2 4 8 7 5 9
Compare the next two elements of the array, if the first element is greater then the second element interchange them, else it will remain as it is.
2.3
2 4 8 7 5 9
2 4
8 7 5 9 Compare the next two elements of the array, if the first element is greater then the second element interchange them, else it will remain as it is.
2.4
2 4 8
7
5 9
2 4
7 8 5 9 Compare the next two elements of the array, if the first element is greater then the second element interchange them, else it will remain as it is.
2.5
2 4 7 8 5 9 The position of 8 & 5 is interchanged as 8 is greater then 5.
2 4 7 5 8 9 Compare the next two elements of the array, if the first element is greater then the second element interchange them, else it will remain as it is.
2.6
2 4 7
5
8 9 The position of 8 & 9 will remain as it is as 8 is smaller then 9.
2 4
7 5 8 9 The final Array after Pass 2
Pass 3: 3.1
2 4 7 5 8 9
Compare the first two elements of the array, if the first element is greater then the second element interchange them, else it will remain as it is.
3.2
2 4 7 5 8 9 The position of 2 & 4 will remain as it is as 2 is smaller then 4.
2 4 7 5 8 9
Compare the next two elements of the array, if the first element is greater then the second element interchange them, else it will remain as it is.
3.3
2 4 7 5 8 9 The position of 4 & 7 will remain as it is as 4 is smaller then 7.
2 4 7 5 8 9 Compare the first two elements of the array, if the first element is greater then the second element interchange them, else it will remain as it is.
3.4
2 4 7 5 8 9
2 4 5 7 8 9 Compare the next two elements of the array, if the first element is greater then the second element interchange them, else it will remain as it is.
3.5
2 4 5 7 8 9 The position of 7 & 8 will remain as it is as 7 is smaller then 8.
2 4
5 7 8 9 Compare the first two elements of the array, if the first element is greater then the second element interchange them, else it will remain as it is.
3.6
2 4 5 7 8 9 The position of 8 & 9 will remain as it is as 8 is smaller then 9.
4
5 7 8 9 The final Array after Pass 3