@@ -837,74 +837,76 @@ public async mergeSort(arr, start, end){
837837 public async heapsort ( arr : SortValue [ ] , options : AlgorithmOptions ) {
838838 switch ( options . heapType ) {
839839 case 'min' :
840- return await this . minHeapsort ( arr ) ;
840+ return await this . minHeapsort ( arr , options . childCount ) ;
841841 case 'max' :
842- return await this . maxHeapsort ( arr ) ;
842+ return await this . maxHeapsort ( arr , options . childCount ) ;
843843 }
844844 }
845845
846- private async minHeapsort ( arr : SortValue [ ] ) {
846+ private async minHeapsort ( arr : SortValue [ ] , childCount : number ) {
847847 for ( let i = Math . ceil ( arr . length / 2 ) + 1 ; i < arr . length ; i ++ ) {
848- await this . minHeapify ( arr , 0 , i ) ;
848+ await this . minHeapify ( arr , 0 , i , childCount ) ;
849849 }
850850 for ( let i = 0 ; i < arr . length ; i ++ ) {
851851 await this . context . drawAndSwap ( arr , arr . length - 1 , i ) ;
852- await this . minHeapify ( arr , i , arr . length - 1 ) ;
852+ await this . minHeapify ( arr , i , arr . length - 1 , childCount ) ;
853853 }
854854 }
855855
856- private async maxHeapsort ( arr : SortValue [ ] ) {
856+ private async maxHeapsort ( arr : SortValue [ ] , childCount : number ) {
857857 for ( let i = Math . floor ( arr . length / 2 ) - 1 ; i >= 0 ; i -- ) {
858- await this . maxHeapify ( arr , arr . length , i ) ;
858+ await this . maxHeapify ( arr , arr . length , i , childCount ) ;
859859 }
860860 for ( let i = arr . length - 1 ; i > 0 ; i -- ) {
861861 await this . context . drawAndSwap ( arr , 0 , i ) ;
862- await this . maxHeapify ( arr , i , 0 ) ;
862+ await this . maxHeapify ( arr , i , 0 , childCount ) ;
863863 }
864864 }
865865
866866 // This a reversed heap, with the smallest element at the last index
867- private async minHeapify ( arr : SortValue [ ] , n : number , i : number ) {
867+ private async minHeapify (
868+ arr : SortValue [ ] ,
869+ n : number ,
870+ i : number ,
871+ childCount : number ,
872+ ) {
868873 let smallestIndex = i ;
869- const left = arr . length - 1 - ( 2 * ( arr . length - 1 - i ) + 2 ) ;
870- const right = arr . length - 1 - ( 2 * ( arr . length - 1 - i ) + 1 ) ;
871- if (
872- left > n &&
873- ( await this . context . compare ( arr , smallestIndex , '>' , left ) )
874- ) {
875- smallestIndex = left ;
876- }
877- if (
878- right > n &&
879- ( await this . context . compare ( arr , smallestIndex , '>' , right ) )
880- ) {
881- smallestIndex = right ;
874+ for ( let c = 0 ; c < childCount ; c ++ ) {
875+ const childIndex =
876+ arr . length - 1 - ( childCount * ( arr . length - 1 - i ) + 1 + c ) ;
877+ if (
878+ childIndex > n &&
879+ ( await this . context . compare ( arr , childIndex , '<' , smallestIndex ) )
880+ ) {
881+ smallestIndex = childIndex ;
882+ }
882883 }
883884 if ( smallestIndex !== i ) {
884885 await this . context . drawAndSwap ( arr , i , smallestIndex ) ;
885- await this . minHeapify ( arr , n , smallestIndex ) ;
886+ await this . minHeapify ( arr , n , smallestIndex , childCount ) ;
886887 }
887888 }
888889
889- private async maxHeapify ( arr : SortValue [ ] , n : number , i : number ) {
890+ private async maxHeapify (
891+ arr : SortValue [ ] ,
892+ n : number ,
893+ i : number ,
894+ childCount : number ,
895+ ) {
890896 let largestIndex = i ;
891- const left = 2 * i + 1 ;
892- const right = 2 * i + 2 ;
893- if (
894- left < n &&
895- ( await this . context . compare ( arr , left , '>' , largestIndex ) )
896- ) {
897- largestIndex = left ;
898- }
899- if (
900- right < n &&
901- ( await this . context . compare ( arr , right , '>' , largestIndex ) )
902- ) {
903- largestIndex = right ;
897+ for ( let c = 0 ; c < childCount ; c ++ ) {
898+ const childIndex = childCount * i + 1 + c ;
899+ if (
900+ childIndex < n &&
901+ ( await this . context . compare ( arr , childIndex , '>' , largestIndex ) )
902+ ) {
903+ largestIndex = childIndex ;
904+ }
904905 }
906+
905907 if ( largestIndex !== i ) {
906- await this . context . drawAndSwap ( arr , i , largestIndex ) ;
907- await this . maxHeapify ( arr , n , largestIndex ) ;
908+ await this . context . drawAndSwap ( arr , i , largestIndex , childCount ) ;
909+ await this . maxHeapify ( arr , n , largestIndex , childCount ) ;
908910 }
909911 }
910912
0 commit comments