1. 程式人生 > >最短尋道時間優先演算法(SSTF)

最短尋道時間優先演算法(SSTF)

#include <stdio.h>
#include <stdlib.h>

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