@@ -19,12 +19,10 @@ function startMergeSort() {
1919 checkIfAlreadyRunning ( ) ;
2020 copyArray = returnArray ( ) ;
2121 mergesort ( copyArray ) ;
22- control . setColorInArrayElements ( copyArray , 3 , true ) ;
2322}
2423function mergesort ( array ) {
2524 if ( array . length < 2 ) {
2625 pivotHelper ( array [ 0 ] ) ;
27- //control.deselectPivotElement(array[0]);
2826 return array ;
2927 }
3028 else {
@@ -38,30 +36,37 @@ function mergesort(array) {
3836 control . setPivotElement ( right [ 0 ] ) ;
3937 pivotElements [ pivCount ] = right [ 0 ] ;
4038 pivCount ++ ;
41- control . setColorInArrayElements ( left , 1 , true ) ;
42- control . setColorInArrayElements ( right , 2 , true ) ;
39+ control . setColorInArrayElements ( left , 1 ) ;
40+ control . setColorInArrayElements ( right , 2 ) ;
4341 control . lowerElements ( left ) ;
4442 control . lowerElements ( right ) ;
45- control . setColorInArrayElements ( array , 1 , false ) ;
43+ control . setColorInArrayElements ( left , 4 ) ;
44+ control . setColorInArrayElements ( right , 4 ) ;
4645 //Split until there is only 1 element left
4746 return merge ( mergesort ( left ) , mergesort ( right ) ) ;
4847 }
4948}
5049function merge ( left , right ) {
5150 var result = [ ] ;
5251 var testing = copyArray . slice ( 0 ) ;
52+ var workingNumbers = left . concat ( right ) ;
5353 control . setPivotElement ( right [ 0 ] ) ;
5454 pivotElements [ pivCount ] = right [ 0 ] ;
5555 pivCount ++ ;
5656 var tempLeftIndex = 0 ;
5757 var tempRightIndex = 0 ;
5858 var counter = copyArray . indexOf ( left [ 0 ] ) ;
59+ var leftAlreadyColored = false ;
5960 while ( tempLeftIndex < left . length && tempRightIndex < right . length ) {
6061 //Compare the elements from each array
61- control . setColorInArrayElement ( left [ tempLeftIndex ] , 0 , true ) ;
62- control . setColorInArrayElement ( right [ tempRightIndex ] , 0 , true ) ;
62+ if ( ! leftAlreadyColored ) {
63+ control . setColorInArrayElement ( left [ tempLeftIndex ] , 0 ) ;
64+ leftAlreadyColored = true ;
65+ }
66+ control . setColorInArrayElement ( right [ tempRightIndex ] , 0 ) ;
6367 if ( left [ tempLeftIndex ] < right [ tempRightIndex ] ) {
64- control . setColorInArrayElement ( left [ tempLeftIndex ] , 3 , true ) ;
68+ leftAlreadyColored = false ;
69+ control . setColorInArrayElement ( left [ tempLeftIndex ] , 3 ) ;
6570 control . moveElementToPlace ( left [ tempLeftIndex ] , counter , copyArray . indexOf ( left [ tempLeftIndex ] ) ) ;
6671 result . push ( left [ tempLeftIndex ] ) ;
6772 testing [ counter ] = left [ tempLeftIndex ] ;
@@ -70,48 +75,47 @@ function merge(left, right) {
7075 }
7176 else {
7277 pivotHelper ( right [ 0 ] ) ;
73- control . setColorInArrayElement ( right [ tempRightIndex ] , 3 , true ) ;
78+ control . setColorInArrayElement ( right [ tempRightIndex ] , 3 ) ;
7479 control . moveElementToPlace ( right [ tempRightIndex ] , counter , copyArray . indexOf ( right [ tempRightIndex ] ) ) ;
7580 result . push ( right [ tempRightIndex ] ) ;
7681 testing [ counter ] = right [ tempRightIndex ] ;
7782 counter ++ ;
7883 tempRightIndex ++ ;
7984 }
8085 }
81- if ( right . slice ( tempRightIndex ) . length > 0 ) {
82- pivotHelper ( right [ 0 ] ) ;
83- var moreRight = right . slice ( tempRightIndex ) ;
84- control . setColorInArrayElements ( moreRight , 3 , true ) ;
86+ if ( left . slice ( tempLeftIndex ) . length > 0 ) {
87+ var moreLeft = left . slice ( tempLeftIndex ) ;
88+ control . setColorInArrayElements ( moreLeft , 3 ) ;
8589 var elems = [ ] ;
8690 var count = [ ] ;
8791 var back = [ ] ;
88- for ( var i = 0 ; i < moreRight . length ; i ++ ) {
89- elems [ i ] = moreRight [ i ] ;
92+ for ( var i = 0 ; i < moreLeft . length ; i ++ ) {
93+ elems [ i ] = moreLeft [ i ] ;
9094 count [ i ] = counter ;
91- back [ i ] = copyArray . indexOf ( moreRight [ i ] ) ;
92- testing [ counter ] = moreRight [ i ] ;
95+ back [ i ] = copyArray . indexOf ( moreLeft [ i ] ) ;
96+ testing [ counter ] = moreLeft [ i ] ;
9397 counter ++ ;
9498 }
9599 control . moveElementsToPlace ( elems , count , back ) ;
96100 }
97- if ( left . slice ( tempLeftIndex ) . length > 0 ) {
98- var moreLeft = left . slice ( tempLeftIndex ) ;
99- control . setColorInArrayElements ( moreLeft , 3 , true ) ;
101+ if ( right . slice ( tempRightIndex ) . length > 0 ) {
102+ pivotHelper ( right [ 0 ] ) ;
103+ var moreRight = right . slice ( tempRightIndex ) ;
104+ control . setColorInArrayElements ( moreRight , 3 ) ;
100105 var elems = [ ] ;
101106 var count = [ ] ;
102107 var back = [ ] ;
103- for ( var i = 0 ; i < moreLeft . length ; i ++ ) {
104- elems [ i ] = moreLeft [ i ] ;
108+ for ( var i = 0 ; i < moreRight . length ; i ++ ) {
109+ elems [ i ] = moreRight [ i ] ;
105110 count [ i ] = counter ;
106- back [ i ] = copyArray . indexOf ( moreLeft [ i ] ) ;
107- testing [ counter ] = moreLeft [ i ] ;
111+ back [ i ] = copyArray . indexOf ( moreRight [ i ] ) ;
112+ testing [ counter ] = moreRight [ i ] ;
108113 counter ++ ;
109114 }
110115 control . moveElementsToPlace ( elems , count , back ) ;
111116 }
112- pivotHelper ( right [ 0 ] ) ;
113117 copyArray = testing . slice ( 0 ) ;
114- control . setColorInArrayElements ( testing , 3 , false ) ;
118+ control . setColorInArrayElements ( workingNumbers , 4 ) ;
115119 return result . concat ( left . slice ( tempLeftIndex ) ) . concat ( right . slice ( tempRightIndex ) ) ;
116120}
117121function pivotHelper ( number ) {
0 commit comments