#include <iostream>
using namespace std;
template<class T>
void insertionSort(T arr[], int n) {
for (int i = 1; i < n; ++i) {
T aux = arr[i];
int mid;
int left = 0, right = i - 1;
while (left <= right) {
mid = left + (right - left) / 2;
if (aux < arr[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
}
// left 为待插入的点
for (int j = i; j > left; --j) {
arr[j] = arr[j - 1];
}
arr[left] = aux;
}
}
int main() {
int arr[] = {-3, -4, 8, -1, 0, 6, -2};
int len = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, len);
for (const int &temp: arr) {
cout << temp << " ";
}
cout << endl;
return 0;
}
折半插入排序-C++
最新推荐文章于 2023-01-29 16:56:25 发布