0

I am trying to implement merge sort using a string, each string contains a number, want to sort based on that number. Here is the code that I have, Employee.java are my constructors, and merg is the main method and mergesort. It worked using an array of numbers but not an array of strings I get the following error:

reason: actual argument Employee[] cannot be converted to int[] by method invocation conversion
1 error


public class Employee
{
    private String name;
    private int idNumber;
    private String department;
    private String position;

    Employee(String n, int id, String dept, String pos)
    {
        name = n;
        idNumber = id;
        department = dept;
        position = pos;
    }
    Employee(int id)
    {
      idNumber = id;
    }
       public void setName(String n)
    {
        name = n;
    }
    public void setIdNumber(int id)
    {
        idNumber = id;
    }
    public void setDepartment(String dept)
    {
        department = dept;
    }
    public void setPosition(String pos)
    {
        position = pos;
    }
    public String getName()
    {
        return name;
    }
    public int getIdNumber()
    {
        return idNumber;
    }
 public String getDepartment()
    {
        return department;
    }
    public String getPosition()
    {
        return position;
    }
    public String toString()
    {
        String str = "|Employee name: " + name
            +"\n|Employee Identification: " + idNumber
            +"\n|Employee Department: " + department
            +"\n|Employee Postions: " + position;
        return str;
    }
}

merg file

public class merg
{
   public static void main(String[] args)
   {
    Employee e1 = new Employee("Edward" , 3342, "Finance", "Consultant");
    Employee e2 = new Employee("Howard", 4452, "Human Resources", "Manager");
    Employee e3 = new Employee("Chelsea", 3354, "IT", "System Admin");
    Employee e4 = new Employee("Kevin", 2298, "Physical Plant" , "Janitor"); 


    Employee arr[] = new Employee[4];
    arr[0]=e1;
    arr[1]=e2;
    arr[2]=e3;
    arr[3]=e4;
    Employee arr1[] = arr;
    System.out.println("Before Merge Sort: ");
    for(int i=0; i<arr.length;i++){
        System.out.println(arr[i].toString());
    }
    System.out.println("After Merge Sort: ");
    MergeSort(arr1);
    for(int i=0;i<arr1.length;i++){
        System.out.println(arr1[i].toString());
      }
   }
   public static void Merge(int[] L, int[] R, int[] A)
   {
      int nL = L.length;
      int nR = R.length;
      int i = 0;
      int j = 0;
      int k = 0;

      while(i < nL && j < nR)
      {
         if(L[i] <= R[j])
         {
            A[k] = L[i];
            k++;
            i++;
         }
         else
         {
            A[k] = R[j];
            k++;
            j++;
         }

      }
      while( i < nL)
      {
         A[k] = L[i];
         i++;
         k++;
      }
      while( j < nR)
      {
         A[k] = R[j];
         j++;
         k++;
      }
   }
   public static void MergeSort(int[] A)
    {
      int n = A.length;
      int i, j,mid;
      if(n < 2)
         return;

      mid = n / 2;
      int[] left = new int[mid];
      int[] right = new int[n - mid];

      for(i = 0; i < mid; i++)
         left[i] = A[i];
      for(i = 0; i< n-mid; i++)
         right[i] = A[i+mid];
      MergeSort(left);
      MergeSort(right);
      Merge(left, right, A);
    }
}
6
  • Your MergeSort method is expecting an int[] as the argument. You are passing an Employee[] Commented Apr 14, 2015 at 23:50
  • How do I use the int[] in my Employee constructor to sort that specific string? @VinceEmigh Commented Apr 14, 2015 at 23:51
  • Your Employee constructor doesn't have an int[]; which int[] are you referring to? Commented Apr 14, 2015 at 23:54
  • The In in my Employee Constructor I have Employee(String, int, String, String), I am trying to Sort it by using that int. Commented Apr 14, 2015 at 23:56
  • So you want to sort the Employee[] by the employees' id? Commented Apr 14, 2015 at 23:57

1 Answer 1

1

Firstly I should ask, are you required to use your own merge sort method? If not you should have your class implement implements Comparator<Employee> and

@Override
public int compare(Employee o1, Employee o2) {
    return Integer.compare(o1.getIdNumber(), o2.getIdNumber());
}

Or use

 Arrays.sort(arr, new Comparator<Employee>() {
    @Override
    public int compare(Employee o1, Employee o2) {
        Integer.compare(o1.getIdNumber(), o2.getIdNumber());
    }
});

which by the way Arrays.sort is a MergeSort

If you have to use your own merge sort you need to modify your merge sort method to take an Employee[] not int[] and use instances of Employee[] where you had int[] so:

public static void MergeSort(Employee[] A)
 {
   int n = A.length;
   int i, j,mid;
   if(n < 2)
      return;

   mid = n / 2;
   Employee[] left = new Employee[mid];
   Employee[] right = new Employee[n - mid];

   for(i = 0; i < mid; i++)
      left[i] = A[i];
   for(i = 0; i< n-mid; i++)
      right[i] = A[i+mid];
   MergeSort(left);
   MergeSort(right);
   Merge(left, right, A);
 }

Then Modify your Merge to also work with Employee[]

public static void Merge(Employee[] L, Employee[] R, Employee[] A)
{
   int nL = L.length;
   int nR = R.length;
   int i = 0;
   int j = 0;
   int k = 0;

   while(i < nL && j < nR)
   {
      if(L[i].getIdNumber() <= R[j].getIdNumber())
      {
         A[k] = L[i];
         k++;
         i++;
      }
      else
      {
         A[k] = R[j];
         k++;
         j++;
      }
   }
   while( i < nL)
   {
      A[k] = L[i];
      i++;
      k++;
   }
   while( j < nR)
   {
      A[k] = R[j];
      j++;
      k++;
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ya i have done it with Comparator, trying to do it with my own mergesort method, thank you I will give this a try!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.