Assume we want to sort four names in alphabetical order
The following occurs during one cycle in the sorting process
The first element is compared to the second element.
A swap will occur if the first element is larger than the second. (Ascending order) The first element is then compared to the third element and all the following elements. Swaps take place whenever a smaller element is found. At the end the first cycle the smallest element will be in the first position of the array.
Zoro Ben Jan Ansa
Ben Zoro Jan Ansa
Ben Zoro Jan Ansa
Ansa Zoro Jan Ben
Ansa Zoro Jan Ben
Ansa Jan Zoro Ben
Ansa Ben Zoro Jan
Ansa Ben Zoro Jan
Ansa Ben Jan Zoro
Bubble sort
Assume we want to sort four names in alphabetical order
The following occurs during one cycle in the sorting process Two consecutive elements are compared to each other, starting with the first two elements in the array. The process determines if element one is greater than element two (ascending order). If so, the two elements are swapped. A similar comparison is then made between the second and third element, and a swap is made if element two is larger than element three. Further comparisons are made until the last two elements in the array are compared. The largest element is now in the last position of the array.
These steps are repeated a number of times - but the number of comparisons is reduced by one each time – until all the elements are sorted.