1

i am trying to sort an array of strings which are terms of a polynomial. every position is 1 term of the polynomial as a string, and signed approapriately, however i want to sort them in order by the power.

eg

+3x^5
+5
-8x
-4x^2

how i have approached this is by creating a second array storing just the power, and i want to sort them both based off this array. ie

for (int i=0; i<sortArray.length; i++) {
        if (sortArray[i].indexOf("^")!= -1) 
            sortArrayDegree[i] = Integer.parseInt((sortArray[i].
                  substring(sortArray[i].indexOf("^") + 1, sortArray[i].length())));
        else if (sortArray[i].indexOf("x")!= -1)    
            sortArrayDegree[i]=1;
        else 
            sortArrayDegree[i]=0;
    }

however i am not sure how to link the two, so any changes to the second happen to the first

currently that means the second array looks like this
5
0
1
2
i thought i could make a new array and store this as the second column(clash of data types), but that still leaves the sorting problem

2
  • How is the polynomial represented? As an array of Strings? As two arrays? Without this information, it is impossible to properly answer the question. Commented Jan 18, 2016 at 9:23
  • the polynomial is stored as a singly linked list containing coefficient and power in each node, but in preparation for converting back from singly linked list to a string and sorting each term, i have broken it into seperate terms stored in a 1D array Commented Jan 18, 2016 at 9:29

2 Answers 2

4

I'm not sure that the way you want achieve this is the wisest way, but this is how you could do it: Create a class of both the power and the number of the polynomial member. Make that class Comparable, then put it in one array and the sort method will use the comparable method you have overridden from the Comparable interface.

public class PolynomialMember implements Comparable<PolynomialMember> {

    public int power; // public for brevity, but should be private with getters and setters
    public String number; // public for brevity, but should be private with getters and setters

    public PolynomialMember(String number, int power) {
        this.number = number;
        this.power = power;
    }

    @Override
    public int compareTo(PolynomialMember o) {
        return Integer.compare(this.power, o.power);
    }

    // optional: override for pretty printing
    @Override
    public String toString() {
        if(!number.equals("0")) {
            if(number.charAt(0) == '-') {
                return number + "x^" + power;
            } else {
                return "+" + number + "x^" + power;
            }
        } else {
            return "";
        }
    }
}

this way you don't need two arrays, and you certainly shouldn't "link" two arrays.

You can use this class like this:

public static void main(String[] args) {

    List<PolynomialMember> polynom = new ArrayList<PolynomialMember>();
    polynom.add(new PolynomialMember("-5", 3));
    polynom.add(new PolynomialMember("7", 1));
    polynom.add(new PolynomialMember("4", 0));
    polynom.add(new PolynomialMember("8", 2));

    for(PolynomialMember pm : polynom) {
        System.out.print(pm + " ");
        // prints: -5x^3 +7x^1 +4x^0 +8x^2 
    }
    System.out.println();
    Collections.sort(polynom); //this is where the magic happens.
    for(PolynomialMember pm : polynom) {
        System.out.print(pm + " ");
        // prints: +4x^0 +7x^1 +8x^2 -5x^3 
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

hmmm, now that you mention doing it like this, the polynomail is actually stored as a singly linked list, is it possible to sort a singly linked list by one of the terms that a node stores?
yes, that's exactly what I've tried showing here. It would work with a LinkedList as well (it will work with any sortable collection from the collections package), but be warned that sorting a LinkedList is more expensive than sorting an ArrayList.
Ideally you would be putting them in a TreeSet really,
0

If I understand correctly, which I'm really not sure, you want to bind the data of 2 arrays containing value types\immutables. The easiest way i know to bind data from 2 arrays is to create a class containing both of them as private members and exposing public methods to control them. in these methods you could implement the logic that defines the relationship between them.

Comments

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.