@@ -137,7 +137,7 @@ public void OnSortClick() {
137
137
case 6 :
138
138
sortButton . interactable = false ;
139
139
sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
140
- StartCoroutine ( HeapSort ( array , array . Length ) ) ;
140
+ StartCoroutine ( HeapSort ( array ) ) ;
141
141
break ;
142
142
case 7 :
143
143
sortButton . interactable = false ;
@@ -459,29 +459,6 @@ IEnumerator ShellSort(int[] arr) {
459
459
}
460
460
}
461
461
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
-
485
462
IEnumerator QuickSort ( int [ ] a , int low , int high ) {
486
463
List < GameObject > pillars = new List < GameObject > ( ) ;
487
464
foreach ( Transform tran in GameObject . Find ( "Bar" ) . transform ) {
@@ -564,61 +541,43 @@ IEnumerator QuickSort(int[] a, int low, int high) {
564
541
}
565
542
}
566
543
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 ) {
574
545
List < GameObject > pillars = new List < GameObject > ( ) ;
575
546
foreach ( Transform tran in GameObject . Find ( "Bar" ) . transform ) {
576
547
pillars . Add ( tran . gameObject ) ;
577
548
}
578
549
579
- yield return StartCoroutine ( buildMaxHeap ( arr , n ) ) ;
550
+ int N = arr . Length ;
580
551
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 ;
583
560
}
584
561
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 -- ) {
588
564
yield return new WaitForSeconds ( time ) ;
589
- // swap value of first indexed
590
- // with last indexed
591
-
592
- swap ( arr , 0 , i ) ;
593
565
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 ;
597
570
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 ;
618
576
}
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 ) ;
622
581
}
623
582
624
583
if ( IsSorted ( this . array ) ) {
@@ -628,30 +587,36 @@ IEnumerator HeapSort(int[] arr, int n) {
628
587
}
629
588
}
630
589
631
- IEnumerator buildMaxHeap ( int [ ] arr , int n ) {
590
+ IEnumerator heapify ( int [ ] arr , int N , int i ) {
632
591
List < GameObject > pillars = new List < GameObject > ( ) ;
633
592
foreach ( Transform tran in GameObject . Find ( "Bar" ) . transform ) {
634
593
pillars . Add ( tran . gameObject ) ;
635
594
}
636
595
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 ) {
639
610
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 ) ;
655
620
}
656
621
}
657
622
0 commit comments