Javascript Sorting. Allocation fail process out of memory error

吃可爱长大的小学妹 提交于 2019-12-11 07:52:53

问题


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.


回答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.



来源:https://stackoverflow.com/questions/18647813/javascript-sorting-allocation-fail-process-out-of-memory-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!