public static final void nbest(final float[] in,
                                 final int offset,
                                 final float[] codebook,
                                 final int len,
                                 final int entries,
                                 final float[] E,
                                 final int N,
                                 final int[] nbest,
                                 final float[] best_dist)
  {
    int i, j, k, l=0, used=0;
    for (i = 0; i < entries; i++)
    {
      float dist = .5f * E[i];
      for (j = 0; j < len; j++)
        dist -= in[offset + j] * codebook[l++];
      if (i < N || dist < best_dist[N - 1]) {
        for (k=N-1; (k >= 1) && (k > used || dist < best_dist[k-1]); k--) {
          best_dist[k] = best_dist[k - 1];
          nbest[k] = nbest[k - 1];
        }
        best_dist[k] = dist;
        nbest[k] = i;
        used++;
      }
    }
  }