https://github.com/nelsonbn/algorithms-data-structures-stable-vs-unstable-sort-algos
Algorithms and Data Structures - Stable Vs. Unstable Sorting Algorithms
https://github.com/nelsonbn/algorithms-data-structures-stable-vs-unstable-sort-algos
algorithms-and-data-structures data-structures sorting-algorithms
Last synced: 9 months ago
JSON representation
Algorithms and Data Structures - Stable Vs. Unstable Sorting Algorithms
- Host: GitHub
- URL: https://github.com/nelsonbn/algorithms-data-structures-stable-vs-unstable-sort-algos
- Owner: NelsonBN
- License: mit
- Created: 2024-06-30T04:18:53.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-02T13:30:37.000Z (9 months ago)
- Last Synced: 2025-03-02T14:28:52.694Z (9 months ago)
- Topics: algorithms-and-data-structures, data-structures, sorting-algorithms
- Language: Python
- Homepage:
- Size: 2.93 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Algorithms and Data Structures - Stable Vs. Unstable Sorting Algorithms
## Demo
For these examples, we will use two sorting algorithms: `Bubble Sort` and `Selection Sort`. The `Bubble Sort` algorithm is a stable sorting algorithm, while the `Selection Sort` algorithm is an unstable sorting algorithm. For both algorithms, we will sort a list of people by their age in ascending order. The list of people is shown below.
### Algorithms
- [Stable Sorting Algorithms](./src/stable_sort_algo.py)
- [Unstable Sorting Algorithms](./src/unstable_sort_algo.py)
### Input
| Name | Age | Position |
|----------|-----|----------|
| William | 21 | 1 |
| Smith | 23 | 2 |
| Alex | 27 | 3 |
| Paul | 21 | 4 |
| Max | 18 | 5 |
| John | 23 | 6 |
| Bob | 25 | 7 |
| Patrick | 21 | 8 |
| Jane | 23 | 9 |
### Output of stable sorting algorithms
For this example, the stable sorting algorithm is the `Bubble Sort` algorithm.
| Name | Age | Position |
|----------|-----|----------|
| Max | 18 | 5 |
| William | 21 | 1 |
| Paul | 21 | 4 |
| Patrick | 21 | 8 |
| Smith | 23 | 2 |
| John | 23 | 6 |
| Jane | 23 | 9 |
| Bob | 25 | 7 |
| Alex | 27 | 3 |
### Output of unstable sorting algorithms
For this example, the unstable sorting algorithm is the `Selection Sort` algorithm.
| Name | Age | Position |
|----------|-----|----------|
| Max | 18 | 5 |
| Paul | 21 | 4 |
| William | 21 | 1 |
| Patrick | 21 | 8 |
| John | 23 | 6 |
| Smith | 23 | 2 |
| Jane | 23 | 9 |
| Bob | 25 | 7 |
| Alex | 27 | 3 |
### Conclusion
From the output of the stable sorting algorithm, we can see that the people with the same age are sorted based on their original position in the list. For example, the people with the age of 21 are sorted in the order of their original position in the list.
| Name | Age | Position |
|----------|-----|----------|
| William | 21 | 1 |
| Paul | 21 | 4 |
| Patrick | 21 | 8 |
On the other hand, the output of the unstable sorting algorithm shows that the people with the same age are not sorted based on their original position in the list. For example, the people with the age of 21 are not sorted in the order of their original position in the list.
| Name | Age | Position |
|----------|-----|----------|
| Paul | 21 | 4 |
| William | 21 | 1 |
| Patrick | 21 | 8 |
## References
- [Other Algorithms & Data Structures](https://github.com/NelsonBN/algorithms-data-structures)