javascript - Determine direction of change in array -


Looking at a sorted array, such as [1,2,3,4,5] and a variation of this array, where an item was moved to a different position (not sorted now). [1,2,5,3,4] or [2,3,4,1,5] to define the "direction" of this step as trustworthy Required -

( 5 moved first left in the first example, 1

At the moment I have a solution that runs from arrays (from left) and their values ​​are compared to index i - as they are not identical (to define anomalies point) So it compares prices on i + 1 if Old [i + 1] & gt; New [i + 1] was omitted, otherwise correct

  // pseudocode: function (old, now) {for (old I [i] 1] & gt; now [i + 1]? "Left": "right"}}}   / Pre> 

edit
Is there a more reliable (and potentially more efficient) solution to that problem? At this time in some situations does it give "false positives" Need First Array? Its always sorted and sequential A.

There really is no room for improvement in your solution -

The complexity of your current time o (n) , which is not really bad at all So you should not worry about this function and if your app is working very slowly, then you should look to improve the other fields.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -