HeapSortを使用して、すでに塗りつぶされている配列をソートするのではなく、配列が塗りつぶされたときにHeapSortを使用しています。ヒープソート理論?
最小の値が一番上にあるヒープについては、ヒープに新しい値を挿入するときに親ノードをチェックして、新しい子が大きいかどうかを確認しました。もしあなたが何もしていなければ、それはあなたがチェックし、必要に応じて木の上に入れ替えるのですか?
public class HeapSort{
static int[] numbers = new int[] { 0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
static int[] array = new int[16];
public static void main(String[] args) {
for (int i = 1; i < 15; i++) {
array[i] = numbers[i];
if (i > 1)
sort(i);
}
for (int i = 1; i < 15; i++) {
System.out.println(array[i]);
}
}
public static void sort(int i) {
int parentLocation = i/2;
int childLocation = i;
int parentValue = array[parentLocation];
int childValue = array[childLocation];
if(parentValue > childValue){
array[parentLocation] = childValue;
array[childLocation] = parentValue;
}
if(parentLocation != 1){
sort(parentLocation);
}
}
}
TIA
をそのanyhelpが、これは出力された場合、私は後方にそれを1-15を与えるとき:それの私の実装はまったく機能していないので、
は、この権利はないです
2
6
3
9
7
5
4
15
12
13
8
14
10
11
しかし、あなたは私のようにすべてが困惑しているようです。
デバッガでコードをステップ実行しようとしましたか? –
投稿したコードが正しいように見えます。 –
数時間。私はそれを理解することができないし、私はそれがあまりにも長く見てきたと思う。 –