Home > Blockchain >  Sorting and Binary Search (Java)
Sorting and Binary Search (Java)

Time:11-08

I was asked to sort a car array by model type and then use Arrays.binarySearch to search for a Car with that Model field. The problem is when the search is conducted it doesn't find anything (even though the model is in there).

Below is my code and output:

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class CarApplication
{
 public static void main (String[] args)
 {
  Scanner keyboard = new Scanner(System.in);   
  Car car1 = new Car("Toyota", "Corolla" , 1996);
  Car car2 = new Car("Nissan", "Murano" , 2004);
  Car car3 = new Car("Mazda" , "Miata", 1999);
  Car car4 = new Car("Ford", "Mustang" , 2013);
  Car car5 = new Car("Chevy", "Volt" , 2020);
  Car car6 = new Car("Tesla", "Model X" , 2016);

  Car [] myCars = {car1, car2, car3, car4, car5, car6};

  Arrays.sort(myCars, new CompareByModel());  
  System.out.println("Sorting by Model only (Comparator)");
  for (Car car:myCars)
    System.out.println(car);  
    
  System.out.println("Enter the name of the car model you wish to purchase: ");
  String model = keyboard.nextLine(); 
  
//binary search

  Car key = new Car("", model, 0); // set the name field so we can look for a match in array
  int location = Arrays.binarySearch(myCars, 0, myCars.length, key, new CompareByModel());
  
//print message
   
  if (location < 0) 
      System.out.println("Sorry, please check back next week.");
   else
        {
     System.out.println("We have a "   model   " in location"   myCars[location]);
        }
  
  }
}

//Comparator

class CompareByModel implements Comparator<Car>
{
    public int compare(Car c1, Car c2) {
        int makeResult = c1.getCarMake().compareTo(c2.getCarMake());
        int modelResult = c1.getCarModel().compareTo(c2.getCarModel()); 
        
        return (modelResult == 0) ? makeResult: modelResult;
    }
    
}

Output: Enter the name of the car model you wish to purchase:

Volt

Sorry, please check back next week.

CodePudding user response:

When you sort, you sort by the make and model, which is cool. But when you try and search, you set the make to "", so the result of comparing the entities won't work correctly.

You could modify the sort Comparator so as to ignore makes with "" when comparing them (so only using models) or supply the make as part of your search or create a seperate "search" Comparator, for example

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        Scanner keyboard = new Scanner(System.in);
        Car car1 = new Car("Toyota", "Corolla", 1996);
        Car car2 = new Car("Nissan", "Murano", 2004);
        Car car3 = new Car("Mazda", "Miata", 1999);
        Car car4 = new Car("Ford", "Mustang", 2013);
        Car car5 = new Car("Chevy", "Volt", 2020);
        Car car6 = new Car("Tesla", "Model X", 2016);

        Car[] myCars = {car1, car2, car3, car4, car5, car6};

        Arrays.sort(myCars, new SortComparator());
        System.out.println(Arrays.toString(myCars));

        String model = "Murano";
        int result = Arrays.binarySearch(myCars, 0, myCars.length, new Car("", model, 0), new ModelComparator());
        System.out.println(result);

        int location = Arrays.binarySearch(myCars, 0, myCars.length, new Car("", model, 0), new SortComparator());
        System.out.println(location);

    }

    public class Car {

        private String make;
        private String model;
        private int year;

        public Car(String make, String model, int year) {
            this.make = make;
            this.model = model;
            this.year = year;
        }

        public String getMake() {
            return make;
        }

        public String getModel() {
            return model;
        }

        public int getYear() {
            return year;
        }

        @Override
        public String toString() {
            return getMake()   " "   getModel()   " @ "   getYear();
        }

    }

    public static class SortComparator implements Comparator<Car> {

        public int compare(Car c1, Car c2) {
            int makeResult = c1.getMake().compareTo(c2.getMake());
            int modelResult = c1.getModel().compareTo(c2.getModel());

            return (modelResult == 0) ? makeResult : modelResult;
        }

    }

    public static class ModelComparator implements Comparator<Car> {

        public int compare(Car c1, Car c2) {
            return c1.getModel().compareTo(c2.getModel());
        }

    }
}
  • Related