List

I have one problem. pls solve it by using Java.

Problem: Write a program that merges two ordered list objects of integers into a single ordered list object of integers. Method merge of class ListMerge should receive references to each of the list objects to be merged, and should return a reference to the merged list object.

thans,

Ripon

[363 byte] By [riponc007a] at [2007-9-29 0:28:06]
# 1

It is not much sweat and probably a good exercise for you if you write it yourself. But if you really think your life is being easier if someone "Write a program" instead of you, here you go:

public static int[] merge(int[] _o1, int[] _o2){

Arrays.sort(_o1);

Arrays.sort(_o2);

return mergeSorted(_o1,_o2);

}

public static int[] mergeSorted(int[] _o1, int[] _o2){

if(_o1.length<=0) return _o2;

if(_o2.length<=0) return _o1;

//<declare new int[] to store result>

int[] newData=new int[_o1.length+_o2.length];

int nrNewData=0;

//<chose data with least maxValue>

int lowerPos=0;//lower

int lowerNrData;

int[] lowerData;

int higherPos=0;//higher

int higherNrData;

int[] higherData;

int lastCompare=_o1[_o1.length-1]-_o2[_o2.length-1];

if(lastCompare<0){//_o1<_o2

lowerData=_o1;

lowerNrData=_o1.length;

higherData=_o2;

higherNrData=_o2.length;

}else{//_o1>=_02

lowerData=_o2;

lowerNrData=_o2.length;

higherData=_o1;

higherNrData=_o1.length;

}

//<parse through higherData and look at the lowerData

while(lowerPos><lowerNrData && higherPos><higherNrData){

int comp=higherData[higherPos]-lowerData[lowerPos];

while(comp><0){

newData[nrNewData++]=higherData[higherPos++];

comp=higherData[higherPos]-lowerData[lowerPos];

}

if(comp==0){//==

higherPos++;//match => take just one and skip this

}

newData[nrNewData++]= lowerData[lowerPos++];

}

//<take the rest from higherdata>

for(int i=higherPos; i<higherNrData; i++){

newData[nrNewData++]= higherData;

}

int[] res=new int[nrNewData];

System.arraycopy(newData,0,res,0,nrNewData);

return res;

}

--

replace int[] to Comparable[] and - to .compareTo(..) then you have a general merge

Gil>

gilroittoa at 2007-7-13 2:55:11 > top of Java-index,Other Topics,Algorithms...
# 2

A simpler way, given that the input arrays are already sorted:public static int[] merge(int[] _o1, int[] _o2){

if(_o1.length<=0) return _o2;

if(_o2.length<=0) return _o1;

int[] result = new int[_o1.length + _o2.length];

System.arraycopy(_o1, 0, result, 0, _o1.length);

System.arraycopy(_o2, 0, result, _o1.length, _o2.length);

Arrays.sort(result);

return result;

}

DrClapa at 2007-7-13 2:55:11 > top of Java-index,Other Topics,Algorithms...
# 3

I think the problem is dealing with LinkedLists and not arrays. Looks like a homework assignment question:

public static LinkedList merge(LinkedList l1, LinkedList l2) {

LinkedList l = new LinkedList();

Node n1 = l1.getHead();

Node n2 = l2.getHead();

//... have fun doing your own homework! =)

return l;

}

rkippena at 2007-7-13 2:55:11 > top of Java-index,Other Topics,Algorithms...
# 4
Jeez, do it the hard way...List listOne = ....List listTwo = ....listOne.removeAll(listTwo);listOne.addAll(listTwo);// Done.
rconnera at 2007-7-13 2:55:11 > top of Java-index,Other Topics,Algorithms...