Dar*_*htA 1 javascript sorting
现场代码:http://jsfiddle.net/fCUZC/
//INPUT ARRAY:
var input = [28,32,21,11,8,2,14,32,64];
//VARIABLE DECLARATION. a = highest number so far, b = position of that number
entireLoop:
for (var i = 1; i<=input.length; i++)
{
if(input[i] > input[i-1])
{
for(var o = i; o>=0; o--)
{
if(input[i-1] > input[o])
{
input.splice(i,0,input[o]);
input.splice((o+1),1);
continue entireLoop;
}
else if(input[o] > input[0])
{
input.splice(0,0,input[o]);
input.splice((o+1),1);
continue entireLoop;
}
}
}
}
document.write(input);
Run Code Online (Sandbox Code Playgroud)
我正在尝试从最大到最小的顺序排序,但有一个32卡在某处.我知道有排序方法,但我是新手,想为自己尝试一下.
**编辑**首先看一下Array的原生.sort()方法.它使原始数组保持原样并接受比较函数.后者使.sort()非常强大.
var input = [28,32,21,11,8,2,14,32,64];
var low2high = function ( a , b ) {
return a > b;
};
var high2low = function ( a , b ) {
return a < b;
};
var resultHigh2low = input.sort( high2low ); // [ 64,32,32,28,21,14,11,8,2 ];
var resultLow2high = input.sort( low2high ); // [ 2,8,11,14,21,28,32,32,64 ];
Run Code Online (Sandbox Code Playgroud)
因此,如果我们想使用bubbleSort(由TJ Crowder提供的链接,请参阅OP注释),我们可以编写以下内容:
// javascript bubbleSort implementation
var bubbleSort = function ( list , comparison ) {
var swapped;
var i;
var val;
list = [].concat( list ); // do not destroy original
comparison = ( typeof comparison == "function" ) ? comparison : function(a,b){return a > b;}
do {
i = list.length;
while ( --i ) {
if ( i && comparison( list[ i ] , comparison[ i-1] ) ) {
val = list[ i ];
list[ i ] = list[ i - 1 ];
list[ i - 1] = val;
swapped = true;
}
}
} while ( swapped );
return list;
}
// using comparison functions from previous example.
var resultHigh2low = bubbleSort( input , high2low ); // [ 64,32,32,28,21,14,11,8,2 ];
var resultLow2high = bubbleSort( input , low2high ); // [ 2,8,11,14,21,28,32,32,64 ];
Run Code Online (Sandbox Code Playgroud)
让我们一步一步地走过去:
var bubbleSort = function ( list , comparison ) {
..code..
}
Run Code Online (Sandbox Code Playgroud)
我们的函数接受2个参数,第一个是数组,第二个是可选的比较函数.
var swapped;
var i = list.length;
var val;
Run Code Online (Sandbox Code Playgroud)
我们将列表的长度存储在变量下i,并声明2个空变量(swapped和val)我们稍后将使用它们.
list = [].concat( list ); // do not destroy original
Run Code Online (Sandbox Code Playgroud)
我们使用克隆列表[].concat( array )并覆盖list原始变量,保留原始原样.
comparison = ( typeof comparison == "function" ) ? comparison : function(a,b){return a > b;}
Run Code Online (Sandbox Code Playgroud)
我们测试typeof的comparison说法,如果它是一个function我们使用一个,否则我们依傍我们自己的comparison功能.true如果a大于,我们的回退比较函数将返回b.
do {
..code..
} while ( swapped );
Run Code Online (Sandbox Code Playgroud)
do/while循环至少运行一次,我们的swapped变量当前undefined是因为它将被解释为falsy.如果我们的comparison函数返回true,则发生交换并且swapped变量将设置为true,因此它将再次循环.
while ( --i ) {
..code..
}
Run Code Online (Sandbox Code Playgroud)
在这里,我从列表的长度向下循环,--操作符放在i变量之前,以确保它在任何事情之前首先处理,i--在while评估之后会因为list[ list.length ]不存在而导致错误的结果.我总是这样做(也许是坏习惯),但如果它让你困惑,那就去绝对透明.
if ( i && comparison( list[ i ] , comparison[ i-1] ) ) {
..code..
}
Run Code Online (Sandbox Code Playgroud)
首先,我们检查是否i有一个truthy值(0评估为falsy)然后我们运行comparison函数传递list[ i ]和list[ i - 1 ]as a和b参数.如果comparison函数返回true,我们执行交换.
val = list[ i ];
list[ i ] = list[ i - 1 ];
list[ i - 1] = val;
swapped = true;
Run Code Online (Sandbox Code Playgroud)
在这里我不使用.splice()方法执行交换,它只是一个有根据的猜测,但我认为直接赋值比函数调用快.我使用val变量作为占位符.交换完成后,我设置swapped为true,所以我们的do/while循环将继续.
return list;
Run Code Online (Sandbox Code Playgroud)
好吧......返回结果.
我已经排除了一些检查,比如当列表的长度为0时我们会做什么等等.基本上在编写辅助函数时,我们还需要处理错误处理.例如,当传递的比较参数不是函数时抛出TypeError,确保比较方法返回布尔值,依此类推.
| 归档时间: |
|
| 查看次数: |
106 次 |
| 最近记录: |