1

I am getting an error of "FATAL ERROR: CALL_AND_RETRY_0 Allocation failed - process out of memory" on my Javascript code. How could I run this code? I see no flaws in the code, because I was doing exactly the same thing as in Python and it works in Python, but here in Javascript I am getting memory errors. Below is my code.

   var sample_arr = [-1, 5, 7, 4, 0, 1, -5]
   function My_Partition(container, first_index, last_index) {
       var x = container[last_index];
       var i = first_index - 1;

       for (var elem = 0; elem < container.length-1; elem++) {
               if (container[elem] <= x) {
               i += 1;
               var temp_1 = container[i];
               container[i] = container[elem];
               container[elem] = temp_1;
           }
       }
       var temp_2 = container[i+1];
       container[i+1] = container[last_index];
       container[last_index] = temp_2;

       return i+1;
   }


   function My_Quick_Sort(container, first_index, last_index) {
       if (first_index < last_index) {
           var mid = My_Partition(container, first_index, last_index);
           My_Quick_Sort(container, first_index, mid-1);
           My_Quick_Sort(container, mid+1, last_index);
       }
   }
   My_Quick_Sort(sample_arr, 0, sample_arr.length-1);
   console.log("Sorted Array:", sample_arr);

Basically I am trying to implement a sorting algorithm, I would greatly appreciate your help.

2
  • What are you trying to achieve by this sort? Commented Sep 6, 2013 at 0:15
  • I am trying to sort a jumbled array to get a sorted version Commented Sep 6, 2013 at 0:21

1 Answer 1

1

That's a pretty spectacular crash.

So, since this looks like a homework/exercise of some sort, rather than answering the question, let's answer the question behind the question: "How do I figure out how to fix problems like this?" The best way is usually to come up with a theory, and then find a way of testing it.

Theory 1: Stack overflow

Usually, if you run out of memory, one obvious guess is stack overflow. To try and test that, lets see if we can set a limit on the number of times the functions get called, and run it in JSFiddle:

   var sample_arr = [-1, 5, 7, 4, 0, 1, -5];
   var stop_running = 0;
   function My_Partition(container, first_index, last_index) {
       if (stop_running++ > 100) return;
       ...
   }


   function My_Quick_Sort(container, first_index, last_index) {
       if (stop_running++ > 100) return;
       ...
   }
   My_Quick_Sort(sample_arr, 0, sample_arr.length-1);
   console.log("Sorted Array:", sample_arr);

Oops! This still crashes. Guess it's not a stack overflow, since we limited the number of times we could call our two functions.

Theory 2: Loop

Let's try another theory. Perhaps what's happening is an infinite loop. Let's log the loop calls instead (click for JSFiddle).

Notice that it's stuck in the loop for over 100 iterations. This is way too many for a simple sort. At this point, you might want to add a console.log call and print out the local variables. Try console.log(elem), or console.log(container.length), or perhaps both.

Sign up to request clarification or add additional context in comments.

4 Comments

Oh thanks a lot. I am quite new to Javascript and this kind of error I've never seen. So Javascript cannot handle this sorting? I wonder if I can fix this in Javascript way, since the same code works in many other languages. What am I missing? Could you help? This is not a homework. I am just self-studying. Thanks a lot!
In Python you're using len() right? Try print() len() in Python, and try console.log() container.length in JavaScript. You're supposed to get the same number, right? Do you?
Console.log is your friend. Take a look at this version, and look at the console: jsfiddle.net/ry2XE/3
Oh so much easier to see what is going wrong. But still trying to figure out how to make it right. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.