3つのinfos
タイプを含む構造体の配列を注文する際に問題があります。私は情報の1つを使って注文し、他の2つの情報を整理したいと思いますが、heapsortを使用していて、自動的に再編成するコードをどのように変更するのか分かりません。私を助ける論理的なヒントはありますか?ここに構造体がありますCの1つの属性で構造体の配列を並べ替え
typedef struct{
int orig;
int dest;
float cost;
}infos;
ありがとうございました。私は人々が私を助けるのに役立つ情報を追加できます!
[EDIT]
void build (Armaz x[], int n){
int i;
float val;
int s;
int f;
for (i = 1; i<n; i++){
val = x[i].cost;
s = i;
f = (s-1)/2;
while (s>0 && x[f].cost<val){
x[s].cost = x[f].cost;
s = f;
f = (s-1)/2;
}
x[s].cost = val;
}
}
void heapsort (infos x[], int n){
build(x,n);
int i;
int s;
int f;
float vali ;
for (i = n - 1 ; i > 0 ; i--){
vali = x[i].cost;
x[i].cost = x[0].cost;
f = 0 ;
if (i == 1){
s = -1;
}
else s = 1;
if (i > 2 && x[2].cost > x[1].cost) s = 2 ;
while (s >= 0 && vali < x[s].cost){
x[f].cost = x[s].cost;
f = s;
s = 2 * f + 1;
if (s + 1 <= i - 1 && x[s].cost < x[s + 1].cost) s++;
if (s > i-1) s = -1;
}
x[f].cost = vali;
}
}
コードを表示すると便利です。 –
ヒープソートアルゴリズムで2つのオブジェクトをどのように比較しますか? –
私はちょうどコード –