@@ -137,7 +137,7 @@ public void OnSortClick() {
137137 case 6 :
138138 sortButton . interactable = false ;
139139 sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
140- StartCoroutine ( HeapSort ( array , array . Length ) ) ;
140+ StartCoroutine ( HeapSort ( array ) ) ;
141141 break ;
142142 case 7 :
143143 sortButton . interactable = false ;
@@ -459,29 +459,6 @@ IEnumerator ShellSort(int[] arr) {
459459 }
460460 }
461461
462- int partition ( int [ ] a , int low , int high ) {
463- int pivot = a [ high ] ;
464- int i = low - 1 ;
465- int temp ;
466-
467- for ( int j = low ; j <= high - 1 ; j ++ ) {
468-
469- if ( a [ j ] <= pivot ) {
470-
471- i ++ ;
472- temp = a [ i ] ;
473- a [ i ] = a [ j ] ;
474- a [ j ] = temp ;
475- }
476- }
477-
478- temp = a [ i + 1 ] ;
479- a [ i + 1 ] = a [ high ] ;
480- a [ high ] = temp ;
481-
482- return i + 1 ;
483- }
484-
485462 IEnumerator QuickSort ( int [ ] a , int low , int high ) {
486463 List < GameObject > pillars = new List < GameObject > ( ) ;
487464 foreach ( Transform tran in GameObject . Find ( "Bar" ) . transform ) {
@@ -564,61 +541,43 @@ IEnumerator QuickSort(int[] a, int low, int high) {
564541 }
565542 }
566543
567- public void swap ( int [ ] a , int i , int j ) {
568- int temp = a [ i ] ;
569- a [ i ] = a [ j ] ;
570- a [ j ] = temp ;
571- }
572-
573- IEnumerator HeapSort ( int [ ] arr , int n ) {
544+ IEnumerator HeapSort ( int [ ] arr ) {
574545 List < GameObject > pillars = new List < GameObject > ( ) ;
575546 foreach ( Transform tran in GameObject . Find ( "Bar" ) . transform ) {
576547 pillars . Add ( tran . gameObject ) ;
577548 }
578549
579- yield return StartCoroutine ( buildMaxHeap ( arr , n ) ) ;
550+ int N = arr . Length ;
580551
581- for ( int i = 0 ; i < arr . Length ; i ++ ) {
582- pillars [ i ] . GetComponent < Pillar > ( ) . Color = whiteColor ;
552+ // Build heap (rearrange array)
553+ for ( int i = N / 2 - 1 ; i >= 0 ; i -- ) {
554+ yield return new WaitForSeconds ( time ) ;
555+ yield return heapify ( arr , N , i ) ;
556+ }
557+
558+ for ( int j = 0 ; j < arr . Length ; j ++ ) {
559+ pillars [ j ] . GetComponent < Pillar > ( ) . Color = whiteColor ;
583560 }
584561
585- for ( int i = n - 1 ; i > 0 ; i -- ) {
586- pillars [ i ] . GetComponent < Pillar > ( ) . Color = swapColor ;
587- pillars [ i - 1 ] . GetComponent < Pillar > ( ) . Color = nextColor ;
562+ // One by one extract an element from heap
563+ for ( int i = N - 1 ; i > 0 ; i -- ) {
588564 yield return new WaitForSeconds ( time ) ;
589- // swap value of first indexed
590- // with last indexed
591-
592- swap ( arr , 0 , i ) ;
593565
594- // maintaining heap property
595- // after each swapping
596- int j = 0 , index ;
566+ // Move current root to end
567+ int temp = arr [ 0 ] ;
568+ arr [ 0 ] = arr [ i ] ;
569+ arr [ i ] = temp ;
597570
598- do {
599- index = ( 2 * j + 1 ) ;
600-
601- // if left child is smaller than
602- // right child point index variable
603- // to right child
604- if ( index < ( i - 1 ) && arr [ index ] <
605- arr [ index + 1 ] )
606- index ++ ;
607-
608- // if parent is smaller than child
609- // then swapping parent with child
610- // having higher value
611- if ( index < i && arr [ j ] < arr [ index ] )
612- swap ( arr , j , index ) ;
613-
614- j = index ;
615-
616- if ( index >= 0 && index < arr . Length ) {
617- pillars [ index ] . GetComponent < Pillar > ( ) . Color = tempColor ;
571+ pillars [ i ] . GetComponent < Pillar > ( ) . Color = checkColor ;
572+
573+ for ( int j = 0 ; j < arr . Length ; j ++ ) {
574+ if ( pillars [ j ] . GetComponent < Pillar > ( ) . Color != checkColor ) {
575+ pillars [ j ] . GetComponent < Pillar > ( ) . Color = whiteColor ;
618576 }
619-
620- yield return new WaitForSeconds ( time ) ;
621- } while ( index < i ) ;
577+ }
578+
579+ // call max heapify on the reduced heap
580+ yield return heapify ( arr , i , 0 ) ;
622581 }
623582
624583 if ( IsSorted ( this . array ) ) {
@@ -628,30 +587,36 @@ IEnumerator HeapSort(int[] arr, int n) {
628587 }
629588 }
630589
631- IEnumerator buildMaxHeap ( int [ ] arr , int n ) {
590+ IEnumerator heapify ( int [ ] arr , int N , int i ) {
632591 List < GameObject > pillars = new List < GameObject > ( ) ;
633592 foreach ( Transform tran in GameObject . Find ( "Bar" ) . transform ) {
634593 pillars . Add ( tran . gameObject ) ;
635594 }
636595
637- for ( int i = 1 ; i < n ; i ++ ) {
638- yield return new WaitForSeconds ( time ) ;
596+ int largest = i ; // Initialize largest as root
597+ int l = 2 * i + 1 ; // left = 2*i + 1
598+ int r = 2 * i + 2 ; // right = 2*i + 2
599+
600+ // If left child is larger than root
601+ if ( l < N && arr [ l ] > arr [ largest ] )
602+ largest = l ;
603+
604+ // If right child is larger than largest so far
605+ if ( r < N && arr [ r ] > arr [ largest ] )
606+ largest = r ;
607+
608+ // If largest is not root
609+ if ( largest != i ) {
639610 pillars [ i ] . GetComponent < Pillar > ( ) . Color = tempColor ;
640- pillars [ i - 1 ] . GetComponent < Pillar > ( ) . Color = whiteColor ;
641-
642- // if child is bigger than parent
643- if ( arr [ i ] > arr [ ( i - 1 ) / 2 ] ) {
644- int j = i ;
645- pillars [ ( i - 1 ) / 2 ] . GetComponent < Pillar > ( ) . Color = nextColor ;
646- // swap child and parent until
647- // parent is smaller
648- while ( arr [ j ] > arr [ ( j - 1 ) / 2 ] ) {
649-
650- yield return new WaitForSeconds ( time ) ;
651- swap ( arr , j , ( j - 1 ) / 2 ) ;
652- j = ( j - 1 ) / 2 ;
653- }
654- }
611+
612+ int swap = arr [ i ] ;
613+ arr [ i ] = arr [ largest ] ;
614+ arr [ largest ] = swap ;
615+
616+ pillars [ largest ] . GetComponent < Pillar > ( ) . Color = nextColor ;
617+
618+ // Recursively heapify the affected sub-tree
619+ yield return heapify ( arr , N , largest ) ;
655620 }
656621 }
657622
0 commit comments