diff --git a/base1/Heap.class b/base1/Heap.class new file mode 100644 index 0000000000000000000000000000000000000000..75fca78eaa109d6ea0b0cd177e63f3911e5adc6d Binary files /dev/null and b/base1/Heap.class differ diff --git a/base1/Heap.java b/base1/Heap.java new file mode 100644 index 0000000000000000000000000000000000000000..bc48933964618f99126bfffe6751db7d578f4ee3 --- /dev/null +++ b/base1/Heap.java @@ -0,0 +1,155 @@ +public class Heap{ + public int[] heap; + public int SIZE; + public int count; + + Heap(int size){ + SIZE = size; + // reference: https://stackoverflow.com/questions/34827626/cannot-be-cast-to-ljava-lang-comparable + heap = new int[SIZE]; + count = 0; + } + + public void add (int item){ + if(count >= SIZE){ + System.out.println("Heap Full"); + return; + } + if(count == 0){ + heap[count] = item; + count++; + return; + } + if(count>=1){ + int index = count; + heap[index] = item; + int ind; + if(index%2==0){ + ind = (index/2)-1; + }else { + ind =index/2; + } + while(heap[ind] heap[parent]){ + int temp; + temp = heap[parent]; + heap[parent] = heap[index]; + heap[index] = temp; + } + }else { + int parent=index/2; + + if(heap[index] > heap[parent]){ + int temp; + temp = heap[parent]; + heap[parent] = heap[index]; + heap[index] = temp; + } + } + index = ind; + if(ind%2==0){ + ind = ind/2-1; + }else { + ind = ind/2; + } + if(ind==-1){ + break; + } + } + count++; + } + } + + public void delete(){ + if(count <=0 ){ + System.out.println("Heap Empty"); + return; + } + if(count==1){ + heap[0]=0; + count--; + return; + } + + heap[0]= heap[count-1]; + heap[count-1] = 0; + count--; + int index = 0; + int left = 2*index+1; + int right = 2*index+2; + while (heap[index] heap[right]) { + int temp = heap[index]; + heap[index] = heap[left]; + heap[left] = temp; + index = left;//3 + left = 2*index+1; + right = 2*index+2; + if(left+1>SIZE && right+1>SIZE){ + return; + } + if(left+1<=SIZE && right+1>SIZE){ + if(heap[index]>heap[left]){ + return; + }else { + temp = heap[index]; + heap[index] = heap[left]; + heap[left] = temp; + return; + } + } + } else { + int temp = heap[index]; + heap[index] = heap[right]; + heap[right] = temp; + index = right; + left = 2*index+1; + right = 2*index+2; + if(left+1>SIZE && right+1>SIZE){ + return; + } + if(left+1<=SIZE && right+1>SIZE){ + if(heap[index]>heap[left]){ + return; + }else { + temp = heap[index]; + heap[index] = heap[left]; + heap[left] = temp; + return; + } + } + + + } + } + } + + public void print(){ + for(int j=1;j<=4;j++){ + int tail = (int) (Math.pow(2,j)-1); + int head = tail-(int)Math.pow(2,j-1); + if(tail == (int) (Math.pow(2,4)-1)){ + int sheng = tail-SIZE; + tail = tail-sheng; + } + for(int i = head;i < tail; i++){ + if(SIZE==tail-1){ + return; + } + if(j==1){ + System.out.print(" "+heap[i]); + }else if(j==2){ + System.out.print(" "+heap[i]+" "); + }else if(j==3){ + System.out.print(" "+heap[i]+" "); + }else if(j==4){ + System.out.print(" "+heap[i]+" "); + } + } + System.out.println(); + } + } +} \ No newline at end of file diff --git a/base1/Main.class b/base1/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..341c62641c53d24b4b2980150c6bcd3f31b87655 Binary files /dev/null and b/base1/Main.class differ diff --git a/base1/Main.java b/base1/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..685a5917ea29f172dcf282b3727bda128942b77a --- /dev/null +++ b/base1/Main.java @@ -0,0 +1,23 @@ +public class Main{ + public static void main(String args[]){ + Heap hp =new Heap(10); + hp.add(12); + hp.add(19); + hp.add(10); + hp.add(9); + hp.add(11); + hp.add(14); + hp.add(6); + hp.add(8); + hp.add(4); + hp.add(20); + + hp.print(); + System.out.println("数组里的个数:"+hp.count); + + hp.delete(); + hp.print(); + System.out.println("数组里的个数:"+hp.count); + + } +} \ No newline at end of file diff --git a/base2/HeapSort.class b/base2/HeapSort.class new file mode 100644 index 0000000000000000000000000000000000000000..a0aa20ad1ae1bdf4d1f2f3ad88d285918ed8db68 Binary files /dev/null and b/base2/HeapSort.class differ diff --git a/base2/HeapSort.java b/base2/HeapSort.java new file mode 100644 index 0000000000000000000000000000000000000000..8e3da8e07a2dbd10f68e37fcf98169a42d4c2f5b --- /dev/null +++ b/base2/HeapSort.java @@ -0,0 +1,61 @@ +import java.util.Arrays; +/** + * Created by chengxiao on 2016/12/17. + * 堆排序demo + */ +public class HeapSort { + public static void main(String []args){ + int []arr = {1,3,4,5,2,6,9,7,8,0};//10 + sort(arr); + System.out.println("出堆排序:"+Arrays.toString(arr)); + } + public static void sort(int []arr){ + //1.构建大顶堆 + for(int i=arr.length/2-1;i>=0;i--){ + //从第一个非叶子结点从下至上,从右至左调整结构 + adjustHeap(arr,i,arr.length); + } + System.out.println("无序的数组经过堆排序后:"+Arrays.toString(arr)); + //2.调整堆结构+交换堆顶元素与末尾元素 + for(int j=arr.length-1;j>0;j--){ + swap(arr,0,j);//将堆顶元素与末尾元素进行交换 + adjustHeap(arr,0,j);//重新对堆进行调整 + } + + } + + /** + * 调整大顶堆(仅是调整过程,建立在大顶堆已构建的基础上) + * @param arr + * @param i + * @param length + */ + public static void adjustHeap(int []arr,int i,int length){ + int temp = arr[i];//先取出当前元素i + for(int k=i*2+1;ktemp){//如果子节点大于父节点,将子节点值赋给父节点(不用进行交换) + arr[i] = arr[k]; + i = k; + }else{ + break; + } + } + arr[i] = temp;//将temp值放到最终的位置 + } + + /** + * 交换元素 + * @param arr + * @param a + * @param b + */ + public static void swap(int []arr,int a ,int b){ + int temp=arr[a]; + arr[a] = arr[b]; + arr[b] = temp; + } +} \ No newline at end of file