# Unable to vizualize a sorted array while implementing mergesort

**URL:** <https://discourse.processing.org/t/unable-to-vizualize-a-sorted-array-while-implementing-mergesort/20122>\
**Category:** Coding Questions\
**Created:** [April 25, 2020, 4:12am UTC](https://discourse.processing.org/t/unable-to-vizualize-a-sorted-array-while-implementing-mergesort/20122 "2020-04-25T04:12:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suraaj](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@Suraaj](https://discourse.processing.org/u/Suraaj)\
**Post date:** [April 25, 2020, 4:12am UTC](https://discourse.processing.org/t/unable-to-vizualize-a-sorted-array-while-implementing-mergesort/20122/1 "2020-04-25T04:12:39Z")

</div>

I tried to implement a mergesort algorithm but the array isn’t getting sorted for some reason.The expected output is a sorted array of lines displayed on the browser window.

Here is my code

```
var values = []

function setup(){
   createCanvas(600,400);
   values.length = width;
   for(let i=0; i<values.length; i++){
	  values[i] = random(height);
   }
mergesort(values, 0, values.length-1)
}

function draw(){
   background(0);
   for (let i=0; i< values.length; i++){
	  stroke(255,0,255);
	  line(i, height, i, height - values[i]);
   }
 }
 function mergesort(arr, start, end){
    if (start < end){

      let middle = (start + (end-1))/2;
  
     merge(arr, start, middle, end);
     mergesort(arr, start, middle);
     mergesort(arr, middle +1, end);
     }
}

function merge(arr, start, middle, end){
   let sizeL = middle-start +1;
   let sizeR = end - middle;
   let arrL = [];
   let arrR = [];
   //var arr1 = [];
   for (let i = start; i < sizeL; i++) 
      arrL.push(arr[i])
   for (let j = start; j < sizeR ; j++) 
      arrR.push(arr[j])

   let a = start; 
   let b = start;
   let k = start + 1;
   while ( a< sizeL && b < sizeR){
 	  if (arrL[a] <= arrR[b]){
    	 arr[k] = arrL[a]; 
         a++; 
       } 
       else{
          arr[k] = arrR[b]; 
          b++; 
       } 
       k++; 
    } 

    while(a < sizeL){ 
       arr[k] = arrL[a]; 
       a++; 
       k++; 
   } 
   while(b < sizeR) { 
       arr[k] = arrR[b]; 
       b++; 
       k++; 
}
   //return arr1;

```

}

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [April 25, 2020, 6:20am UTC](https://discourse.processing.org/t/unable-to-vizualize-a-sorted-array-while-implementing-mergesort/20122/2 "2020-04-25T06:20:05Z")

</div>

Welcome to the forums! Whether or not this is hw, I encourage you to take a little more time to bug test. Problem solving in general is a really valuable skill.

Specifically, seeing that you have two functions, `merge()`, and `mergesort()`, try testing each function individually. Another good idea is to start with really small test-cases that you can trace by hand.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![Suraaj](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@Suraaj](https://discourse.processing.org/u/Suraaj)\
**Post date:** [April 25, 2020, 6:29am UTC](https://discourse.processing.org/t/unable-to-vizualize-a-sorted-array-while-implementing-mergesort/20122/3 "2020-04-25T06:29:16Z")

</div>

My familiarity with java script is pretty low. I have been trying to rectify my code for quite sometime now with little success. Initially I got a stack limit exceeded error and now this.  
I will try to test the two functions separately like you suggested.  
Thank you tony.

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [April 25, 2020, 6:49am UTC](https://discourse.processing.org/t/unable-to-vizualize-a-sorted-array-while-implementing-mergesort/20122/4 "2020-04-25T06:49:48Z")

</div>

If you’re more familiar with a strongly-typed language like Java or C++, Javascript definitely takes some getting used to. All i can really say is it takes practice (and a lot of debugging and a lot of stackoverflow 🤣)

---

<div class="post-metadata">

**Author:** ![Suraaj](https://avatars.discourse-cdn.com/v4/letter/s/2bfe46/32.png) [@Suraaj](https://discourse.processing.org/u/Suraaj)\
**Post date:** [April 26, 2020, 6:50am UTC](https://discourse.processing.org/t/unable-to-vizualize-a-sorted-array-while-implementing-mergesort/20122/5 "2020-04-26T06:50:50Z")

</div>

Hey guys, I found the solution to the problem.  
Here is the final working program:

> <https://github.com/SurajKumarM98/p5.js/blob/master/MergeSort.js>
