From 876cefa2583331037be97a912dc41d7c053bacb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E6=A2=A6=E5=A8=87?= <1791095245@qq.com> Date: Tue, 15 Dec 2020 20:17:27 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- work/Heap.class | Bin 0 -> 2382 bytes work/Heap.java | 155 ++++++++++++++++++++++++++++++++++++++++++++++++ work/Main.class | Bin 0 -> 900 bytes work/Main.java | 23 +++++++ 4 files changed, 178 insertions(+) create mode 100644 work/Heap.class create mode 100644 work/Heap.java create mode 100644 work/Main.class create mode 100644 work/Main.java diff --git a/work/Heap.class b/work/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/work/Heap.java b/work/Heap.java new file mode 100644 index 0000000..bc48933 --- /dev/null +++ b/work/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/work/Main.class b/work/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 20:48:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- work1/Heap.class | Bin 0 -> 2382 bytes work1/Heap.java | 155 +++++++++++++++++++++++++++++++++++++++++++ work1/Main.class | Bin 0 -> 900 bytes work1/Main.java | 23 +++++++ work2/HeapSort.class | Bin 0 -> 1249 bytes work2/HeapSort.java | 57 ++++++++++++++++ 6 files changed, 235 insertions(+) create mode 100644 work1/Heap.class create mode 100644 work1/Heap.java create mode 100644 work1/Main.class create mode 100644 work1/Main.java create mode 100644 work2/HeapSort.class create mode 100644 work2/HeapSort.java diff --git a/work1/Heap.class b/work1/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>j8Qz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 b/work1/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/work1/Main.class b/work1/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 z73wuAH$X@TL?wg}5?AOULeh*m-Z>zBk|ZzInTUykA)Z zFoJO%UFg&C5e_DC2>luk>yYS6;1gvBbbN|I<$b0jiy@V0Cort+5e-K*w1b87+`lP1G7+4xZf^g^{+pYtyG3aL{i z6?j@s0U0o{$G|@9*N`{xIgSbJ|38B-n|`_E)yaGP#>)DmpEqvaSzo#J`sD)y$8kc# zh=G$BRm&+2V+Ov!X#*eQfWW}Uy+=&le6YB2_u1y^;^ykDSHIo(_lWh|H^Wm>DmU>D zF#&U%fipYr&9U;4M(nYLhRlmSBE0f0=3p)XZ}i#;Lqpt9g}@z|rh3l70|s z_sGH)QZw+&`E&KUd%ZzwLA7-k;SEg=eNfG^z1dyEJz*>VWydS3DXwMSxV0O*Y4mf5 zx;TYE45WlTT)Vl;&_~Vh{=2A0!^2qXPz8*U?M$MyT2u>UN%yN%{_j>1CD)`3{uW5gGwQ2Wdi^s*y* zjwL57*{XZG^}C(QINLOBQUm%3agfCNF^Iz$!T^r(M>59UIO7*EjIVGcq}qY#U+54b v^A>5c=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