Bubble Sort Algorithm
- Step 1: Find local max
- Step 2: Shift all the element backward after local max
- Step 3: Assign local max
Let's see what will happen in function,
For the 0'th iteration we have,
0: 4,3,5,2,1
1) we have local max (step 1) => 52) we shifted all elements backward after local max (step 2)=> 4,3,2,1,1
3) now, assign local max (step 3) => arr[length-1] = local max (5) =>
4,3,2,1,5
Similarly it will carry on for further iteration,
1: 4,3,2,1,5
2: 3,2,1,4,5
3: 2,1,3,4,5
4: 1,2,3,4,5 -> sort!
After this the function will end/terminate.

Comments
Post a Comment