问题描述
说我有一个整数数组在Javascript中,我想检查是否所有的值按升序排列。我要的是保存在另外的情况下阵列的方向键的算法找到一个值是低于(或等于)不仅比较直接的previous之一,也是比较是之前任何价值。
我所做的是这样的:
Say I have an array of integers in Javascript, that I would like to check if all of its values are in ascending order. What i want is to save the array key in another array in case the algorithm finds a value that is lower (or equal) not only comparing the immediate previous one, but also comparing any value that is before it.What I did was this:
arr = [], nonvalid = [];
for (var j = 1; j < arr.length; j++){
if ( arr[j+1] <= arr[j] ){
nonvalid.push(j);
}
}
显然,上述algorightm只检查更低才比较值之一。
Obviously the above algorightm checks only for values that are lower comparing the one before it.
这是阵列可能包含这样的值:
An array might contain values like these:
ARR = 1,2,3,10, 5 ,11,12, 2 4 25
arr = 1, 2, 3, 10, 5, 11, 12, 2, 4, 25
非有效值是大胆的。如果我运行上面的循环,也不会赶第二个最后一个( 4 ),因为它比它的最接近左兄的,但不是说比高高其所有的左兄弟的
The non valid values are the bold ones. If I run the above loop, it won't "catch" the second last one (4), because it's higher than its closest left brother, but not that high than its all left brothers.
编辑:
尝试了以下解决方案,并没有返回所有nonvalid值这个数组,除了我的。 (
Tried the following solutions and none return all the nonvalid values for this array except mine . :(
他们correctedly返回的最后两个值,但不是第二个。
我不明白为什么虽然。
They returned the last two values correctedly, but not the second one.I don't understand why though.
[24398, 24397 ,25004,25177,26302,28036,29312,29635,29829,30476,32595,33732,34995,36047,36363,37310,38022,38882,40746,41212 ,42846,43588,44029,44595,44846,45727,46041,47293,48002,48930,49858,51184,51560,53895,54247,54614,55713,56813,57282,57480,57875,58073,58403,60321,61469 ,62051,62310,62634,63217,64505,65413,65677,65940,66203,66572,67957,68796,68964,69098,69233,69435,69759,71496,72577,72823,73007,73252,73743,73866,76405 ,77037,77416,77669,79691,80885,81339,81794,82067,82431,83244,84861,86836,88632,89877,90296,91049,91885,92351,92614,93141,93733,93930,94531,95206,95882 ,96895,97732,97973,99261,99422,99583,100332,100599,101666,102066,102600,103504,104432,105174,107216,109085,110181,110679,111177,111988,112553,113005,113457, 600 600 ]
[24398, 24397, 25004, 25177, 26302, 28036, 29312, 29635, 29829, 30476, 32595, 33732, 34995, 36047, 36363, 37310, 38022, 38882, 40746, 41212, 42846, 43588, 44029, 44595, 44846, 45727, 46041, 47293, 48002, 48930, 49858, 51184, 51560, 53895, 54247, 54614, 55713, 56813, 57282, 57480, 57875, 58073, 58403, 60321, 61469, 62051, 62310, 62634, 63217, 64505, 65413, 65677, 65940, 66203, 66572, 67957, 68796, 68964, 69098, 69233, 69435, 69759, 71496, 72577, 72823, 73007, 73252, 73743, 73866, 76405, 77037, 77416, 77669, 79691, 80885, 81339, 81794, 82067, 82431, 83244, 84861, 86836, 88632, 89877, 90296, 91049, 91885, 92351, 92614, 93141, 93733, 93930, 94531, 95206, 95882, 96895, 97732, 97973, 99261, 99422, 99583, 100332, 100599, 101666, 102066, 102600, 103504, 104432, 105174, 107216, 109085, 110181, 110679, 111177, 111988, 112553, 113005, 113457, 600, 600]
推荐答案
当你发现一个元素失灵,看下面的元素,直到他们不再出相对顺序的元素之前乱序之一。
When you find an element out of order, look at the next elements until they are no longer out of order relative to the element before the out of order one.
乱序元素从新加入到第二阵列,并继续以元素。
Add the out of order elements to the second array and continue from the new in order element.
var outs= [], L= A.length, i= 0, prev;
while(i<L){
prev= A[i];
while(A[++i]<prev) outs.push(i);
}
alert(outs)
这篇关于检查JavaScript数组值按升序排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!