I'm trying to merge two lists in sorted order, and I was wondering what the fastest way to merge them would be. Currently, my algorithm is basically (ignoring syntax):
merge(a, b){
newlist = new Arraylist()
while(!a.isEmpty() && !b.isEmpty()){
if(a.get(0) > b.get(0))
newlist.add(a.remove(0))
else
newlist.add(b.remove(0))
}
newlist.addAll(a)
newlist.addAll(b)
return newlist
}
Is there a faster way to do this kind of merge? I'm trying to reduce my runtime as much as possible as this function is called thousands of times with very large ArrayLists.