Barry The Wizard
Barry The Wizard

Reputation: 1318

Java - Merge sort array

I'm making an algorithm for a shop. I have made an array which contains customer information. Now I want to implement a merge-sort on this array and sort it on age. This is my code for the customer class:

public class Customer{

private int customerID;
private String name;
private int age;
private char gender;
private String email;

public List<Customer> customerList = new ArrayList<Customer>();

public Customer(String name, int age, char gender, String email) {
    this.customerID = customerList.size();
    this.name= name;
    this.age= age;
    this.gender= gender;
    this.email = email;
    customerList.add(this);
}
public int getCustomerID() {
    return customerID;
}

public void setCustomerID(int customerID) {
    this.customerID = customerID;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name= name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public char getGender() {
    return gender;
}

public void setGender(char gender) {
    this.gender = gender;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

}

This is my merge-sort:

    private int[] helper;
private int number;

public void sort(Klant[] values) {
    this.customerList= values;                      <---Error
    number = values.length;
    this.helper = new int[number];
    mergesort(0, number - 1);
}

private void mergesort(int low, int high) {
    // check if low is smaller then high, if not then the array is sorted
    if (low < high) {
        // Get the index of the element which is in the middle
        int middle = low + (high - low) / 2;
        // Sort the left side of the array
        mergesort(low, middle);
        // Sort the right side of the array
        mergesort(middle + 1, high);
        // Combine them both
        merge(low, middle, high);
    }
}

private void merge(int low, int middle, int high) {

    // Copy both parts into the helper array
    for (int i = low; i <= high; i++) {
        helper[i] = customerList[i];
    }

    int i = low;
    int j = middle + 1;
    int k = low;
// Copy the smallest values from either the left or the right side back
    // to the original array
    while (i <= middle && j <= high) {
        if (helper[i] <= helper[j]) {
            customerList[k] = helper[i];
            i++;
        } else {
            customerList[k] = helper[j];
            j++;
        }
        k++;
    }
    // Copy the rest of the left side of the array into the target array
    while (i <= middle) {
        customerList[k] = helper[i];
        k++;
        i++;
    }

}

On the line with: this.customerList = values; I get an error: incompatible types Customer[] cannot be converted to List

My question is how can I fix this error and is my merge-sort correct this way?

Edit 1: @Jens Your first option: this.customerList= Arrays.asList(values);

fixed the error. But now I'm getting an error on this line:

            helper[i] = customerList[i];

It says: array required, but List found

Does anybody know how to fix this?

Upvotes: 0

Views: 1338

Answers (2)

yyunikov
yyunikov

Reputation: 5897

Why not to use Collections.sort() method? It also uses merge sort algorithm.

List<Klant> list = Arrays.asList(values);
Collections.sort(list);

Or you can use Arrays.sort() as well, but it does not use merge sort.

But keep in mind that will you need it to implement Comparable interface for your Klant class.

UPDATE:

In case if you need to implement merge sort algorithm by yourself, probably you need to change customerList variable type from List to array of Customer[]. If you would like to use the list you can do as Jens suggested, but use add() method of List and not assign the value via =.

Upvotes: 0

Jens
Jens

Reputation: 69440

You can`t assign a Array to a list.

Try

 this.customerList= Arrays.asList(values);   

or Change the method parameetr to List<Customer>

public void sort(List<Customer> values) {

Upvotes: 2

Related Questions