ヒープソート:大きなルートヒープを使用します
すべての配列がヒープに入力され、ヒープがヒープから放出され、背面から正面へのアレイに戻り、アレイは小から大部分に注文されます。
パブリッククラスmaxheap <tは比較可能<? Super t >> {private t [] data;プライベートINTサイズ。プライベートINT容量; public Maxheap(int capacity){this.data =(t [])new comparable [capacity + 1];サイズ= 0; this.capacity = capurity; } 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]; data [i] = data [j];データ[j] = temp; }} public void insert(t item){size ++; data [size] = item; Shiftup(size); } / ** * @return pop maximumroot(popupは、seekmaxと比較して削除を意味します) * / public t popmax(){swap(1、size--);シフトダウン(1);データを返す[size + 1]; } / ** * @param Child子ノードの下コーナーマークは子であり、親ノードの下コーナーテーブルは子供 / 2 * / public void shiftup(int chird){while(child> 1 && data [child] .compareto(data [child / 2])> 0){swap(child、child / 2); child = child / 2; }}/*** @paramデータアレイの要素の下部コーナーマークa; //戻りますa}}/*** @paramデータ配列の要素の下部コーナーマーク最大=最大(最大、c);大きく戻ります。 }/** * @param父親の親ノードの下コーナーマークは父親であり、左右の2つの子ノードの下コーナーテーブルは次のとおりです。父 * 2と父 * 2 + 1 */public void shiftdown(int father){while(true){int lchild = father * 2; //左の子供int rchild =父 * 2 + 1;親の下部コーナーマーク、左右のノードは誰ですか? if(lchild> size){//父ノードに左の子供も右の子供も戻っていない場合。 } else if(rchild> size){//父親のノードが子供しか残っていない場合、右の子供ニューファーザー=マックス(父、lchild); } else {//父のノードに左の子供と右の子供の両方がある場合、newfather = max(父、lchild、rchild); } if(newfather ==父){//父親は両方の子ノードよりも大きいことを意味し、テーブル名はすでに大きなルートヒープであるため、返品を調整し続ける必要はありません。 } else {//そうでなければ、大きな根のヒープ条件が満たされるまでヒープを調整し続ける必要があります(父、新人); super t >> void sort(t [] arr){int len = arr.length; // in-heap maxheap <t> maxheap = new maxheap <t>(len); for(int i = 0; i <len; i ++){maxheap.insert(arr [i]); } // outheap 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}}ヒープソート:アレイにヒープを作成します(最大ヒープ)
パブリッククラスmaxheap <tは比較可能<? Super t >> {private t [] data;プライベートINTサイズ。プライベートINT容量; public Maxheap(int capacity){this.capacity = capuration; this.size = 0; this.data =(t [])new comparable [capacity + 1]; } public maxheap(t [] arr){// heapify、arrayヒープ容量= arr.length; data =(t [])new Camparable [容量 + 1]; System.ArrayCopy(arr、0、data、1、arr.length); size = 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(){return data [1]; } public void swap(int i、int j){if(i!= j){t temp = data [i]; data [i] = data [j];データ[j] = temp; }} public void insert(t item){size ++; data [size] = item; Shiftup(size); } public t popmax(){swap(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データ配列の要素の下部コーナーマークデータ[a] Big Return a; //返品a}}/*** @paramデータアレイの要素の下部コーナーマーク最大=最大(最大、c);最大の戻り; } public void shiftdown(int father){while(true){int lchild = father * 2; int rchild =父 * 2 + 1; int newfather = father; //割り当てがここに割り当てられているかどうかは関係ありません。次の返品が破損するように変更された場合、(lChild> size){//左および右の子供がない場合は返される場合。 } else if(rchild> size){//正しい子供のnewfather = max(父、lchild); } else {//左と右の子供がいる場合、newfather = max(父、lchild、rchild); } if(newfather == father){//元の親ノードが3つの最大である場合、パイルリターンを整理し続ける必要はありません。 } else {//親ノードは最大ではなく、年長の子供を交換し、大きなルートヒープが満たされるまで下向きに調整し続けます(Newfather、父)。父=ニューファーザー; //シフトダウンに相当(新人)。ニューファーザーが父親の左の子であることが判明した場合、それはシフトダウン(2*父)と同等です(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配列の実装)は、私があなたと共有するすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。