Bubble Sort
Idea
The basic concept is same to Selection Sort. The list is rearranged from minimal to maximal value by picking the maximal(or minimal) value from the unsorted list iteratively.
In selection sort, the minimal element is selected after searching whole list. On the other hand, bubble sort iteratively compares two neighbor elements and swaps the elements if the left element is greater(or less) than the right one, from the list head to the list tail. Therefore, the maximal, the second-maximal, ... values will be "bubbled" up to the tail of list one by one.
Take as an example:
- Step 1: Move the maximal value to the last position of the list.
- so swap them.
- so swap them.
- so swap them.
- so swap them.
- The maximal value is moved to the last of the list
- Step 2: Move the second-maximal value to the second-last position of the list.
- so do nothing.
- so swap them.
- so swap them.
- The second-maximal value is moved to the second-last of the list
- Step 3: Move the third-maximal value to the third-last position of the list.
- so swap them.
- so swap them.
- The third-maximal value is moved to the third-last of the list
- Step 4: Move the fourth-maximal value to the fourth-last position of the list.
- so swap them.
- The fourth-maximal value is moved to the fourth-last of the list
Dividing one list into unsorted list and sorted list
In above example, the list is naturally partition into the sorted part and the unsorted part. The part contains the "bubbled" elements are sorted. The others are unsorted.
- Step 1: Move the maximal value to the last position of the list.
- This iteration ends after the comparison of last two elements(4th and 5th)
- The maximal value is "bubbled" to the last(5th) of the list
- The 5th element is sorted part and the 1st-to-4th is unsorted part
- Step 2: Move the second-maximal value to the second-last position of the list.
- This iteration ends after the comparison of second-last two elements(3rd and 4th)
- The second-maximal value is "bubbled" to the second-last(4th) of the list
- The 4th-to-5th elements is sorted part and the 1st-to-3rd is unsorted part
- Step 3: Move the third-maximal value to the third-last position of the list.
- This iteration ends after the comparison of third-last two elements(2nd and 3rd)
- The third-maximal value is "bubbled" to the third-last(3rd) of the list
- The 3rd-to-5th elements is sorted part and the 1st-to-2nd is unsorted part
- Step 4: Move the fourth-maximal value to the fourth-last position of the list.
- This iteration ends after the comparison of fourth-last two elements(1st and 2nd)
- The fourth-maximal value is "bubbled" to the fourth-last(2nd) of the list
- The 2nd-to-5th elements is sorted part and the 1st is unsorted part
- The last left element is definitely the smallest value
- so whole list from 1st to 5th is sorted
Algorithm
- At the 1st round, the 1st-last element is sorted
- At the 2nd round, the 2nd-last to 1st-last elements are sorted
- At the 3rd round, the 3rd-last to 1st-last elements are sorted
- At the round, the -last to 1st-last elements are sorted
Proof
Proof by mathematical induction
Lemma 1
Given a list with elements, where , the maximal element of will be (the last element).
- Base step: When , the assumption obviously holds
- Induction Hypothesis: Assume the hypothesis holds when
- Induction Step: when
- After the iteration of , the list is divided into two parts: and
- From above hypothesis, the maximal value in is
- When :
- if , then the maximal element is , so the hypothesis still holds
- if , they will be swapped. After then, the maximal element will be , so the hypothesis still holds
Lemma 2
After the iteration, will be the th largest element of
- Base step: When , the assumption holds because lemma 1 is true
- Induction Hypothesis: Assume hypothesis holds when
- Induction Step: When
- The goal is to prove that the is the th largest element of
- After the iteration , is the th largest element of
- We can divide the list into and
- The list contains the picked th, th, .... , st largest elements
- The list is the unselected and unsorted list
- By applying the lemma 1 to , the maximal element of will be after all the iterations for .
- is the th selected maximal element
- so is the th largest element of
Conclusion
By lemma 2
- After , is the st largest element of
- After , is the nd largest element of
- After , is the rd largest element of
- ...
- After , is the th largest element of
- After , is the th largest element of
- ...
- After , is the th largest element of
- After , is the th largest element of
- After , is the th largest element of
Thus, the list $L[1...N]$ is sorted by the order that .
Complexity
Let denote the length of list .
| iterations for | |
|---|---|
| ... | ... |
The total of all iterations of BubbleSort() is tracked in above table and its sum is:
Thus, the complexity is .
Implementation
See the files on gist here.