实现分块检索算法(C语言)【急求】

来源:百度知道 编辑:UC知道 时间:2024/07/05 06:53:24
实现分块检索算法
要求:块间采用折半检索,块内采用顺序检索。
采用数据文件方式输入记录序列。
格式:
36,6
10, 30, 20, 15, 25, 5
100, 200,150,250,300,50
500, 550,510,450,580,400
……
1000, 1500, 1200, 2000, 1800, 1300
首先根据文件建立块索引,然后根据给定值检索。

/*
假定数据在文件中的存放格式如下:
4, 6 (4表示行数,6表示列数)
10, 30, 20, 15, 25, 5
100, 200, 150, 250, 300, 50
500, 550, 510, 450, 580, 400
1000, 1500, 1200, 2000, 1800, 1300
代码仅供参考,如发现错漏之处,可发消息给我。
*/
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

/* 从小到大排序数组 */
void ArraySort(int *a, int s)
{
int i, j, k, t;

for (i = 0; i < s-1; ++i)
{
k = i;

for (j = i + 1; j < s; ++j)
{
if (a[k] > a[j])
{
k = j;
}
}

if (k != i)
{
t = a[i];
a[i] = a[k];
a[k] = t;
}
}
}

/* 使用折半查找定位k所在的区块 */
int Partition(int *a, int s, int c, int k)
{
int bottom = 0;
int top = s - 1;

while (bottom <= top)
{
int middle = (bottom + top) / 2;

if (k >= a[middle * c] && k <= a[middle