Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
BinaryBall committed Sep 5, 2019
1 parent 3ab8957 commit 9516de9
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 15 deletions.
13 changes: 11 additions & 2 deletions heap/src/com/jamal/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class App {
public static void main(String[] args) {
// heap();
sort();
sort1();
}

public static void heap(){
Expand All @@ -29,7 +29,16 @@ public static void heap(){
public static void sort(){
int[] a = {2,4,6,8,5,1,9,13,56,34,67,23};
HeadSort sort = new HeadSort();
sort.sort(a,11);
sort.sort(a,12);
for (int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
}

public static void sort1(){
int[] a = {2,4,6,8,5,1,9,13,56,34,67,23};
HeadSort1 sort = new HeadSort1();
sort.sort(a,12);
for (int i=0;i<a.length;i++){
System.out.print(a[i]+" ");
}
Expand Down
45 changes: 35 additions & 10 deletions heap/src/com/jamal/HeadSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,37 @@
public class HeadSort {


private void buildHeap(int[] a, int n) {
for (int i = n/2; i >= 1; --i) {
heapify(a, n, i);
}
// private void buildHeap(int[] a, int n) {
// for (int i = n/2; i >= 1; --i) {
// heapify(a, n, i);
// }
// }
//
// private void heapify(int[] a, int n, int i) {
// while (true) {
// int maxPos = i;
// if (i*2 <= n && a[i] < a[i*2]) maxPos = i*2;
// if (i*2+1 <= n && a[maxPos] < a[i*2+1]) maxPos = i*2+1;
// if (maxPos == i) break;
// swap(a, i, maxPos);
// i = maxPos;
// }
// }
// // n 表示数据的个数,数组 a 中的数据从下标 1 到 n 的位置。
// public void sort(int[] a, int n) {
// buildHeap(a, n);
// int k = n;
// while (k > 1) {
// swap(a, 1, k);
// --k;
// heapify(a, k, 1);
// }
// }
private void buildHeap(int[] a, int n) {
for (int i = n/2; i >= 0; i--) {
heapify(a, n, i);
}
}

private void heapify(int[] a, int n, int i) {
while (true) {
Expand All @@ -28,15 +54,14 @@ private void heapify(int[] a, int n, int i) {
}
// n 表示数据的个数,数组 a 中的数据从下标 1 到 n 的位置。
public void sort(int[] a, int n) {
buildHeap(a, n);
int k = n;
while (k > 1) {
swap(a, 1, k);
buildHeap(a, n-1);
int k = n-1;
while (k > 0) {
swap(a, 0, k);
--k;
heapify(a, k, 1);
heapify(a, k, 0);
}
}

private void swap(int[] a, int i, int i1) {
int temp = a[i];
a[i]=a[i1];
Expand Down
31 changes: 31 additions & 0 deletions heap/src/com/jamal/HeadSort1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.jamal;

/**
* @author xiaoxiang
* @title: HeadSort
* @projectName heap
* @description: TODO
* @date 2019/9/517:39
*/
public class HeadSort1 {



// n 表示数据的个数,数组 a 中的数据从下标 1 到 n 的位置。
public void sort(int[] a, int n) {
Heap heap = new Heap(n+1);
// 先将数组添加到堆中
for (int i =0;i<n;i++){
heap.insert(a[i]);
}
for (int i =0;i<n;i++){
a[i] = heap.pop();
}
}
private void swap(int[] a, int i, int i1) {
int temp = a[i];
a[i]=a[i1];
a[i1] = temp;
}

}
16 changes: 13 additions & 3 deletions heap/src/com/jamal/Heap.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.jamal;

/**
* @author xiaoxiang
* @author xiaoxiang https://www.cnblogs.com/hapjin/p/4622681.html
* @title: Heap
* @projectName heap
* @description: TODO
Expand All @@ -20,15 +20,16 @@ public Heap(int capacity) {
}

/**
* 堆的插入,采用从下往上的堆化
* 堆的插入,采用从下往上的堆化,需要浪费第一个存储空间,即数组下标1为堆顶
* @param data
*/
public void insert(int data) {
if (count >= n) return; // 堆满了
++count;
a[count] = data;
int i = count;
while (i/2 > 0 && a[i] > a[i/2]) { // 自下往上堆化
//
while (i/2 > 0 && a[i] > a[i/2]) {
swap(a, i, i/2); // swap() 函数作用:交换下标为 i 和 i/2 的两个元素
i = i/2;
}
Expand All @@ -45,6 +46,15 @@ public void removeMax() {
heapify(a, count, 1);
}

public int pop(){
if (count == 0) return -1; // 堆中没有数据
int temp = a[1];
a[1] = a[count];
a[count] = 0;
--count;
heapify(a, count, 1);
return temp;
}

/**
* 堆化,将最大的元素放到堆顶
Expand Down

0 comments on commit 9516de9

Please sign in to comment.