选择排序算法不工作...警告...我是一个新手 [英] Selection Sort algorithm not working...warning...I'm a newbie
本文介绍了选择排序算法不工作...警告...我是一个新手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的算法是行不通的,我是一个新手,对所有的算法和JavaScript,我只是似乎无法工作了,为什么它不工作。
My algorithm is not working, I'm a newbie to all algorithms and javascript and I just can't seem to work out why it's not working.
这里的阵列(23,19,35,12,30)
最大值= 0
下面是我有...
For K= 0 to n – 2
MAX = 0
For j = 0 to n – k - 2
If item[j] > item[MAX]
MAX = item[j]
end if
end for
swap n - k - 2 with item[MAX]
end for
end SelectionSort
我在哪里出了错?谢谢!!!!
Where have I gone wrong? thanks!!!!
此外,如果有人能告诉我如何使用它在跟踪表,我会SOOO快乐!
Also, if anyone could tell me how to use it in a trace table I would be sooo happy!
亲切的问候 克雷格
推荐答案
在此EX pression使用的最大原因造成的问题项[MAX],这将是学家请给它一个考虑,然后再试一次像这样,选择排序演示
Using max in this expression is causing the problem item[MAX], It would be j. Please give it a consideration and then try again like this, The selection sort Demo
var arr = new Array (23, 19, 35, 12, 30);
temp = 0;
for( k = 0; k < arr.length-1; k++)
{
for( j = k+1; j < arr.length; j++)
{
if (arr [k] > arr [j])
{
temp = arr[k];
arr[k] = arr[j];
arr[j] = temp;
}
}
}
for(k = 0; k < arr.length; k++)
alert(arr[k]);
这篇关于选择排序算法不工作...警告...我是一个新手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文