Skip to content

Instantly share code, notes, and snippets.

@ochilab
Last active December 9, 2020 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ochilab/aa9d5de6e7501b93f939cf7822441cd3 to your computer and use it in GitHub Desktop.
Save ochilab/aa9d5de6e7501b93f939cf7822441cd3 to your computer and use it in GitHub Desktop.
ヒープ化をするプログラム(たぶん合ってるかな。。)
void heap (int array[], int left, int right) {
int target;
int parent;
for(parent=left;2*parent<=right;parent=target){
int cl = 2* parent; //左の子
int cr=cl+1;// 右の子
//比較対象の選定
if ((cl < right) && (array[cl] > array[cr])) {
target=cr;// 右の子の方が小さい
}
else{
target=cl;//左が小さい
}
//親が小さい
if (array[target] >= array[parent]) {
break;//なにもせず終了
}
//親と小さい方を交換
swap(&array[target], &array[parent]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment