admin管理员组文章数量:1316974
I'm taking a Algorithm class Khan Academy for JavaScript. I wrote a code like this:
var insert = function(array, rightIndex, value) {
for(var i = rightIndex;
i > 0 && array[i-1] > value;
i--) {
array[i] = array[i-1];
}
array[i] = value;
};
var insertionSort = function(array) {
for (var st = 1; st < array.length; st++) {
insert(array, st, array[st]);
}
};
var array = [22, 11, 99, 88, 9, 7, 42];
insertionSort(array);
println("Array after sorting: " + array);
Program.assertEqual(array, [7, 9, 11, 22, 42, 88, 99]);
And now I wanna know what's wrong here, that I can't get into the next level ... Please help. :)
I'm taking a Algorithm class Khan Academy for JavaScript. I wrote a code like this:
var insert = function(array, rightIndex, value) {
for(var i = rightIndex;
i > 0 && array[i-1] > value;
i--) {
array[i] = array[i-1];
}
array[i] = value;
};
var insertionSort = function(array) {
for (var st = 1; st < array.length; st++) {
insert(array, st, array[st]);
}
};
var array = [22, 11, 99, 88, 9, 7, 42];
insertionSort(array);
println("Array after sorting: " + array);
Program.assertEqual(array, [7, 9, 11, 22, 42, 88, 99]);
And now I wanna know what's wrong here, that I can't get into the next level ... Please help. :)
Share Improve this question edited Oct 15, 2018 at 15:24 user124942 asked Aug 31, 2015 at 13:27 user124942user124942 1451 silver badge9 bronze badges 6- 1 You'll have to explain a little more. Seems a bit broad your question. – kockburn Commented Aug 31, 2015 at 13:30
- Hey, I wrote this code to Khan Academy challenge for implement Insertion sort. It looks something isn't right here. Why Khan Academy doesn't take this code for right? – user124942 Commented Aug 31, 2015 at 13:33
- I can't go on the next level. But for me I think nothing is missing to my code. – user124942 Commented Aug 31, 2015 at 13:33
- I have no idea what this Khan Challenge is but you're going to have to update your question and let people know what you want this code to do. Then you'll have to explain which parts seems to be causing a problem. So on and so forth. Debugging questions are not meant for SO btw. – kockburn Commented Aug 31, 2015 at 13:35
- Still don't know what's wrong here ... This insertionSort returns me a sorted array. – user124942 Commented Aug 31, 2015 at 14:05
5 Answers
Reset to default 11to all :) It's the right solution. You can't change what is already written.
var insert = function(array, rightIndex, value) {
for(var j = rightIndex;
j >= 0 && array[j] > value;
j--) {
array[j + 1] = array[j];
}
array[j + 1] = value;
};
var insertionSort = function(array) {
for (var st = 1; st < array.length; st++) {
insert(array, st - 1, array[st]);
}
};
var array = [22, 11, 99, 88, 9, 7, 42];
insertionSort(array);
println("Array after sorting: " + array);
Program.assertEqual(array, [7, 9, 11, 22, 42, 88, 99]);
The problem wasn't that you were giving a wrong answer, it's that you weren't giving the coding solution they were expecting.
On this particular problem, there's a "hint" section in the upper right hand corner. If you click on the What's this? link.
This hint shows the code you'll need to successfully plete this step, but it is not the plete answer. The empty blanks are parts that you will need to figure out yourself. If you see colored blanks, the values you put in two blanks of the same color must be exactly the same
In their hint, they were expecting the same value be used for the initial var, the for loop and the array. Example: substitute for foo.
var foo;
for(foo = -----; -----; ----){
array[foo + 1] = -----;
}
----;
The original poster already showed the Khan Academy solution (shown below). Which doesn't match their hint. shrug This code came from a later exercise which included the insert solution.
var insert = function(array, rightIndex, value) {
for(var j = rightIndex;
j >= 0 && array[j] > value;
j--) {
array[j + 1] = array[j];
}
array[j + 1] = value;
};
var insertionSort = function (unsortedList) {
var len = unsortedList.length;
for(var i = 0; i < len; i++) {
var tmp = unsortedList[i]; //Copy of the current element.
/*Check through the sorted part and pare with the
number in tmp. If large, shift the number*/
for(var j = i - 1; j >= 0 && (unsortedList[j] > tmp); j--) {
//Shift the number
unsortedList[j+1] = unsortedList[j];
}
//Insert the copied number at the correct position
//in sorted part.
unsortedList[j+1] = tmp;
}
};
var array = [22, 11, 99, 88, 9, 7, 42];
insertionSort(array);
println("Array after sorting: " + array);
Your program is right but may be syntax is wrong: use Console.log("Array after sorting: " + array); instead of: println("Array after sorting: " + array); in java Script there is no any method println for output.
this works...
var insert = function(array, rightIndex, value) {
for (var i = rightIndex; i>=0 && array[i] > value; i --) {
array[i+1] = array[i];
}
array[i+1] = value;
};
本文标签: javascriptKhan AcademyChallenge Implement insertion sortStack Overflow
版权声明:本文标题:javascript - Khan Academy - Challenge: Implement insertion sort - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742019222a2414351.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论