1. Fast Sorting algorithm
The worst time complexity is O (n*n), which is associated with the selection of cardinality, the expected elapsed time is O (NLGN), and the implied constant factor in the O (NLGN) notation is small. In addition, it can be sorted in place.
Quick Sort Logic:
1, first from the array to take out a number as the base, the selection of the cardinality of the effect of sorting efficiency is very large, generally random to choose.
2, partition, will be smaller than this base of the number of all put on its left, than the number of large base to the right
3. Repeat the second step for the left and right two intervals until only one number per interval
#include <iostream> #include <cstdlib> void Swap (int& A, int& b) {int
TMP = A;
A = b;
b = tmp;
int partion (int a[], int f, int e) {int i = f;
for (int j = f; j < E; J +) {if (A[j] < a[f]) {swap (A[++i], a[j]);
} swap (A[i], a[f]);
return i;
} void QuickSort (int a[], int f, int e) {if (F < e) {int i = f + rand ()% (e-f);
Swap (A[f], a[i]);
int mid = Partion (A, F, e);
QuickSort (A, F, mid);
QuickSort (A, mid + 1, e);
int main () {int a[] = {6, 7, 0, 3, 2, 4, 5, 8, 9, 1};
QuickSort (A, 0, 10);
for (int i = 0; i < i++) {std::cout<<a[i]<< "";
} std::cout<<std::endl;
return 0; }
2. Merge sort
The idea of an O (n*lgn)
Merge sort is divide and conquer, and each recursive process involves three steps:
First, decomposition: the sequence of n elements to be sorted into two subsequence, each subsequence Includes N/2 elements.
Second, governance: Call merge Sort merge for each subsequence, recursive operation
Third, Merge: Merge two sorted subsequence, and generate sort results.
#include <iostream> void Mergearray (int a[], int f, int m, int e, int tmp[]) {int i = f, j = m + 1;
int k = 0;
while (I <= m && J <= E) {if (A[i] < a[j]) tmp[k++] = a[i++];
else tmp[k++] = a[j++];
while (i <= m) tmp[k++] = a[i++];
while (J <= e) tmp[k++] = a[j++];
for (i = 0; i < K; i++) {a[f + i] = tmp[i];
} void Merge (int a[], int f, int e, int temp[]) {if (F < e) {int mid = f + (e-f)/2; Merge (A, F, mid, temp); Left ordered merge (A, mid + 1, E, temp); Right ordered Mergearray (A, F, Mid, E, temp);
Merges left and right two ordered arrays} void mergesort (int a[], int len) {int *p = new Int[len];
Merge (A, 0, Len, p);
Delete[] p;
int main () {int a[] = {6, 7, 0, 3, 2, 4, 5, 8, 9, 1};
MergeSort (A, 9);
for (int i = 0; i < i++) {std::cout<<a[i]<< "";
} std::cout<<std::endl;
return 0;
3. Select sort
Time Complexity of O (n * N)
Select the sort logic:
Each trip selects the smallest (or largest) element from the data element to be sorted, sequentially at the end of the ordered sequence, until all the data elements to be sorted are finished. The select sort is not a stable sort algorithm that destroys the order of the original elements.
#include <iostream>
void selectsort (int a[], int n)
{for
(int i = 0; i < n; i++)
{
int pos = i;
for (int j = i + 1; j < N; j + +)
{
if (A[j] < A[pos])
{
pos = j;
}
}
if (pos!= i)
{
int tmp = a[i];
A[i] = A[pos];
A[pos] = tmp;
}}} int main ()
{
int a[] = {6, 7, 0, 3, 2, 4, 5, 8, 9, 1};
Selectsort (A, a);
for (int i = 0; i < i++)
{
std::cout<<a[i]<< "";
}
std::cout<<std::endl;
return 0;
}
4. Insert Sort
Time Complexity of O (n * N)
The insertion sort is where each step inserts a pending data into the appropriate position in the sorted data, until all inserts are completed.
#include <iostream>
void insertsort (int a[], int n)
{for
(int i = 1; i < n; i++)
{for
(int j = i-1; J >= 0 && a[j + 1] < A[J]; j--)
{
int tmp = A[J];
A[J] = a[j+1];
A[J+1] = tmp;
}}} int main ()
{
int a[] = {6, 7, 0, 3, 2, 4, 5, 8, 9, 1};
Insertsort (A, a);
for (int i = 0; i < i++)
{
std::cout<<a[i]<< "";
}
std::cout<<std::endl;
return 0;
}
5. Hill Sort
Hill sort is a sort of insertion. The essence of hill sort is grouping insertion sort, which is also called narrowing incremental ordering.
The basic idea of the method is:
The entire backlog sequence is divided into several sequences (consisting of an element separated by an "increment") for direct insertion, respectively. Then the increment is then sorted, and the elements in the whole sequence are basically ordered (the increment is small enough), and then the whole element is inserted in a direct order. Because the direct insertion sort is very efficient in the case where the elements are basically orderly (close to the best case).
#include <iostream>
void shallsort (int a[], int len)
{for
(int grap = LEN/2; grap > 0; grap/= 2)
{for
(int i = Grap i < len; ++i)
{for
(int j = i-grap; J >=0 && A[j] > a[j + grap]; J- = Grap)
std::swap (A[j], a[j + grap]);
}} int main ()
{
int a[] = {6, 7, 0, 3, 2, 4, 5, 8, 9, 1};
Shallsort (A, a);
for (int i = 0; i < i++)
{
std::cout<<a[i]<< "";
}
std::cout<<std::endl;
return 0;
}
6. Heap sorting
Time Complexity of O (N*LOGN)
Using the large top heap (small top heap) heap top record is the feature of the maximum keyword (the minimum key), making it easy to select the maximum record (minimum record) from the disorder each time.
Heap sort is an unstable sort and is not suitable for records with less sorting
#include <iostream> void heapation (int a[], int pos, int len) {int left = pos * 2;
int right = left + 1;
int root = pos;
if (left < len && A[left] > a[root]) root = left;
if (right < Len && A[right] > a[root]) root = right;
if (root!= pos) {std::swap (A[root], A[pos]);
Heapation (A, root, Len); }///create maximum heap void createheap (int a[], int len) {for (int i = LEN/2; I >= 0; i--) {heapation (A, I, l
EN);
} void Heapsort (int a[], int len) {createheap (A, Len);
for (int i = 0; i < len; i++) {Std::swap (a[0], a[len-i-1]);
Heapation (A, 0, len-i-1);
int main () {int a[] = {6, 7, 0, 3, 2, 4, 5, 8, 9, 1};
Heapsort (A, 10);
for (int i = 0; i < i++) {std::cout<<a[i]<< "";
} std::cout<<std::endl;
return 0; }
7. Counting Sort
To use this sort of note is that the sorted data conforms to the normal distribution and has great limitations, although the time complexity is O (n), but the space is sacrificed. Suppose the number to sort is: 4,5,9,2,3,1000 then we need to allocate 1000 spaces.
#include <iostream>
void countsort (int a[], int len)
{
if (len <= 0 | | a = = NULL) return
;
int b[1024] = {0};
int c[1024] = {0};
int maxlen = a[0];
for (int i = 0; i < len; ++i)
{
b[a[i]]++;
if (MaxLen < a[i])
maxlen = A[i];
}
for (int i = 0; i < maxlen ++i)
{
B[i + 1] + = B[i];
}
for (int i = 0; i < len; ++i)
{
c[--b[a[i]]] = A[i];
}
for (int i = 0; i < len; ++i)
{
A[i] = C[i];
}
int main ()
{
int a[] = {6, 7, 0, 3, 2, 4, 5, 8, 9, 1};
Countsort (A, a);
for (int i = 0; i < i++)
{
std::cout<<a[i]<< "";
}
std::cout<<std::endl;
return 0;
}