Two-way Bubble Sorting for sorting and sorting bubbles
Bidirectional Bubble Sorting is simply the process of traversing from left to right to finding the maximum and searching from right to left. Under normal circumstances, two-way Bubble Sorting is faster than normal Bubble sorting. Because the code is relatively simple, I will not talk much about it, so I will directly go to the code.
# Include <iostream> using namespace std; // bidirectional bubble void dbSort (int * a, int n) {int l, r; bool flag = true; // Do You Want To sort l = 0; r = n-1; while (l <r & flag) {flag = false; // sort from left to right for (int I = l; I <r; ++ I) {if (a [I]> a [I + 1]) {swap (a [I], a [I + 1]); flag = true ;}-- r; // sorted, data at the r position is sorted // sorted from the left to the right for (int I = r; I> l; -- I) {if (a [I] <a [I-1]) {swap (a [I], a [I-1]); flag = true ;}++ l; // sorted, data at the l position has been sorted.} int main () {// test code int a [] = {9, 10 }; int n = sizeof (a)/sizeof (int); dbSort (a, n); for (int I = 0; I <n; ++ I) cout <a [I] <"; cout <endl; return 0 ;}