@@ -12,6 +12,8 @@ public class ArrayController : MonoBehaviour {
1212 public Text sliderTextUI ;
1313
1414 public bool running ;
15+ private float sliderValue ;
16+ private float time ;
1517
1618 [ Header ( "Colors" ) ]
1719 public Material tempColor ;
@@ -31,6 +33,13 @@ private void Awake() {
3133 timer = GameObject . Find ( "TimerManager" ) . GetComponent < Timer > ( ) ;
3234 }
3335
36+ private void Update ( ) {
37+ string sliderText = sliderTextUI . text ;
38+ List < char > charsToRemove = new List < char > ( ) { 'x' , '%' } ;
39+ sliderValue = ( float ) Convert . ToDouble ( String . Concat ( sliderText . Split ( charsToRemove . ToArray ( ) ) ) ) ;
40+ time = 0.01f / sliderValue ;
41+ }
42+
3443 public void GenerateArray ( ) {
3544 timer . timerText . color = new Color32 ( 255 , 255 , 255 , 255 ) ;
3645 timer . currentTime = 0 ;
@@ -95,36 +104,37 @@ public void OnSortClick() {
95104 running = true ;
96105
97106 /*string sliderText = sliderTextUI.text;
98- sliderText = sliderText.Replace("x", string.Empty);
99- float sliderValue = (float)Convert.ToDouble(sliderText);*/
100- string sliderText = sliderTextUI . text ;
101107 List<char> charsToRemove = new List<char>() { 'x', '%'};
102- float sliderValue = ( float ) Convert . ToDouble ( String . Concat ( sliderText . Split ( charsToRemove . ToArray ( ) ) ) ) ;
108+ float sliderValue = (float)Convert.ToDouble(String.Concat(sliderText.Split(charsToRemove.ToArray())));*/
103109
104110 if ( dropdown . SortSelector ( ) == 0 ) {
105111 sortButton . interactable = false ;
106112 sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
107- StartCoroutine ( BubbleSort ( this . array , 0.01f / sliderValue ) ) ;
113+ StartCoroutine ( BubbleSort ( this . array ) ) ;
108114 } else if ( dropdown . SortSelector ( ) == 1 ) {
109115 sortButton . interactable = false ;
110116 sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
111- StartCoroutine ( SelectionSort ( this . array , 0.01f / sliderValue ) ) ;
117+ StartCoroutine ( SelectionSort ( this . array ) ) ;
112118 } else if ( dropdown . SortSelector ( ) == 2 ) {
113119 sortButton . interactable = false ;
114120 sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
115- StartCoroutine ( InsertionSort ( this . array , 0.01f / sliderValue ) ) ;
121+ StartCoroutine ( InsertionSort ( this . array ) ) ;
116122 } else if ( dropdown . SortSelector ( ) == 3 ) {
117123 sortButton . interactable = false ;
118124 sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
119- StartCoroutine ( BottomUpMergeSort ( this . array , 0.01f / sliderValue ) ) ;
125+ StartCoroutine ( BottomUpMergeSort ( this . array ) ) ;
120126 } else if ( dropdown . SortSelector ( ) == 4 ) {
121127 sortButton . interactable = false ;
122128 sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
123- StartCoroutine ( ShellSort ( this . array , 0.01f / sliderValue ) ) ;
129+ StartCoroutine ( ShellSort ( this . array ) ) ;
124130 } else if ( dropdown . SortSelector ( ) == 5 ) {
125131 sortButton . interactable = false ;
126132 sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
127- StartCoroutine ( QuickSort ( this . array , 0 , array . Length - 1 , 0.01f / sliderValue ) ) ;
133+ StartCoroutine ( QuickSort ( this . array , 0 , array . Length - 1 ) ) ;
134+ } else if ( dropdown . SortSelector ( ) == 6 ) {
135+ sortButton . interactable = false ;
136+ sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
137+ StartCoroutine ( HeapSort ( this . array , array . Length ) ) ;
128138 }
129139 }
130140
@@ -135,7 +145,7 @@ void DestroyWithTag(string destroyTag) {
135145 Destroy ( oneObject ) ;
136146 }
137147
138- IEnumerator BubbleSort ( int [ ] arr , float time ) {
148+ IEnumerator BubbleSort ( int [ ] arr ) {
139149 List < GameObject > pillars = new List < GameObject > ( ) ;
140150 foreach ( Transform tran in GameObject . Find ( "Bar" ) . transform ) {
141151 pillars . Add ( tran . gameObject ) ;
@@ -182,7 +192,7 @@ IEnumerator BubbleSort(int[] arr, float time) {
182192
183193 }
184194
185- IEnumerator SelectionSort ( int [ ] arr , float time ) {
195+ IEnumerator SelectionSort ( int [ ] arr ) {
186196 List < GameObject > pillars = new List < GameObject > ( ) ;
187197 foreach ( Transform tran in GameObject . Find ( "Bar" ) . transform ) {
188198 pillars . Add ( tran . gameObject ) ;
@@ -232,7 +242,7 @@ IEnumerator SelectionSort(int[] arr, float time) {
232242
233243 }
234244
235- IEnumerator InsertionSort ( int [ ] arr , float time ) {
245+ IEnumerator InsertionSort ( int [ ] arr ) {
236246 List < GameObject > pillars = new List < GameObject > ( ) ;
237247 foreach ( Transform tran in GameObject . Find ( "Bar" ) . transform ) {
238248 pillars . Add ( tran . gameObject ) ;
@@ -278,9 +288,7 @@ IEnumerator InsertionSort(int[] arr, float time) {
278288
279289 }
280290
281- IEnumerator Wait ( float time ) { yield return new WaitForSeconds ( time ) ; }
282-
283- IEnumerator BottomUpMergeSort ( int [ ] a , float time ) {
291+ IEnumerator BottomUpMergeSort ( int [ ] a ) {
284292 List < GameObject > pillars = new List < GameObject > ( ) ;
285293 foreach ( Transform tran in GameObject . Find ( "Bar" ) . transform ) {
286294 pillars . Add ( tran . gameObject ) ;
@@ -292,6 +300,7 @@ IEnumerator BottomUpMergeSort(int[] a, float time) {
292300 for ( int i = 0 ; i < pillars . Count ; i ++ ) {
293301 pillars [ i ] . GetComponent < Pillar > ( ) . Color = whiteColor ;
294302 }
303+
295304 for ( int eachRunStart = 0 ; eachRunStart < a . Length ; eachRunStart = eachRunStart + 2 * runWidth ) {
296305 yield return new WaitForSeconds ( time ) ;
297306 int start = eachRunStart ;
@@ -310,18 +319,28 @@ IEnumerator BottomUpMergeSort(int[] a, float time) {
310319
311320 pillars [ mid ] . GetComponent < Pillar > ( ) . Color = tempColor ;
312321
313- this . Merge ( a , start , mid , end , temp , time ) ;
322+ this . Merge ( a , start , mid , end , temp ) ;
323+
314324 }
315325
316326 for ( int i = 0 ; i < a . Length ; i ++ ) {
317327 yield return new WaitForSeconds ( time ) ;
318328 a [ i ] = temp [ i ] ;
319329
320- if ( pillars [ i ] . GetComponent < Pillar > ( ) . Color == tempColor || pillars [ i ] . GetComponent < Pillar > ( ) . Color == nextColor ) {
321- for ( int j = 0 ; j < i ; j ++ ) {
322- pillars [ j ] . GetComponent < Pillar > ( ) . Color = whiteColor ;
330+ if ( runWidth > 1 ) {
331+ if ( pillars [ i ] . GetComponent < Pillar > ( ) . Color == nextColor ) {
332+ for ( int j = 0 ; j < i ; j ++ ) {
333+ pillars [ j ] . GetComponent < Pillar > ( ) . Color = whiteColor ;
334+ }
335+ }
336+ } else {
337+ if ( pillars [ i ] . GetComponent < Pillar > ( ) . Color == tempColor || pillars [ i ] . GetComponent < Pillar > ( ) . Color == nextColor ) {
338+ for ( int j = 0 ; j < i ; j ++ ) {
339+ pillars [ j ] . GetComponent < Pillar > ( ) . Color = whiteColor ;
340+ }
323341 }
324342 }
343+
325344 pillars [ i ] . GetComponent < Pillar > ( ) . Color = swapColor ;
326345 }
327346 }
@@ -333,7 +352,7 @@ IEnumerator BottomUpMergeSort(int[] a, float time) {
333352 }
334353 }
335354
336- void Merge ( int [ ] a , int start , int mid , int end , int [ ] temp , float time ) {
355+ void Merge ( int [ ] a , int start , int mid , int end , int [ ] temp ) {
337356 List < GameObject > pillars = new List < GameObject > ( ) ;
338357 foreach ( Transform tran in GameObject . Find ( "Bar" ) . transform ) {
339358 pillars . Add ( tran . gameObject ) ;
@@ -376,7 +395,7 @@ void Merge(int[] a, int start, int mid, int end, int[] temp, float time) {
376395 Assert . IsTrue ( j == end + 1 ) ;
377396 }
378397
379- IEnumerator ShellSort ( int [ ] arr , float time ) {
398+ IEnumerator ShellSort ( int [ ] arr ) {
380399 List < GameObject > pillars = new List < GameObject > ( ) ;
381400 foreach ( Transform tran in GameObject . Find ( "Bar" ) . transform ) {
382401 pillars . Add ( tran . gameObject ) ;
@@ -431,7 +450,7 @@ IEnumerator ShellSort(int[] arr, float time) {
431450 }
432451 }
433452
434- int partition ( int [ ] a , int low , int high , float time ) {
453+ int partition ( int [ ] a , int low , int high ) {
435454 int pivot = a [ high ] ;
436455 int i = low - 1 ;
437456 int temp ;
@@ -454,7 +473,7 @@ int partition(int[] a, int low, int high, float time) {
454473 return i + 1 ;
455474 }
456475
457- IEnumerator QuickSort ( int [ ] a , int low , int high , float time ) {
476+ IEnumerator QuickSort ( int [ ] a , int low , int high ) {
458477 List < GameObject > pillars = new List < GameObject > ( ) ;
459478 foreach ( Transform tran in GameObject . Find ( "Bar" ) . transform ) {
460479 pillars . Add ( tran . gameObject ) ;
@@ -535,4 +554,83 @@ IEnumerator QuickSort(int[] a, int low, int high, float time) {
535554 timer . timerText . color = new Color32 ( 27 , 255 , 0 , 255 ) ;
536555 }
537556 }
557+
558+ public void swap ( int [ ] a , int i , int j ) {
559+ int temp = a [ i ] ;
560+ a [ i ] = a [ j ] ;
561+ a [ j ] = temp ;
562+ }
563+
564+ IEnumerator HeapSort ( int [ ] arr , int n ) {
565+ List < GameObject > pillars = new List < GameObject > ( ) ;
566+ foreach ( Transform tran in GameObject . Find ( "Bar" ) . transform ) {
567+ pillars . Add ( tran . gameObject ) ;
568+ }
569+
570+ yield return StartCoroutine ( buildMaxHeap ( arr , n ) ) ;
571+
572+ for ( int i = n - 1 ; i > 0 ; i -- ) {
573+ pillars [ i ] . GetComponent < Pillar > ( ) . Color = checkColor ;
574+ yield return new WaitForSeconds ( time ) ;
575+ // swap value of first indexed
576+ // with last indexed
577+ swap ( arr , 0 , i ) ;
578+
579+ // maintaining heap property
580+ // after each swapping
581+ int j = 0 , index ;
582+
583+ do {
584+ index = ( 2 * j + 1 ) ;
585+
586+ // if left child is smaller than
587+ // right child point index variable
588+ // to right child
589+ if ( index < ( i - 1 ) && arr [ index ] <
590+ arr [ index + 1 ] )
591+ index ++ ;
592+
593+ // if parent is smaller than child
594+ // then swapping parent with child
595+ // having higher value
596+ if ( index < i && arr [ j ] < arr [ index ] )
597+ swap ( arr , j , index ) ;
598+
599+ j = index ;
600+ yield return new WaitForSeconds ( time ) ;
601+ } while ( index < i ) ;
602+ }
603+
604+ if ( IsSorted ( this . array ) ) {
605+ running = false ;
606+ sortButton . GetComponentInChildren < Text > ( ) . text = "Sorted!" ;
607+ timer . timerText . color = new Color32 ( 27 , 255 , 0 , 255 ) ;
608+ }
609+ }
610+
611+ IEnumerator buildMaxHeap ( int [ ] arr , int n ) {
612+ List < GameObject > pillars = new List < GameObject > ( ) ;
613+ foreach ( Transform tran in GameObject . Find ( "Bar" ) . transform ) {
614+ pillars . Add ( tran . gameObject ) ;
615+ }
616+
617+ for ( int i = 1 ; i < n ; i ++ ) {
618+ yield return new WaitForSeconds ( time ) ;
619+ pillars [ i ] . GetComponent < Pillar > ( ) . Color = tempColor ;
620+ pillars [ i - 1 ] . GetComponent < Pillar > ( ) . Color = whiteColor ;
621+
622+ // if child is bigger than parent
623+ if ( arr [ i ] > arr [ ( i - 1 ) / 2 ] ) {
624+ int j = i ;
625+
626+ // swap child and parent until
627+ // parent is smaller
628+ while ( arr [ j ] > arr [ ( j - 1 ) / 2 ] ) {
629+ yield return new WaitForSeconds ( time ) ;
630+ swap ( arr , j , ( j - 1 ) / 2 ) ;
631+ j = ( j - 1 ) / 2 ;
632+ }
633+ }
634+ }
635+ }
538636}
0 commit comments