Insertion Sort
- An input element, is inserted into a proper position has been sorted in a number of columns, the number of columns is still ordered.
- Thought: Comparison of the number to be inserted one by one with the number of each of the array, when the first to find a large number of insertion elements than i, that is the element before the insertion position. Then starting from the last element of the array until the element, after a shift by one unit. Finally, given the number of insertions of elements a [i] to. If the number of insertions than all the element values are then inserted into the small final position.
#include <iostream>
using namespace std;
template<class T>
void InsertionSort(T *a, int n);
template<class T>
void InsertionSort_2(T* a, int n);
template<class T>
void Insert(const T& e, T* a, int i);
int main()
{
int x[] = { 1,3,5,7,9,2,4,6,8,0 };
double y[] = { 0,1.2,3.3,1,4.5,6 };//第一个元素不算在排序数组中
InsertionSort(x, 10);
InsertionSort_2(y, 5);
for (int i = 0; i < 10; i++)
cout << x[i] << " ";
cout << endl;
for (int i = 1; i < 5; i++)
cout << y[i] << " ";
cout << endl;
return 0;
}
template<class T>
void InsertionSort(T* a, int n) {
int in, out;
//out=0已经出去了
for (out = 1; out < n; out++) {
int temp = a[out];
in = out;
while (in > 0 && a[in - 1] >= temp) {
a[in] = a[in - 1];
--in;
}
a[in] = temp;
}
}
template<class T>
void InsertionSort_2(T* a, int n) {
//a[0]用来保存排序使用,不能保存原始数据
for (int j = 2; j < n; j++) {
T temp = a[j];
Insert(temp, a, j - 1);
/*a[0] = temp;
int i = j - 1;
while (temp < a[i]) {
a[i + 1] = a[i];
i--;
}
a[i + 1] = temp;*/
}
}
template<class T>
void Insert(const T& e, T* a, int i) {
a[0] = e;
while (e < a[i]) {
a[i + 1] = a[i];
i--;
}
a[i + 1] = e;
}
Quick Sort
- The fastest, the use of recursion.
- The right pivot selected discharge constantly dividing, is greater than the pivot, the pivot is smaller than the left discharge.
#include <iostream>
using namespace std;
template<class T>
void QuickSort(T* a, const int left, const int right) {
if (left < right) {
// 选枢轴划分
int i = left, j = right + 1;//右边加一个很大的数,始终保持不动
int pivot = a[left];
//划分过程
do {
do i++; while (a[i] < pivot);
do j--; while (a[j] > pivot);//在这里提高排序速度
if (i < j) swap(a[i], a[j]);
} while (i < j);
swap(a[left], a[j]);
//j现在为中间位置
QuickSort(a, left, j - 1);
QuickSort(a, j + 1, right);
}
}
int main() {
int a[] = { 0,2,4,6,8,1,3,5,7,9,99 };
double b[] = { 2.2,3.3,1.1,99 };
QuickSort(a, 0, 9);
QuickSort(b, 0, 2);
for (int i = 0; i <= 9; i++)
cout << a[i] << " ";
cout << endl;
for (int i = 0; i <= 2; i++)
cout << b[i] << " ";
return 0;
}
Merge sort
The two have been sorted array merge into a sorted array.
#include <iostream>
#include<algorithm> //copy算法的库
using namespace std;
template<class T>
void MergeSort(T* initList, T* mergeList, const int l, const int m, const int n) {
//初始数组和排序后的数组、数组下标l/m、n是数组个数
int i1, i2, iResult = 1;
for (i1 = l, i2 = m + 1; i1 <= m && i2 <= n; iResult++) {
if (initList[i1] <= initList[i2]) {
mergeList[iResult] = initList[i1];
i1++;
}
else {
mergeList[iResult] = initList[i2];
i2++;
}
}
copy(initList + i1, initList + m + 1, mergeList + iResult);
copy(initList + i2, initList + n + 1, mergeList + iResult);
}
int main() {
int a[] = { 0,23,47,81,96,7,14,39,55,62,74 };//两个已排序的数组放在一个数组里,第一个元素0不算
int b[11] = { 0 };//归并后的数组
MergeSort(a, b, 1, 4, 10);
for (int i = 1; i <= 10; i++)
cout << b[i] << " ";
cout << endl;
return 0;
}