Scarl
Scarl

Reputation: 950

Linked Lists & quickSort

I was trying another approach to sort a linked list. Aside from the available methods, i decided to take each node from the linked list and place it in an array and by that i would be able to compare the data variables easily. I applied the quickSort on the array and this is what i got...however, when the result is displayed, I'm getting the memory address of the nodes and not the Students information

This is what is appearing: (as an output)

Sorted list is:

homework2.Node@44b471fe
homework2.Node@22a7fdef
homework2.Node@431067af
homework2.Node@6a07348e
null

This is my code.

public static void main(String[] args) {

MyLinkedList list = new MyLinkedList();

Student s = new Student(1, "John", 20, "Italy", "2011");
list.addStudent(s);
Student s2 = new Student(2, "Mark", 19, "UAE", "2010");
list.addStudent(s2);
Student s3 = new Student(3, "Sally", 35, "UAE", "2000");
list.addStudent(s3);

 System.out.println("Students in the list: ");
list.print();

 Node[] n = list.convertA(list);
  quickSort(n, 0, (n.length-1));
  System.out.println("Sorted list is:");

  for(int q =0;q<n.length;q++){
      System.out.println(n[q] + " ");
  }
}

public static int partition(Node arr[], int left, int right) {

int i = left, j = right;

Node tmp;

Node pivot = arr[(left + right) / 2];

while (i <= j) {

    while (arr[i].getStudent().getAge() < pivot.getStudent().getAge()) {
        i++;
    }

    while (arr[j].getStudent().getAge() > pivot.getStudent().getAge()) {
        j--;
    }

    if (i <= j) {

        tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
        i++;
        j--;

    }

}
return i;

}

public static void quickSort(Node arr[], int left, int right) {

int index = partition(arr, left, right-1);

if (left < index - 1) {
    quickSort(arr, left, index - 1);
}

if (index < right) {
    quickSort(arr, index, right);
}
}

the Node class is as follows:

public class Node {

private Student student;
public Node link;

public Node() {
    student = null;
    link = null;
}

public Node(Student s) {
    student = s;
}

public Node(Student s, Node l) {
    student = s;
    link = l;
}

public Object getData() {
    return student.toString();
}

public Student getStudent() {
    return student;
}

public void setLink(Node link) {
    this.link = link;
}

public void setStudent(Student student) {
    this.student = student;
}

@Override
public String toString() {
    return this.student.toString();
}
}

Upvotes: 0

Views: 1886

Answers (2)

Scarl
Scarl

Reputation: 950

I've fixed the issue and sorted the array,

there was an issue in my quickSort method.. for the index it should be:

int index = partition(arr,left,right)

instead of :

int index = partition(arr, left, right-1);

As ZouZou pointed out earlier, for loop was faulty so I fixed that as well

Thanks a lot for the help !

Upvotes: 0

Alexis C.
Alexis C.

Reputation: 93872

.however, when the result is displayed, I'm getting the memory address of the nodes and not the Students information

Because you didn't override the toString method in your Node class so you print the default behavior of this method defined in the Object class :

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

First, you have to override the toString method in your Student class :

@Override
public String toString (){
  return this.id+"-"+this.name+"-"+this.age+"-"+this.country+"-"+this.year;
}

Then you can override the toString method in your Node class :

 @Override
 public String toString (){
   return this.student.toString();
 }

And then let your for loop like this.

Or just override the toString method in the Student class :

And modify your for loop like this :

for(int q =0;q<n.length;q++){
      System.out.println(n[q].getStudent() + " ");
}

Upvotes: 3

Related Questions