A. Notes
Bubble Sort is very similar to the selection sort algorithm. Steps to sort an array ascending are below.
Step 1 − Set k to index 0
Step 2 − Compare all elements after k with element at k in the array: if any element after k is less, swap it with the element at k.
Step 3 − Increase k to point to the next element, repeat until k point to the second but last element
To sort an array descending, just change less to larger when comparing:
Step 1 − Set k to index 0
Step 2 − Compare all elements after k with element at k in the array: if any element after k is larger, swap it with the element at k.
Step 3 − Increase k to point to the next element, repeat until k point to the second but last element
B. Watch
A good example of step by step sorting process is here.
This YouTube video is only 2 minutes, but very helpful.
C. HW:
Write Java code for bubble sort to sort the array both ascending and descending.
{30099,181263,10128,5238,3839,5170,5641,11000,15973,1200,12500,87337,107430,227931,56400}