title: Search algorithm for the smallest index in a sequence of vectors matching predefined conditions
body: <p>Let us presume we have <strong>k</strong> sequences of fixed length <strong>p</strong>. Each sequence has double values in range <strong>0</strong> to <strong>1.0</strong>. For simplicity let us also assume that the sequences are just arrays; in the real implementation, they will be list.</p>
  
  <p>Now, the algorithm needs to find the smallest index the value of which represents a "major upset" in a given sequence. This upset can be a value of <strong>1.0</strong> or a value that goes over a certain threshold (e.g. <strong>0.2</strong>).  If, for example, moving from <strong>j-1</strong> to <strong>j</strong> the value increases over the threshold, then the index we seek would be <strong>j-1</strong>.</p>
  
  <p>The upset of 1.0 takes precedence over the threshold value; for instance, if we find an index matching the threshold, we should still check the sequence for containing <strong>1.0</strong>.</p>
  
  <p>Finally, the algorithm should produce the smallest index that resulted in the upset.
  I have quickly put together some code to test the concept and show you the sort of thing I am after.  What I am looking for is a possibly more efficient implementation as this algorithm is going to be executed pretty extensively.</p>
  
  <pre><code>List&lt;double[]&gt; nearCaptures = new ArrayList&lt;double[]&gt;();
  double threshold = 0.2;
  double majorUpset = 1.0;
  int[] indexes = new int[nearCaptures.size()];
  for (int i = 0; i &lt; nearCaptures.size(); i++) {
      int index = 0;
      double[] tempArray = nearCaptures.get(i);
      Arrays.sort(tempArray);
      int tempIndex = Arrays.binarySearch(tempArray, majorUpset);
      if (tempIndex &gt; 0) {
          for (int j = 1; j &lt; nearCaptures.get(0).length; j++) {
              if (nearCaptures.get(i)[j] == majorUpset) {
                  index = j-1;
                  break;
              }
          }
      } else {
          for (int j = 1; j &lt; nearCaptures.get(0).length; j++) {
              if (nearCaptures.get(i)[j] &gt;= nearCaptures.get(i)[j-1] + threshold) {
                  index = j-1;
                  break;
              }
          }
      }
      indexes[i] = index;
  }
  Arrays.sort(indexes);
  System.out.println(indexes[0]);
  </code></pre>