ListNode vs Array Efficency

I'm not really having a problem, just a question....

I'm reading in a large text file (AKA Hamlet or some other work of literature) and puting them into an array or ListNode Alphebetiaclly. Now shouldn't theoretically the ListNode system run faster than the array system because the ListNode does not require shifting values.

Could someone explain to me why the array is consistently beating the ListNode approach?

Here are my insert methods if it will help:

publicint insertWord(String word){

return ll ? insertWordLL(word) : insertWordAL(word);

}

privateint insertWordLL(String word){

numWords++;

if (head==null){ head =new WordListNode(word,null);return 1;}

WordListNode pos = head;

while (pos.getNext()!=null && pos.getNextNode().getStringValue().compareToIgnoreCase(word)<0)

pos = pos.getNextNode();

if (pos.getNext()!=null&&pos.getNextNode().getStringValue().compareToIgnoreCase(word)==0){

pos.getNextNode().increment();

return pos.getNextNode().getOccurances();

}elseif (pos.getStringValue().compareToIgnoreCase(word)==0){

pos.increment();

return pos.getOccurances();

}else{

pos.setNext(new WordListNode(word,pos.getNextNode()));

return 1;

}

}

privateint insertWordA(String word){

numWords++;

if (array==null){ array=new Word[1]; array[0]=new Word(word);return 1;}

int pos = -1;

Word w2=null;

do{

pos++;

if (!(pos<array.length))break;

w2 = array[pos];

}while (word.compareToIgnoreCase(w2.getWord())><0);

if (pos>=array.length){

Word[] a2 =new Word[array.length+1];

System.arraycopy(array,0,a2,0,array.length);

a2[a2.length-1]=new Word(word);

array = a2;

System.out.println();

return 1;

}elseif (w2!=null&&word.compareToIgnoreCase(w2.getWord())==0){

w2.inc();

return w2.occurences();

}

else{

Word[] a2 =new Word[array.length+1];

if (pos!=0) System.arraycopy(array,0,a2,0,pos);

a2[pos] =new Word(word);

System.arraycopy(array,pos,a2,pos+1,array.length-pos);

array=a2;

return 1;

}

}

Thank you!

[4407 byte] By [AFruvous] at [2007-9-30 20:34:11]
# 1

Without putting too much thought into the problem, I would guess that the reason the array is winning is because it will find the proper position to insert the value much faster than traversing a linked list. So, while inserting an element into the linked list would be faster, actually finding where to put it is probably slower.

jdmiller at 2007-7-7 1:23:39 > top of Java-index,Java Essentials,Java Programming...
# 2

One other thing you might want to try is to use a TreeSet from the java.util package. It implements the SortedSet interface which guarantees that an Iterator returned from its iterator() method returns the items in sorted order. The only problem with it is that it is a Set, so it's not good if you want duplicates. It also only uses the compareTo() method to determine equality which can cause some real headaches if you are using objects where a.compareTo(b) == 0 but a.equals(b) is false.

jdmiller at 2007-7-7 1:23:39 > top of Java-index,Java Essentials,Java Programming...
# 3

Your best option would be to use the TreeMap as this

runs in O(log n) for each word insert. It is sorted but can only

store one copy of each word. Therefore you should use the word

as the key and a counter as the value. In this way you can keep

track of how many occurances of each word you have. Another

option that may be quicker is to use a HashMap in exactly the

same way as the tree map. The has map has an expected access

time of O(1) but is not sorted. Therefore you would add all the

words and then sort the result afterwards. A number of sorting algorithms work in expected O(mlog m).

where n is the number of words in the document and m is the number of unqiue words in the document. Which approach you

choose depends on the ratio of words to unique words and the general performace of the structures you use.

matfud

matfud at 2007-7-7 1:23:39 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks for the suggestions, the point however is that this is an assignment meant to compare ListNode vs Array efficency and its supposed to come out the other way.
AFruvous at 2007-7-7 1:23:39 > top of Java-index,Java Essentials,Java Programming...
# 5

If you implement the 'shifting' of the array to make room without using System.arraycopy you might actually find it slower, which is probably what the assignment meant for you to do.Try implementing a function similar to arraycopy, but do the copying yourself and see how the performance changes.

jdmiller at 2007-7-7 1:23:39 > top of Java-index,Java Essentials,Java Programming...
# 6
what is this doing?public int insertWord(String word){return ll ? insertWordLL(word) : insertWordAL(word);}
matfud at 2007-7-7 1:23:39 > top of Java-index,Java Essentials,Java Programming...
# 7
Thanks for the input.That is checking if it is using the ListNode Algorithm, and if not using the array algorithm. the method name in the second part is wrong it should call insertWordA not insertWordAL, Just ignore it.
AFruvous at 2007-7-7 1:23:39 > top of Java-index,Java Essentials,Java Programming...