顺序表的简单插入排序和折半查找c语言
时间: 2023-06-13 19:05:40 浏览: 242
希尔排序后折半查找
顺序表的简单插入排序:
```c
void InsertSort(int* a, int n)
{
int i, j, temp;
for(i = 1; i < n; ++i)
{
temp = a[i];
for(j = i - 1; j >= 0 && a[j] > temp; --j)
a[j + 1] = a[j];
a[j + 1] = temp;
}
}
```
折半查找:
```c
int BinarySearch(int* a, int n, int key)
{
int low = 0, high = n - 1, mid;
while(low <= high)
{
mid = (low + high) / 2;
if(a[mid] == key)
return mid;
else if(a[mid] > key)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
```
阅读全文