(因为这是按照自己的思路实现的,代码有点臃肿,后续会对代码进行优化)
堆的创建
public static int[] createHeap(int a[]) {
int temp = 0;
//求深度
int depth = 0;
int n = a.length;
while (n>=1) {
n = n/2;
depth++;
}
System.out.println("深度:"+depth);
int back = 1;
int length = a.length-1;
for(int k = length/2;k>=1;k--){
System.out.println(length);
System.out.println(k);
if(k<Math.pow(2, depth-2)){
depth--;
back++;
System.out.println("depth:"+depth);
System.out.println("back:"+back);
}
int i = k;
for(int j = 0;j<back;j++){
if(2*i==a.length-1){
if(a[i]<a[2*i]){
temp = a[i];
a[i] = a[2*i];
a[2*i] = temp;
}
}else{
if(a[2*i]>a[2*i+1]){
if(a[i]<a[2*i]){
temp = a[i];
a[i] = a[2*i];
a[2*i] = temp;
}
}else {
if(a[i]<a[2*i+1]){
temp = a[i];
a[i] = a[2*i+1];
a[2*i+1] = temp;
}
}
}
i = i*2;
}
}
return a;
}
test
public static void main(String[] args) {
int a[] = {0,79,66,43,83,30,87,38,55,91,72,49,9};
a = createHeap(a);
for (int i = 1; i < a.length; i++)
System.out.println(a[i]);
}
91
83
87
79
72
43
38
55
66
30
49
9
堆的插入和删除
package 堆;
public class Heap {
// 添加
public static int[] insert(int n, int a[]) {
int[] b = new int[a.length + 1];
System.arraycopy(a, 0, b, 0, a.length);
a = b;
int i = a.length - 1;
for (; i != 1 && n > a[i / 2]; i = i / 2) {
a[i] = a[i / 2];
}
a[i] = n;
return a;
}
// 删除堆顶
public static int[] delete(int a[]) {
int medium = a[1];
a[1] = a[a.length - 1];
int[] b = new int[a.length - 1];
System.arraycopy(a, 0, b, 0, a.length - 1);
a = b;
int tmp = 0;
System.out.println(a.length);
System.out.println(a[5]);
for (int i = 1; i < a.length; i = i * 2) {
if(i*2>=a.length){
break;
}
if (i * 2 == a.length-1 ) {
if (a[i * 2] < a[i]) {
break;
}
tmp = a[i];
a[i] = a[i * 2];
a[i * 2] = tmp;
} else {
System.out.println("i = "+i );
if (a[i * 2] > a[i * 2 + 1]) {
if (a[i] > a[i * 2]) {
break;
} else {
tmp = a[i];
a[i] = a[i * 2];
a[i * 2] = tmp;
}
} else {
if (a[i] > a[i * 2 + 1]) {
break;
} else {
tmp = a[i];
a[i] = a[i * 2 + 1];
a[i * 2 + 1] = tmp;
}
i++;
}
}
}
a[0] = medium;
return a;
}
public static void main(String[] args) {
int[] a = { 0, 20, 15, 2, 14, 10 };
a = insert(21, a);
for (int i = 1; i < a.length; i++)
System.out.println(a[i]);
a = delete(a);
System.out.println(a[0] + " " + a.length);
}
}
21
15
20
14
10
2
6
10
堆的排序
public static int[] arithmetic(int a[]) {
int temp = 0;
for (int i = a.length - 1; i > 0; i--) {
if(i == 3){
if(a[2]<a[3]){
int tmp = a[2];
a[2] = a[3];
a[3] = tmp;
}
}
System.out.println("-----------");
System.out.println("交换:"+a[1]+" "+a[i]);
temp = a[1];
a[1] = a[i];
a[i] = temp;
int j = 1;
for (; 2 * j < i;) {
if (2 * j == i-1 && a[2 * j] > a[j]) {
int tem = a[2 * j];
a[2 * j] = a[j];
a[j] = tem;
System.out.println("交换最后" + " " + a[j] + " "+ a[2 * j] + "j="
+ j);
j = j * 2;
} else {
if (a[2 * j] > a[2 * j + 1]) {
// System.out.println(" 验证");
if (a[2 * j] > a[j]) {
int tem = a[2 * j];
a[2 * j] = a[j];
a[j] = tem;
System.out.print("交换" + " " + a[j]+ " " + a[2 * j]
+" i-1 "+i);
j = j * 2;
System.out.println(" j=" + j);
}
} else {
if (a[2 * j + 1] > a[j]) {
int tem = a[2 * j + 1];
a[2 * j + 1] = a[j];
a[j] = tem;
System.out.print(" 交换" + " "
+ a[j]+ " " + a[2 * j + 1]);
j = j * 2 + 1;
System.out.println(" j=" + j);
}
}
}
if (2 * j > i-1) {
System.out.println("无条件");
j = j * 2;
}
}
}
return a;
}
交换:91 9
交换 87 9 j=3
交换 43 9 i-1 12 j=6
无条件
-----------
交换:87 49
交换 83 49 i-1 11 j=2
交换 79 49 i-1 11 j=4
交换 66 49 j=9
无条件
-----------
交换:83 30
交换 79 30 i-1 10 j=2
交换 72 30 j=5
无条件
-----------
交换:79 49
交换 72 49 i-1 9 j=2
交换 66 49 i-1 9 j=4
交换最后 55 49j=4
无条件
-----------
交换:72 49
交换 66 49 i-1 8 j=2
交换 55 49 i-1 8 j=4
无条件
-----------
交换:66 38
交换 55 38 i-1 7 j=2
交换 49 38 i-1 7 j=4
无条件
-----------
交换:55 9
交换 49 9 i-1 6 j=2
交换 38 9 i-1 6 j=4
无条件
-----------
交换:49 30
交换 43 30 j=3
无条件
-----------
交换:43 9
交换 38 9 i-1 4 j=2
无条件
-----------
交换:38 9
交换最后 30 9j=1
无条件
-----------
交换:30 9
-----------
交换:9 9
9
30
38
43
49
55
66
72
79
83
87
91