插入排序
#include <iostream>
void print_arrs(const int *, int N);
void sort_arrs(int *pInt, int N);
int main() {
int arr[] = {1, 3, 5, 7, 6, 2, 4, 9, 8, 0};
std::cout << "排序前:" << std::endl;
print_arrs(arr, 10);
sort_arrs(arr, 10);
std::cout << "排序后:" << std::endl;
print_arrs(arr, 10);
return 0;
}
/**
* 插入排序
* @param pInt
* @param N
*/
void sort_arrs(int *const pInt, int N) {
for (int i = 1; i < N; ++i) {
int temp = pInt[i];
for (int j = i-1; j >= 0 && pInt[j] > temp; --j) {
pInt[j + 1] = pInt[j];//移出空位
pInt[j] = temp;//新排落位
}
}
}
void print_arrs(const int *pInt, int N) {
for (int i = 0; i < N; ++i) {
std::cout << pInt[i] << "\t";
}
std::cout << std::endl;
}
FlowLiver Notes