Last active
December 9, 2020 16:34
-
-
Save ochilab/aa9d5de6e7501b93f939cf7822441cd3 to your computer and use it in GitHub Desktop.
ヒープ化をするプログラム(たぶん合ってるかな。。)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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