可以做一下的C语言题目(二)
二分查找法插入排序
#include <stdio.h>
void initArray(int *array, int size)
{
int i = 0;
for(i = 0; i < size ; i++){
array[i] = rand()%30;
}
}
void insertionSort(int *array, int size)
{
int i = 0;
int j = 0;
int mid = 0;
int count = 0;
int temp = 0;
for(count = 1; count < size; count++){
if(array[count] > array[count-1]){
continue;
}
i = 0;
j = count-1;
mid = (i+j)/2;
temp = array[count];
while(i <= j){
if(array[mid] >= temp){
j = mid - 1;
}else if(array[mid] < temp){
i = mid + 1;
}
mid = (i+j)/2;
}
for(j = count; j > i; j--){
array[j] = array[j-1];
}
array[j] = temp;
}
}
int main()
{
int array[20];
initArray(array,20);
insertionSort(array,20);
return 0;
}