diff --git "a/1,\350\256\276\347\275\256\350\264\246\345\217\267.bat" "b/1,\350\256\276\347\275\256\350\264\246\345\217\267.bat" new file mode 100644 index 0000000000000000000000000000000000000000..ac7e603f607b06a8dea612634ab3d16bac6cb9d2 --- /dev/null +++ "b/1,\350\256\276\347\275\256\350\264\246\345\217\267.bat" @@ -0,0 +1,2 @@ +git config --global user.name "苏瑜煌" +git config --global user.email "1394157072@qq.com" \ No newline at end of file diff --git "a/2,\345\210\207\346\215\242\345\210\206\346\224\257.bat" "b/2,\345\210\207\346\215\242\345\210\206\346\224\257.bat" new file mode 100644 index 0000000000000000000000000000000000000000..1ec69c41c0deeb98cfd6290e04f42c025b86b003 --- /dev/null +++ "b/2,\345\210\207\346\215\242\345\210\206\346\224\257.bat" @@ -0,0 +1,2 @@ +git branck 39 +git checkout 39 \ No newline at end of file diff --git "a/3,\350\207\252\345\212\250\346\217\220\344\272\244.bat" "b/3,\350\207\252\345\212\250\346\217\220\344\272\244.bat" new file mode 100644 index 0000000000000000000000000000000000000000..0fbd52fa8111ae13721da1f2c119822552292813 --- /dev/null +++ "b/3,\350\207\252\345\212\250\346\217\220\344\272\244.bat" @@ -0,0 +1,3 @@ +git add . +git commit -m "AutoPush" +git push \ No newline at end of file diff --git a/base/Heap.class b/base/Heap.class new file mode 100644 index 0000000000000000000000000000000000000000..75fca78eaa109d6ea0b0cd177e63f3911e5adc6d Binary files /dev/null and b/base/Heap.class differ diff --git a/base/Heap.java b/base/Heap.java new file mode 100644 index 0000000000000000000000000000000000000000..bc48933964618f99126bfffe6751db7d578f4ee3 --- /dev/null +++ b/base/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/base/Main.class b/base/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..341c62641c53d24b4b2980150c6bcd3f31b87655 Binary files /dev/null and b/base/Main.class differ diff --git a/base/Main.java b/base/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..42525437efade4916baa6f60980b08dc2f5e808d --- /dev/null +++ b/base/Main.java @@ -0,0 +1,23 @@ +public class Main{ + public static void main(String args[]){ + Heap hp =new Heap(10); + hp.add(1); + hp.add(2); + hp.add(3); + hp.add(4); + hp.add(5); + hp.add(6); + hp.add(7); + hp.add(8); + hp.add(9); + hp.add(10); + + 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