Bubble sort
The bubble sort is done as the simplest sort algorithm, with two lines for loops.
Step: First, before and after the next two number of adjacent to the size, if the front is larger than the next two number exchange position, so that the largest number of one round will be exchanged to the last side.
Second, repeat the steps of a comparison (but the last number does not need to participate in the comparison, because the first round has chosen its largest), the second-largest election to the bottom.
。。。。
Three, until all the numbers do not need to compare the sort succeeds
Example does not give, direct realization:
buddle.cc
#include <iostream> #include <vector>using namespace std;template< class T >void Buddle (vector<t > &a) { int length = A.size (); for (int i = length-1; i > 0; i.) for (int j = 0; J < i; J + +) { if (A[j] > a[j+1]) swap (A[j], a[ J+1]);} } int main (int argc, char **argv) { vector<int> a = {8, 9, 1, 7, 4, one,, 3, 2, 5}; Vector<int>::iterator iter; Buddle<int> (a); for (iter = A.begin (); ITER! = A.end (); iter++) { cout<< *iter << ""; } cout << Endl; return 0;}
Operation Result:
Simple selection sorting
1, choose the smallest number from the number of 1--n, put it in the first place
2, choose the smallest number from the 2--n number, put it in the last
。。。
Code: select.cc
#include <iostream> #include <vector>using namespace std;template< class T >void select_sort (vector <T> &a) { int length = A.size (); int min = 0; for (int i = 0, i < length; i++) {for (int j = i; j < length; J + +) { if (A[j] < a[min]) min = j;
} if (i! = min) swap (A[i], a[min]);} } int main (int argc, char **argv) { vector<int> a = {8, 9, 1, 7, 4, one,, 3, 2, 5}; Vector<int>::iterator iter; Select_sort<int> (a); for (iter = A.begin (); ITER! = A.end (); iter++) { cout<< *iter << ""; } cout << Endl; return 0;}
Run results
Insert Sort
It is similar to the card program when fighting landlords.
Each time a number is inserted into an already sorted sequence, so that it is still in order, insert the time can be inserted in the form of binary (that is, a binary search method to determine where to insert)
On code: half_insert.cc
#include <iostream> #include <vector>using namespace std;template< class T >void Half_insert (vector <T> &a) { int length = A.size (); int Low,mid,high; T tmp; for (int i = 1; i < length; ++i) {Low = 0; High = i-1; while (low <= high) { mid = (low + high)/2; if (A[i] > A[mid]) Low = mid + 1; else high = mid-1; } TMP = A[i]; for (int j = i, J > low;--j) a[j] = a[j-1]; A[low] = tmp; }} int main (int argc, char **argv) { vector<int> a = {8, 9, 1, 7, 4, one,, 3, 2, 5}; Vector<int>::iterator iter; Half_insert<int> (a); for (iter = A.begin (); ITER! = A.end (); iter++) { cout<< *iter << ""; } cout << Endl; return 0;}
Operation Result:
Merge sort
This is a two-way merge sort.
Use Merge_sort to recursively divide a sequence into two sub-sequences, and then call the merge function to merge two subsequence into an ordered sequence
Code implementation: merge.cc
#include <iostream> #include <vector>using namespace std;const int MAX = 0x7fffffff;template< class T > void merge (vector<t> &a, int begin, int mid, int end) {int length1 = mid-begin+1; int length2 = end-mid+1; int tag1 = 0; int tag2 = 0; int tag = begin; Vector<t> VEC1 (length1+1); Vector<t> vec2 (length2+1); for (int i = begin; I <= mid; i++) vec1[tag1++] = A[i]; VEC1[TAG1] = MAX; for (int i = mid+1; I <= end; i++) vec2[tag2++] = A[i]; VEC2[TAG2] = MAX; TAG1 = 0; Tag2 = 0; while (tag <= end) {if (Vec1[tag1] < VEC2[TAG2]) a[tag++] = vec1[tag1++]; else a[tag++] = vec2[tag2++]; }}template< class T >void merge_sort (vector<t> &a, int begin, int end) {if (Begin < end) {I NT MID = (begin + End)/2; Merge_sort (A, begin, mid); Merge_sort (A, mid+1, end); Merge (A, begin, Mid, end); }}int Main (int aRGC, char **argv) {vector<int> a = {10, 8, 9, 1, 7, 4, 11, 23, 3, 2, 5}; Vector<int>::iterator ITER; Merge_sort (A, 0, a.size ()-1); for (iter = A.begin (); ITER! = A.end (); iter++) {cout<< *iter << ""; } cout << Endl; return 0;}
Operation Result:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
DIY data structure--sorting algorithm 1 (bubbling, inserting, merging, simple selection) (C + + implementation)