From 9148841742d0e7ca4226fbff23b2f8f2c6f85837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=92=A7=E5=9E=AB=E6=AA=BD=E9=90=9E=E7=A0=95?= <407837686@qq.com> Date: Tue, 15 Dec 2020 18:45:32 +0800 Subject: [PATCH 1/3] homework --- base/Heap.class | Bin 0 -> 2382 bytes base/Heap.java | 155 ++++++++++++++++++++++++++++++++++++++++++++++++ base/Main.class | Bin 0 -> 900 bytes base/Main.java | 23 +++++++ 4 files changed, 178 insertions(+) create mode 100644 base/Heap.class create mode 100644 base/Heap.java create mode 100644 base/Main.class create mode 100644 base/Main.java diff --git a/base/Heap.class b/base/Heap.class new file mode 100644 index 0000000000000000000000000000000000000000..75fca78eaa109d6ea0b0cd177e63f3911e5adc6d GIT binary patch literal 2382 zcmcguT~JhI7=FIpvwIde!iubb=bQpfT^0dTBNbRt@dpJ_*UX=OSU4N3F0kUFQQ5Df zlVg~vQ&~2P+E86ZlW661;azr7-PCl|Z5K^VGtHDn(DR+M1dL8I-LyONe((4GpZ9ss zyL)r&)6W4c!$a9HP>}^4m6B9RvN#L(V~Gz-vuW^v57pj!+R9Hh9>j8Qz2CX1Sdoof zRQRw`o;8xxN>V2s!#=F?q27npKCJO!t&}yWSf^sWfYKR@CIwu38_7o+_pB2TWIgRY zecdSmZ*9Cgo(fYJY%JT!V{MNW6Ub|dcgME$b+yHMcSPF~l(?goB`_nBini}>jwUlw zuN6zgQc@k1-kxM{Tt>}~^z`+%$JWQCXVykmP`NL9D5_yD$~4@I5)CHq(NKyK6&o~c z1WU5*O4j!!5-K)n*o-CylD;MDx{|4W70nv9phcii#;Qt0yF02PDJJY#(-%)zv0eeK zWXeawR&0}GyM_pMXb8!O9@|1d%~-N@a%_+!f`x5H8QUQ93iu~8wY2Svwe!)Yh$8(5 zQ?V`v=;@;(&}19Od#bj|Ca|(tw5vv7)?LI*Z+9xuO~0VyIOTb2WG{0kb2mp*olKML zIn4Tl4Gm=t1TLCP#<~ef#ohex2$^XS>oExQzo>nw9;c}S1;INFYf#F$H8_@T2nixN zArZCDQu|cM&&52-?<2PwE`EXH&{c?#eg&>j=wrC9XJ3IkGxl~%sSI9%A6>QOxSd#$KykMsL97+3tW&*JHcLqs~`t{Hf$yeNOjWr z2%w5BUCb6PM=|O!2Mt7S6H$v0v4@G|6ZB26wFgm#mr#zAScErGfkAp+rsw;p#wYZ? zffe`)EAbtpe2*}Gz$*MmWPie1{K}}mVFSj{h=r`A5IF_VRGiVMXU%ES2v+|;_=fuI&B%H?_tpk`YFNTxE2q-Paw6KQKO18Nw@X&bVjuK3#1d}D0|3?X9U(46gnXDU}0HBF4V>9x& zdi~s`McBsK*-juM+^0KH!#Uc3U6k&oWDn_HJdQ_j1dno>9>ZDQ(rCqdh~fjZ;X|}z z1Qu>0hA+{T%N(WT)xoK~C`x%&XiEr@0l*a1zdP63%fxUguoAfqrh2XK^0y;w_^8Hon0He2aH* t8)pPCD7-jHq%YYh%|Z4kT;ea(2(Av7@GmGY58j675Bmq8+@8use*+y@<97f6 literal 0 HcmV?d00001 diff --git a/base/Heap.java b/base/Heap.java new file mode 100644 index 0000000..bc48933 --- /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 GIT binary patch literal 900 zcmah|+iDY06kR8mnPfU`Vw1F5Q?>OrZEWJDUQ-nmi-CB7h){f*B!@C3nF(iP0^uU(hjdv3i&Pps{FzQBa@pt|Nha!W1OUoF+buII0oRiaf3dfkRx_7pjJ zi{zr>*%(KL**B%zQT7QN3kr(Jm?+seiBkfTe^W707*k@oICp{_V;0&5iXh z?_P5I{lkWh(>P;u6laxn&c=CMFmchwC6sL}=>t^<0_p9_cj{}h5waDhqWX=x&89%S zLjiRD#;ZMk7|J#YL63plmRc_;-BnBqmTtSuvALa&^jTrS*<~I(T)zY{G2RGz>O@fy z$g`DyP!nOWHDH0Pvy;eT87=S6<^)VFszv#^-I}9#qbXZ5l$b%9Z%KU-2z67$EO*mf z1=sle0OB Date: Tue, 22 Dec 2020 22:02:28 +0800 Subject: [PATCH 2/3] homework --- work/base2/HeapSort.class | Bin 0 -> 1248 bytes work/base2/HeapSort.java | 61 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 work/base2/HeapSort.class create mode 100644 work/base2/HeapSort.java diff --git a/work/base2/HeapSort.class b/work/base2/HeapSort.class new file mode 100644 index 0000000000000000000000000000000000000000..a0aa20ad1ae1bdf4d1f2f3ad88d285918ed8db68 GIT binary patch literal 1248 zcmZux&utY7}Uv;trl z1sy%ur(*{`OJF|^XgH`tVqYAeE8VZ-5C)WWSVtCvD$tH&Na-UQj%vsW#76z9AB+h^ zvV)TX@@%c_3G__(RqtYJZrW>n?@m|fk#nwJ71*7{SiOBSd?lq~;~$6# zn41io=~-`vm2YtsuQda2jxDXVcwx00=GXF<*sXv)^W3=tJMY%(UbQT6G`mepKXP?y zWMUhs75J6>nMT9C)+Du{)}BQ;q4B|wvYECkyREw~EakuKIHJ1ZTH(a4?buD?0GFtT zTL?r!O4v!+%TtD0D6jn#;!hd^eU!1#8Ak%UV9{2g*SVQp)RU(EOfN;GQ<5>K6xEzk zEbf%Fs8foYbeJ(SL8+O%Fykh#%tXYQ7+OTv6GWCFOHUE)jV(Z|Wvk zI;WrH{MY=JTOzP%O3UQrnj zVA+w>BDzD_PJ68wbcNLq5D#opl6Ch|?`0Q;$>20OoaJepwu2Nc6(!$66K`R@!+t7@ zxm?u_*WXx6lu;M#RBkL4{ZUSxkd{n8!iXj16m_3%$sB)jD)$Jnc`4L6^E>vkB3h0m zM=jaTd%W?&PGy`;nl_sR^b_I`=Xn?d>R$skj$sHRID!KG=Xm;-UzMVoMC3h^LS)_| tO_uyfIxLU#3Dxr+#gf43oMWfkNbPlKkEFn$QDUCvE{!oY&6F-={sF1n3xxmx literal 0 HcmV?d00001 diff --git a/work/base2/HeapSort.java b/work/base2/HeapSort.java new file mode 100644 index 0000000..8e3da8e --- /dev/null +++ b/work/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 -- Gitee From 181f010208265c3d8ceb4617cd183018d12ae68c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=92=A7=E5=9E=AB=E6=AA=BD=E9=90=9E=E7=A0=95?= <407837686@qq.com> Date: Tue, 22 Dec 2020 22:11:20 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base2/HeapSort.class | Bin 0 -> 1248 bytes base2/HeapSort.java | 61 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 base2/HeapSort.class create mode 100644 base2/HeapSort.java diff --git a/base2/HeapSort.class b/base2/HeapSort.class new file mode 100644 index 0000000000000000000000000000000000000000..a0aa20ad1ae1bdf4d1f2f3ad88d285918ed8db68 GIT binary patch literal 1248 zcmZux&utY7}Uv;trl z1sy%ur(*{`OJF|^XgH`tVqYAeE8VZ-5C)WWSVtCvD$tH&Na-UQj%vsW#76z9AB+h^ zvV)TX@@%c_3G__(RqtYJZrW>n?@m|fk#nwJ71*7{SiOBSd?lq~;~$6# zn41io=~-`vm2YtsuQda2jxDXVcwx00=GXF<*sXv)^W3=tJMY%(UbQT6G`mepKXP?y zWMUhs75J6>nMT9C)+Du{)}BQ;q4B|wvYECkyREw~EakuKIHJ1ZTH(a4?buD?0GFtT zTL?r!O4v!+%TtD0D6jn#;!hd^eU!1#8Ak%UV9{2g*SVQp)RU(EOfN;GQ<5>K6xEzk zEbf%Fs8foYbeJ(SL8+O%Fykh#%tXYQ7+OTv6GWCFOHUE)jV(Z|Wvk zI;WrH{MY=JTOzP%O3UQrnj zVA+w>BDzD_PJ68wbcNLq5D#opl6Ch|?`0Q;$>20OoaJepwu2Nc6(!$66K`R@!+t7@ zxm?u_*WXx6lu;M#RBkL4{ZUSxkd{n8!iXj16m_3%$sB)jD)$Jnc`4L6^E>vkB3h0m zM=jaTd%W?&PGy`;nl_sR^b_I`=Xn?d>R$skj$sHRID!KG=Xm;-UzMVoMC3h^LS)_| tO_uyfIxLU#3Dxr+#gf43oMWfkNbPlKkEFn$QDUCvE{!oY&6F-={sF1n3xxmx literal 0 HcmV?d00001 diff --git a/base2/HeapSort.java b/base2/HeapSort.java new file mode 100644 index 0000000..8e3da8e --- /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 -- Gitee