arrays - Shell Sort don't working on JavaScript -
i have code in java , works perfect, code translated javascript throw error. shellsort method pass array method.(i using console debug) code is:
this.shellsort = function(nums) { //o contador de tempo inicia aqui var temposs = new date(); var n = nums.length; console.log("n = ",n); var h = n / 2; console.log("h = ",h); var c, j; while (h > 0) { (var = h; < n; i++) { c = nums[i]; j = i; while (j >= h && nums[j - h] > c) { nums[j] = nums[j - h]; console.log("nums["+j+"] = ",nums[j]); j = j - h; console.log("j = ",j); } nums[j] = c; console.log("nums["+j+"] = ",nums[j]); } h = h / 2; console.log("h = ",h); }
the error caught -- [13:52:59.581] referenceerror: reference undefined property nums[(j - h)] @ file:///c:/documents%20and%20settings/erickribeiro/desktop/www/index.html:240
test page: dev.erickribeiro.com.br/index.html
the total script html page. wrong?
i believe you're getting stuck in endless loop because h
may float sometimes. have ensure it's integer (which in java always, since declared such):
this.shellsort = function(nums) { //o contador de tempo inicia aqui var temposs = new date(); var n = nums.length; console.log("n = ",n); // here: var h = math.floor(n / 2); console.log("h = ",h); var c, j; while (h > 0) { (var = h; < n; i++) { c = nums[i]; j = i; while (j >= h && nums[j - h] > c) { nums[j] = nums[j - h]; console.log("nums["+j+"] = ",nums[j]); j = j - h; console.log("j = ",j); } nums[j] = c; console.log("nums["+j+"] = ",nums[j]); } // , here: h = math.floor(h / 2); console.log("h = ",h); } }
javascript arrays sorting shellsort
No comments:
Post a Comment