힙 분류 : 큰 뿌리를 사용합니다
모든 어레이는 힙에 입력 된 다음 힙을 힙에서 방출하고 배열에 다시 삽입되며 배열은 작게 주문됩니다.
공개 클래스 MaxHeap <t는 비슷한 <? 슈퍼 t >> {private t [] 데이터; 개인 INT 크기; 개인 INT 용량; public maxHeap (int capacity) {this.data = (t []) 새로운 비슷한 [용량 + 1]; 크기 = 0; this.capacity = 용량; } public int size () {return this.size; } public boolean isempty () {return size == 0; } public int getCapacity () {return this.capacity; } / ** * @return 최대 루트를 봅니다 (Popmax와 비교하여 삭제 없이만 참조) * / public t seekmax () {return data [1]; } public void swap (int i, int j) {if (i! = j) {t temp = data [i]; 데이터 [i] = 데이터 [j]; 데이터 [j] = 온도; }} public void insert (t item) {size ++; 데이터 [size] = 항목; Shiftup (크기); } / ** * @return 팝 최대 루트 (팝업은 seekMax와 비교하여 삭제를 의미합니다) * / public t popmax () {스왑 (1, size--); 시프트 다운 (1); 반환 데이터 [size + 1]; } / ** * @param child 자식 노드의 하단 모서리 마크는 자식이며, 부모 노드의 하부 코너 테이블은 child / 2 * / public void shiftup (int child) {while (while> 1 && data [child] .compareto (data [child / 2]) {swap (child, child / 2); 자녀 = 자녀 / 2; }}/*** @param 데이터 배열에서 요소의 하단 코너 마크 A @param b 데이터 배열의 요소의 하단 모서리 마크* @retern 요소의 하단 모서리 마크는 큰*/private int max (int a, int b) {if [a] .compareto (data [b]) <0) {// 빅 리턴 B; // return b; // if return b; a; // return a}}/*** @param 데이터 배열에서 요소의 하단 코너 마크* @param b 데이터 배열에서 요소의 하단 코너 마크 데이터 배열에서 요소의 하단 모서리 마크*/private int max (int b, int c) {int b) {a, b); 가장 큰 = 최대 (가장 큰, c); 더 큰 반환; }/** * @param 아버지 부모 노드의 하부 모서리 마크는 아버지이며, 왼쪽과 오른쪽의 하부 코너 테이블은 다음과 같습니다. 아버지 * 2와 아버지 * 2 + 1 */public void shiftdown (int 아버지) {int lchild = {int lchild = int lchild = int int rchild = rachild = 아버지 * 2 + 1; // 아버지; 부모, 왼쪽 및 오른쪽 노드의 하단 코너 마크는 누구입니까? if (lchild> size) {// 아버지 노드에 자녀가 남지 않거나 오른쪽 아이가 돌아 오지 않는 경우; } else if (rchild> size) {// 아버지 노드가 자녀 만 남지 않은 경우, 오른쪽 자녀 Newfather = Max (아버지, lchild); } else {// 아버지 노드가 남은 아이와 오른쪽 자녀 Newfather = Max (아버지, Lchild, Rchild); } if (newfather == 아버지) {// 아버지가 아동 노드보다 크고 테이블 이름이 이미 큰 루트 더기이므로 반품을 계속 조정할 필요가 없습니다. } else {// 그렇지 않으면, 큰 루트 힙 조건이 충족 될 때까지 힙을 계속 조정해야합니다 (아버지, 신 Newfather); // 값 거래소 아버지 = Newfather; // 아버지의 가치를 업데이트합니다. super t >> void sort (t [] arr) {int len = arr.length; // eap maxHeap <t> maxHeap = New MaxHeap <t> (Len); for (int i = 0; i <len; i ++) {maxHeap.insert (arr [i]); } // (int i = len-1; i> = 0; i-) {arr [i] = maxHeap.popMax (); }} public static void printarr (object [] arr) {for (object o : arr) {system.out.print (o); System.out.print ( "/t"); } system.out.println (); } public static void main (String args []) {integer [] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6}; printarr (arr); // 3 5 1 7 2 9 8 0 4 6 정렬 (ARR); printarr (arr); // 0 1 2 3 4 5 6 7 8 9}}힙 분류 : 배열의 힙 구성 (최대 힙)
공개 클래스 MaxHeap <t는 비슷한 <? 슈퍼 t >> {private t [] 데이터; 개인 INT 크기; 개인 INT 용량; public maxHeap (int capacity) {this.capacity = 용량; this.size = 0; this.data = (t []) 새로운 비교 [용량 + 1]; } public maxHeap (t [] arr) {// heapify, 배열 힙 용량 = arr.length; data = (t []) 새로운 비슷한 [용량 + 1]; System.ArrayCopy (ARR, 0, DATA, 1, ARR.LENGTH); 크기 = arr.length; for (int i = size / 2; i> = 1; i-) {shiftdown (i); }} public int size () {return this.size; } public int getCapacity () {return this.capacity; } public boolean isempty () {return size == 0; } public t seekMax () {반환 데이터 [1]; } public void swap (int i, int j) {if (i! = j) {t temp = data [i]; 데이터 [i] = 데이터 [j]; 데이터 [j] = 온도; }} public void insert (t item) {size ++; 데이터 [size] = 항목; Shiftup (크기); } public t popmax () {스왑 (1, size-); 시프트 다운 (1); 반환 데이터 [size + 1]; } public void shiftup (int child) {while (child> 1 && data [child] .compareto (data [child / 2])> 0) {swap (child, child / 2); 자식 /= 2; }}/*** @param 데이터 배열에서 요소의 하단 코너 마크* @param b 데이터 배열에서 요소의 하단 코너 마크 @return 요소가 더 큽니다. 요소의 하단 모서리 마크가 더 큽니다*/private int max (int a, int b) {if (data [a] .compareto (data [b]) <0) {// retain b} 데이터 [a] 큰 반환 a; // return a}}/*** @param 데이터 배열에서 요소의 하단 모서리 마크* @param b 데이터 배열의 요소의 하단 모서리 마크 데이터 배열의 요소의 하단 모서리 마크* @ @int max (int a, int c) {int a, int c) {int b, int c). 가장 큰 = 최대 (가장 큰, c); 가장 큰 반환; } public void shiftdown (int father) {while (true) {int lchild = 아버지 * 2; int rchild = 아버지 * 2 + 1; int newfather = 아버지; // 할당이 여기에 할당되었는지 여부는 중요하지 않습니다. 다음 반환이 중단되도록 변경되면 (lchild> size) {// 왼쪽과 오른쪽이없는 경우 리턴이 있으면 할당해야합니다. } else if (rchild> size) {// 올바른 아이가 없으면 newfather = max (아버지, lchild); } else {// 왼쪽과 오른쪽 자녀가있는 경우 Newfather = Max (아버지, Lchild, Rchild); } if (newfather == 아버지) {// 원래 상위 노드가 3 개 중 가장 큰 경우 더미 반환을 계속 정리할 필요가 없습니다. } else {// 상위 노드는 가장 크지 않으며, 나이가 많은 어린이를 교환하고 큰 루트 힙이 스왑이 만족 될 때까지 아래쪽으로 계속 조정합니다 (Newfather, Father); 아버지 = Newfather; // Shift Down (Newfather)에 해당합니다. 뉴 파트가 아버지의 좌파 자녀로 판명되면, 교대 (2*아버지)}}} public static <t는 비슷한 <? super t >> void sort (t [] arr) {int len = arr.length; MaxHeap <t> maxHeap = New MaxHeap <> (ARR); for (int i = len-1; i> = 0; i-) {arr [i] = maxHeap.popMax (); }} public static void printarr (object [] arr) {for (object o : arr) {system.out.print (o); System.out.print ( "/t"); } system.out.println (); } public static void main (String args []) {integer [] arr = {3, 5, 1, 7, 2, 9, 8, 0, 4, 6}; printarr (arr); // 3 5 1 7 2 9 8 0 4 6 정렬 (ARR); printarr (arr); // 0 1 2 3 4 5 6 7 8 9}}위의 힙 정렬 예제 (Java Array 구현)는 내가 공유하는 모든 컨텐츠입니다. 나는 당신이 당신에게 참조를 줄 수 있기를 바랍니다. 그리고 당신이 wulin.com을 더 지원할 수 있기를 바랍니다.