Insert sorting:
There is an ordered data sequence that requires inserting a number in the sorted data sequence, but the data sequence is still ordered after insertion, in this case, we need to use a new sorting method-insert sorting method. The basic operation of insert sorting is to insert a data to the sorted data, in this way, a new ordered data with a number plus one is obtained. The algorithm is suitable for sorting a small amount of data, and the time complexity is O (n ^ 2 ). Is a stable sorting method. The insert algorithm divides the array to be sorted into two parts: the first part contains all elements of the array, except the last element, and the second part contains only this element. After sorting the first part, insert the last element to the position in the first part of the order.
// Insertion_sort # include
# Include
Using namespace std; int main () {int a [10], I; for (I = 1; I <10; I ++) a [I] = 1 + rand () % 100; // some integers are randomly generated // Insert_sort for (I = 2; I <10; I ++) {// insert a [I] into the sequence a [I-1]... a [1] int key = a [I]; int j = I-1; while (j> 0 & a [j]> key) {a [j + 1] = a [j]; j --;} a [j + 1] = key;} // printf the result for (I = 1; I <10; I ++) {cout <
Select sort:
Each trip selects the smallest (or largest) element from the data element to be sorted, and places it at the end of the sorted series until all the data elements to be sorted are arranged. Selecting sorting is an unstable sorting method.
// Selection_insert # include
# Include
Using namespace std; int main () {int a [10], I, j, key, temp, k; // a random number of 9 for (I = 1; I <= 9; I ++) {a [I] = 1 + rand () % 100;} // Selection_insertfor (I = 1; I <= 9; I ++) {// slect the smallest number from the sequence key = a [I]; for (j = I + 1; j <= 9; j ++) {if (a [j]
Merge Sorting: Merge Sorting is an effective Sorting Algorithm Based on the merge operation. This algorithm is a very typical application of Divide and Conquer. The Merge Sorting method combines two (or more) ordered tables into a new ordered table, that is, the sequence to be sorted is divided into several sub-sequences, each of which is ordered. Then combine the ordered subsequences into the overall ordered sequence. Merges ordered subsequences to obtain a fully ordered sequence. That is, first orders each subsequence, and then orders the subsequence segments. If two ordered tables are merged into an ordered table, it is called a 2-way merge. Merge Sorting is also called Merge Sorting.
// Merge_sort # include
# Include
Const int maxnum= 999999; using namespace std; int a [50]; void merge (int * a, int p, int q, int r) {int m = q-p + 1; int n = r-q; int left [100], right [100]; int I, j, k; for (I = 1; I <= m; I ++) {left [I] = a [I + p-1] ;}for (j = 1; j <= n; j ++) {right [j] = a [j + q];} left [m + 1] = maxnum; right [n + 1] = maxnum; I = j = 1; k = p; while (k <= r) {if (left [I] <= right [j]) {a [k ++] = left [I ++];} else a [k ++] = right [j ++] ;}} void merge_sort (int *, int p, int q) {if (p
Heap sorting: n key word sequences Kl, K2 ,..., Kn is called (Heap). if and only when the sequence satisfies the following properties (Heap properties): (1) ki <= k (2i) and the ki <= k (2i + 1) (1 ≤ I ≤ n), of course, this is a small root heap, and the big root heap is replaced by> =. // K (I) is equivalent to a non-leaf node of a binary tree, K (2i) is the left subnode, and k (2i + 1) if the right subnode stores the vector R [1 .. n] as a tree
Full Binary TreeThe heap is essentially a Complete Binary Tree that meets the following requirements:
The keywords of any non-leaf node in the tree are not greater than (or not less than) the keywords of its left and right children (if any) nodes.
// HEAP_SORT # include
# Include
Using namespace std; int maxv = 49, a [50]; void Max_Heapify (int * a, int I) {int l = I * 2, r = I * 2 + 1, largest; if (l <= maxv & a [l]> a [I]) largest = l; else largest = I; if (r <= maxv & a [r]> a [largest]) largest = r; if (largest! = I) {int temp = a [I]; a [I] = a [largest]; a [largest] = temp; Max_Heapify (a, largest );}} void Build_Max_Heap (int * a) {int I; for (I = maxv/2; I> = 1; I --) Max_Heapify (a, I );} void Heap_Sort (int * a) {for (int I = maxv; I> = 2; I --) {int temp = a [1]; a [1] = a [I]; a [I] = temp; maxv = maxv-1; Max_Heapify (a, 1) ;}} int main () {int I, j; for (I = 1; I <50; I ++) a [I] = 1 + rand () % 99; Build_Max_Heap (a); Heap_Sort (); for (I = 1; I <= 49; I ++) cout
<计数排序:计数排序是一个非基于比较的排序算法,该算法于1954年由 harold h. seward 提出。它的优势在于在对一定范围内的整数排序时,它的复杂度为ο(n+k)(其中k是整数的范围),快于任何比较排序算法。
// Counting_Sort # include
Using namespace std; int main () {int a [9] = {, 3}; int c [6]; int I; int B [9]; for (I = 0; I <= 5; I ++) c [I] = 0; for (I = 1; I <= 8; I ++) c [a [I] = c [a [I] + 1; for (I = 1; I <= 5; I ++) c [I] = c [I-1] + c [I]; for (I = 8; I> = 1; I --) {B [c [a [I] = a [I]; c [a [I] --;} for (I = 1; I <= 8; I ++) cout <
<<" ";cout<
Reprinted please indicate the source http://write.blog.csdn.net/postedit
<<">