@@ -103,38 +103,47 @@ public void OnSortClick() {
103103 timer . currentTime = 0 ;
104104 running = true ;
105105
106- /*string sliderText = sliderTextUI.text;
107- List<char> charsToRemove = new List<char>() { 'x', '%'};
108- float sliderValue = (float)Convert.ToDouble(String.Concat(sliderText.Split(charsToRemove.ToArray())));*/
109-
110- if ( dropdown . SortSelector ( ) == 0 ) {
111- sortButton . interactable = false ;
112- sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
113- StartCoroutine ( BubbleSort ( this . array ) ) ;
114- } else if ( dropdown . SortSelector ( ) == 1 ) {
115- sortButton . interactable = false ;
116- sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
117- StartCoroutine ( SelectionSort ( this . array ) ) ;
118- } else if ( dropdown . SortSelector ( ) == 2 ) {
119- sortButton . interactable = false ;
120- sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
121- StartCoroutine ( InsertionSort ( this . array ) ) ;
122- } else if ( dropdown . SortSelector ( ) == 3 ) {
123- sortButton . interactable = false ;
124- sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
125- StartCoroutine ( BottomUpMergeSort ( this . array ) ) ;
126- } else if ( dropdown . SortSelector ( ) == 4 ) {
127- sortButton . interactable = false ;
128- sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
129- StartCoroutine ( ShellSort ( this . array ) ) ;
130- } else if ( dropdown . SortSelector ( ) == 5 ) {
131- sortButton . interactable = false ;
132- sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
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 ) ) ;
106+ switch ( dropdown . SortSelector ( ) ) {
107+ case 0 :
108+ sortButton . interactable = false ;
109+ sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
110+ StartCoroutine ( BubbleSort ( array ) ) ;
111+ break ;
112+ case 1 :
113+ sortButton . interactable = false ;
114+ sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
115+ StartCoroutine ( SelectionSort ( array ) ) ;
116+ break ;
117+ case 2 :
118+ sortButton . interactable = false ;
119+ sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
120+ StartCoroutine ( InsertionSort ( array ) ) ;
121+ break ;
122+ case 3 :
123+ sortButton . interactable = false ;
124+ sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
125+ StartCoroutine ( BottomUpMergeSort ( array ) ) ;
126+ break ;
127+ case 4 :
128+ sortButton . interactable = false ;
129+ sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
130+ StartCoroutine ( ShellSort ( array ) ) ;
131+ break ;
132+ case 5 :
133+ sortButton . interactable = false ;
134+ sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
135+ StartCoroutine ( QuickSort ( array , 0 , array . Length - 1 ) ) ;
136+ break ;
137+ case 6 :
138+ sortButton . interactable = false ;
139+ sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
140+ StartCoroutine ( HeapSort ( array , array . Length ) ) ;
141+ break ;
142+ case 7 :
143+ sortButton . interactable = false ;
144+ sortButton . GetComponentInChildren < Text > ( ) . text = "Sorting..." ;
145+ StartCoroutine ( RadixSort ( array ) ) ;
146+ break ;
138147 }
139148 }
140149
@@ -569,11 +578,17 @@ IEnumerator HeapSort(int[] arr, int n) {
569578
570579 yield return StartCoroutine ( buildMaxHeap ( arr , n ) ) ;
571580
581+ for ( int i = 0 ; i < arr . Length ; i ++ ) {
582+ pillars [ i ] . GetComponent < Pillar > ( ) . Color = whiteColor ;
583+ }
584+
572585 for ( int i = n - 1 ; i > 0 ; i -- ) {
573- pillars [ i ] . GetComponent < Pillar > ( ) . Color = checkColor ;
586+ pillars [ i ] . GetComponent < Pillar > ( ) . Color = swapColor ;
587+ pillars [ i - 1 ] . GetComponent < Pillar > ( ) . Color = nextColor ;
574588 yield return new WaitForSeconds ( time ) ;
575589 // swap value of first indexed
576590 // with last indexed
591+
577592 swap ( arr , 0 , i ) ;
578593
579594 // maintaining heap property
@@ -582,7 +597,7 @@ IEnumerator HeapSort(int[] arr, int n) {
582597
583598 do {
584599 index = ( 2 * j + 1 ) ;
585-
600+
586601 // if left child is smaller than
587602 // right child point index variable
588603 // to right child
@@ -597,6 +612,11 @@ IEnumerator HeapSort(int[] arr, int n) {
597612 swap ( arr , j , index ) ;
598613
599614 j = index ;
615+
616+ if ( index >= 0 && index < arr . Length ) {
617+ pillars [ index ] . GetComponent < Pillar > ( ) . Color = tempColor ;
618+ }
619+
600620 yield return new WaitForSeconds ( time ) ;
601621 } while ( index < i ) ;
602622 }
@@ -622,15 +642,74 @@ IEnumerator buildMaxHeap(int[] arr, int n) {
622642 // if child is bigger than parent
623643 if ( arr [ i ] > arr [ ( i - 1 ) / 2 ] ) {
624644 int j = i ;
625-
645+ pillars [ ( i - 1 ) / 2 ] . GetComponent < Pillar > ( ) . Color = nextColor ;
626646 // swap child and parent until
627647 // parent is smaller
628648 while ( arr [ j ] > arr [ ( j - 1 ) / 2 ] ) {
649+
629650 yield return new WaitForSeconds ( time ) ;
630651 swap ( arr , j , ( j - 1 ) / 2 ) ;
631652 j = ( j - 1 ) / 2 ;
632653 }
633654 }
634655 }
635656 }
657+
658+ IEnumerator RadixSort ( int [ ] Array ) {
659+ int n = Array . Length ;
660+ int max = Array [ 0 ] ;
661+
662+ //find largest element in the Array
663+ for ( int i = 1 ; i < n ; i ++ ) {
664+ if ( max < Array [ i ] )
665+ max = Array [ i ] ;
666+ }
667+
668+ //Counting sort is performed based on place.
669+ //like ones place, tens place and so on.
670+ for ( int place = 1 ; max / place > 0 ; place *= 10 ) {
671+ yield return new WaitForSeconds ( time ) ;
672+ yield return StartCoroutine ( CountingSort ( Array , place ) ) ;
673+ }
674+
675+ if ( IsSorted ( this . array ) ) {
676+ running = false ;
677+ sortButton . GetComponentInChildren < Text > ( ) . text = "Sorted!" ;
678+ timer . timerText . color = new Color32 ( 27 , 255 , 0 , 255 ) ;
679+ }
680+ }
681+
682+ IEnumerator CountingSort ( int [ ] Array , int place ) {
683+ int n = Array . Length ;
684+ int [ ] output = new int [ n ] ;
685+
686+ //range of the number is 0-9 for each place considered.
687+ int [ ] freq = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ;
688+ //count number of occurrences in freq array
689+ for ( int i = 0 ; i < n ; i ++ ) {
690+ yield return new WaitForSeconds ( time ) ;
691+ freq [ ( Array [ i ] / place ) % 10 ] ++ ;
692+ }
693+
694+ //Change count[i] so that count[i] now contains actual
695+ //position of this digit in output[]
696+ for ( int i = 1 ; i < 10 ; i ++ ) {
697+ yield return new WaitForSeconds ( time ) ;
698+ freq [ i ] += freq [ i - 1 ] ;
699+ }
700+
701+ //Build the output array
702+ for ( int i = n - 1 ; i >= 0 ; i -- ) {
703+ yield return new WaitForSeconds ( time ) ;
704+ output [ freq [ ( Array [ i ] / place ) % 10 ] - 1 ] = Array [ i ] ;
705+ freq [ ( Array [ i ] / place ) % 10 ] -- ;
706+ }
707+
708+ //Copy the output array to the input Array, Now the Array will
709+ //contain sorted array based on digit at specified place
710+ for ( int i = 0 ; i < n ; i ++ ) {
711+ yield return new WaitForSeconds ( time ) ;
712+ Array [ i ] = output [ i ] ;
713+ }
714+ }
636715}
0 commit comments