直接插入排序
void insertDirect(int* arr,int len) {
for (int i = 1; i < len; i++) {
int temp = arr[i];
int j = i-1;
while (j >= 0 && arr[j] > temp) {
arr[j+1] = arr[j];
j -= 1;
}
arr[j+1] = temp;
}
}
二分插入排序
void binaryInsert(int* arr,int len) {
for(int i = 1; i < len; i++) {
int start = 0;
int end = i-1;
int temp = arr[i];
while (start <= end) {
int mid = (end + start) >> 1;
if (arr[mid] <= temp) {
start = mid + 1;
} else {
end = mid - 1;
}
}
for(int j = i; j > start; j--) {
arr[j] = arr[j-1];
}
arr[start] = temp;
}
}
希尔插入排序
void insertShell(int* arr,int len) {
for(int gap = len/2; gap > 0; gap /= 2) {
for(int i = gap; i < len; i++) {
int temp = arr[i];
int j = i-gap;
while (j >= 0 && arr[j] > temp) {
arr[j+gap] = arr[j];
j -= gap;
}
arr[j+gap] = temp;
}
}
}