Java List Sort searchSorted(List a, double v)

Here you can find the source of searchSorted(List a, double v)

Description

Search sorted list index

License

LGPL

Parameter

Parameter Description
a Sorted list
v value

Return

Index

Declaration

public static int searchSorted(List<Number> a, double v) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.List;

public class Main {
    /**//from  w w w.j av  a 2  s  .  c  o m
     * Search sorted list index
     *
     * @param a Sorted list
     * @param v value
     * @return Index
     */
    public static int searchSorted(List<Number> a, double v) {
        int idx = -1;
        int n = a.size();
        if (a.get(1).doubleValue() > a.get(0).doubleValue()) {
            if (v < a.get(0).doubleValue()) {
                return idx;
            }

            if (v > a.get(n - 1).doubleValue()) {
                return idx;
            }

            for (int i = 1; i < n; i++) {
                if (v < a.get(i).doubleValue()) {
                    idx = i - 1;
                    break;
                }
            }
        } else {
            if (v > a.get(0).doubleValue()) {
                return idx;
            }

            if (v < a.get(n - 1).doubleValue()) {
                return idx;
            }

            for (int i = 1; i < n; i++) {
                if (v > a.get(i).doubleValue()) {
                    idx = i - 1;
                    break;
                }
            }
        }

        return idx;
    }
}

Related

  1. mergeSortedLists(List dest, List[] src, boolean removeDuplicates)
  2. quickSort(Comparable[] list, int min, int max)
  3. removeSortByColumnName(List sortProperty, List ascending, String columnName)
  4. removeSortColumns(List selectColumns, List sorts, List ascending)
  5. removeSortColumns(List selectColumns, List sorts, List ascending)
  6. setToSortedList(final Set set, final Comparator comparator)
  7. sort( S list)
  8. sort(Collection list)
  9. Sort(Collection list)