最短寻道时间优先算法(SSTF)

#include 
#include 

int cmp(const void* a, const void* b){
	return *(int *)a - *(int *)b;
}

int find(int* g, int len, int n){
	int i = 0;
	for(; i < len-1; i++) if(g[i+1] >= n) break;
	return i;
}

//dn:disk number; cp:current position
void sstf(int* dn, int len, int cp){
	qsort(dn, len, sizeof(int), cmp);
	int p = find(dn, len, cp);
	int q = p+1;
	while(p >= 0 || q <= len-1){
		if(q == len || dn[q]-cp > cp-dn[p]) cp = dn[p--];
		else cp = dn[q++];
		printf("%d ", cp);
	}
}

main(){
	int dn[100] = {47,72,48,14,3,15,2,35,1,26,86,65,5,100,54};
	sstf(dn, 15, 66);
}

运行结果:

65 72 86 100 54 48 47 35 26 15 14 5 3 2 1

你可能感兴趣的:(算法)