简体   繁体   中英

How to implement binary Search process in Arraylist in Java

I have no idea how to implement binary Search process in Arraylist in Java.

There are two arraylist showing airport Names and routes between two airport Names.

Here are airportName and Route Class defined below.

public class AirportName{
    private String airportName;
}

public class Route{
    private String takeOffPoint;
    private String landingPoint;
}

Airport Names are defined an abbreviation of Airport Names like (JFK) in Arraylist Route names are defined a route object including take off point and landing point in Arraylist like (JFK - TLV)

Because there are a lot of airport Names and routes in each arraylist whose size is above 30000, I have to use binary search to implement optimization of code.

I've already done the process without the usage of binary search.

How can I do it via binary search?

Here is my code snippet shown below.

ArrayList<AirportName> airportNames = ShowProcess.getAirports();
ArrayList<Route> routeNamesList = ShowProcess.getAirportRoutes();

Airpot Names

Scanner sc = new Scanner(System.in);
    System.out.print("Enter AirportName : ");
    String airportName = sc.nextLine(); 

    boolean checkAirportNameValid = false;

    for(AirportName airport : airportNames) {
        if(airport.getAirportName().equals(airportName)) {
            checkAirportNameValid = true;
        }
    }

Route Names

public static ArrayList<String> searchProcess(String airportName, ArrayList<Route> routeNamesList) {
        ArrayList<String> destinationNames = new ArrayList<String>();
        for(Route route : routeNamesList) {
            if(route.getTakeOffPoint().equals(airportName)) {
                destinationNames.add(route.getLandingPoint());
            }
        }

        return destinationNames;
}

Here is my solution. It works

public static Integer[] binarySearch(ArrayList<Route> routeNamesList, Comparable key) {
        ArrayList<Integer> arrList = new ArrayList<Integer>();
        int lo = 0, hi = routeNamesList.size() - 1, mid;
        routeNamesList.sort((str1, str2) -> str1.getTakeOffPoint().compareTo(str2.getTakeOffPoint()));

        while (lo <= hi) {
            mid = lo + (hi - lo) / 2;
            int cmp = key.compareTo(routeNamesList.get(mid).getTakeOffPoint());
            if (cmp == 0) {
                int lowerBoundary = lowerIndex(routeNamesList, key, lo, mid);
                int upperBoundary = upperIndex(routeNamesList, key, mid, hi);
                for(int i = lowerBoundary; i <= upperBoundary; i++) {
                    arrList.add(i);
                }
                break;
            } else if (cmp < 0)
                hi = mid - 1;
            else
                lo = mid + 1;
        }
        return arrList.stream().toArray(Integer[]::new);
    }

    public static int lowerIndex(ArrayList<Route> routeNamesList, Comparable key, int lo, int hi) {
        int mid;
        int lastMatch = hi;
        while (lo <= hi) {
            mid = lo + (hi - lo) / 2;
            int cmp = key.compareTo(routeNamesList.get(mid).getTakeOffPoint());
            if (cmp == 0) {
                lastMatch = mid;
                hi = mid - 1;
            } else if (cmp < 0) {
                lo = mid + 1;
            } else {
                break;
            }
        }

        return lastMatch;
    }

    public static int upperIndex(ArrayList<Route> routeNamesList, Comparable key, int lo, int hi) {
        int mid;
        int lastMatch = lo;
        while (lo <= hi) {
            mid = lo + (hi - lo) / 2;
            int cmp = key.compareTo(routeNamesList.get(mid).getTakeOffPoint());
            if (cmp == 0) {
                lastMatch = mid;
                lo = mid + 1;
            } else if (cmp < 0) {
                hi = mid - 1;
            } else {
                break;
            }
        }

        return lastMatch;
    }

    public static ArrayList<String> searchProcess(String airportName, ArrayList<Route> routeNamesList) {
        ArrayList<String> destinationNames = new ArrayList<String>();

        Integer[] indices = binarySearch(routeNamesList, airportName);
        for (int i = 0; i < indices.length; i++) {
            if(routeNamesList.get(i).getTakeOffPoint().equals(airportName)) {
                destinationNames.add(routeNamesList.get(i).getLandingPoint());
            }
        }

        return destinationNames;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM