When looking for a sorting algorithm, we find a kind of tagged bubble sorting algorithm, which has the advantage of having a sequential sequence in the back, which saves the operation of continuing the backward comparison.
Tagged bubble sorting algorithm: In a single order, mark the position of the last interchange element, and in the next order, you only need to compare to this marker position, because the subsequent elements are already sorted.
C + + implementation
#include <iostream> #include <vector> using namespace std;
Template<typename t> void Bubblesort (vector<t> &vec);
int main () {int arr[] = {8, 3, 10, 49, 2, 6, 13, 5, 14, 7, 51, 53, 55};
Vector<int> VEC (&arr[0], &arr[13]);
Bubblesort (VEC);
return 0;
} template<typename t> void Bubblesort (vector<t> &vec) {int vsize = Vec.size ();
if (Vsize < 1) return;
if (1 = = vsize) {cout << "only one element in array:" << vec[0] << Endl;
Return
} int pos = VSize-1; for (int vidx = 0; vidx < VSize-1; vidx++) {//for (int uidx = 0; Uidx < (vsize-vidx-1); uidx++) int Newpos
= 0; for (int uidx = 0; uidx < POS; uidx++)//pos {if (Vec[uidx] > vec[uidx + 1]) {VEC[UIDX] ^= Vec[uidx +
1];
Vec[uidx + 1] ^= vec[uidx];
VEC[UIDX] ^= Vec[uidx + 1];
Newpos = Uidx + 1; }}} for (int vidx = 0; vidx < vsize; vidx++) {cout << ' value in ' << vidx << "is" << Vec[vidx] << Endl;
} return; }