Article Directory
Sorting is one of the main tasks in computer programs, and the elements of an array are arranged in a specific order. Sorting makes searching easier. Bubble sort and selection sort are sorting algorithms, which can be distinguished by the sorting method they use. Bubble sorting essentially exchanges elements, while selective sorting performs sorting by selecting elements.
Another significant difference between the two is that bubble sort is a stable algorithm, while selection sort is an unstable algorithm. Stable means that records with the same value appear in the same order before and after sorting the list or array. Generally speaking, most stable and fast algorithms use additional memory.
Comparison chart
Comparison item | Bubble Sort | Select sort |
---|---|---|
Basic | Compare and exchange adjacent elements | Select the largest element and swap with the last element (in the case of ascending order) |
Best-case time complexity | O (n) | O ( n 2 n ^ 2n2) |
effectiveness | low efficiency | More efficient than bubble sort |
stable | Yes | no |
method | exchange | select |
speed | slow | Faster than bubble sort |
Definition of bubble sort
Bubble sort is the simplest iterative algorithm. It compares each element with the element next to it and exchanges them when needed. Simply put, it compares the first and second elements of the list and swaps them when they are not in the correct order. Similarly, the second and third elements are compared and exchanged, and this comparison and exchange continues to the end of the list.
The number of comparisons in the first iteration is n-1, where n is the number of elements in the array. After the first iteration, the largest element is located at the nth position. After each iteration, the number of comparisons decreases, and only one comparison is performed in the last iteration.
This algorithm is the slowest sorting algorithm. The best-case complexity of bubble sorting (when the list is arranged in order) is in the order of n(O(n)), and the worst-case complexity is O( n 2 n^2n2 ). In the best case, its order is n, because it just compares the elements without swapping them. This technique also requires additional space to store temporary variables.
Definition of selection sort
The performance of the selection sort is slightly better than the bubble sort algorithm, and the efficiency is higher. Suppose we want to arrange an array in ascending order, then its role is to find the largest element and exchange it with the last element, and then repeat the following process on the sub-array until the entire list is sorted.
In selection sorting, there is no difference between sorted and unsorted arrays, and the complexity in the best and worst cases consumes n 2 n^2n2(O (n 2 n ^ 2n2 )) The order of magnitude. Selection sorting is faster than bubble sorting.
The main difference between bubble sort and selection sort
- In bubble sorting, if necessary, each element and its neighbors are compared and exchanged. On the other hand, selection sorting works by selecting elements and swapping that particular element with the last element. The selected element can be the largest or smallest, depending on the ascending or descending order.
- The worst-case complexity of the two algorithms is the same, that is, O( n 2 n^2n2 ), but the optimal complexity is different. Bubble sort uses n time sequences, while selective sort usesn 2 n^2n2 chronological sequences.
- Bubble sort is a stable algorithm, while selection sort is unstable.
- The selection sort algorithm is fast and effective. In contrast, bubble sort is very slow and inefficient.
in conclusion
The bubble sort algorithm is considered to be the simplest and least efficient algorithm, and the selective sort algorithm is more efficient than the bubble sort algorithm. Bubble sort also requires additional space to store temporary variables, and requires more exchange.
Reference documents
[1]techdifferences.com.Difference Between Bubble Sort and Selection Sort[EB/OL].https://techdifferences.com/difference-between-bubble-sort-and-selection-sort.html,2021-01-01.
[2] Baidu. Sorting algorithm stability [EB/OL]. https://baike.baidu.com/item/sorting algorithm stability/9763250, 2020-04-13.