Mercurial > hg > Database > Alice
view src/alice/test/topology/mergesort/MergeArray.java @ 151:98a1292ae8ef working
add merge sort
author | sugi |
---|---|
date | Thu, 29 Nov 2012 16:28:36 +0900 |
parents | |
children |
line wrap: on
line source
package alice.test.topology.mergesort; import java.util.List; import org.msgpack.type.Value; import alice.codesegment.CodeSegment; import alice.datasegment.CommandType; import alice.datasegment.Receiver; public class MergeArray extends CodeSegment{ private Receiver info1 = ids.create(CommandType.TAKE); private Receiver info2 = ids.create(CommandType.TAKE); int keyNum1; int keyNum2; public MergeArray(int num1, int num2) { keyNum1 = num1; keyNum2 = num2; String key1 = Integer.toString(num1); String key2 = Integer.toString(num2); info1.setKey("local", key1, 1); info2.setKey("local", key2, 1); } @Override public void run() { List<Value> list1 = info1.asArray(); List<Value> list2 = info2.asArray(); //System.out.println(list1); //System.out.println(list2); int length = list1.size() + list2.size(); int[] array = new int[length]; for (int i=0,k=0,j=0;i<length;i++){ int array1 = list1.get(j).asIntegerValue().getInt(); int array2 = list2.get(k).asIntegerValue().getInt(); if (array1<=array2){ array[i]=array1; j++; if (j == list1.size()){ for (i=i+1;i<length/*&&k!=list2.size()*/;i++,k++){ array[i]=list2.get(k).asIntegerValue().getInt(); } break; } } else if (array1>array2){ array[i]=array2; k++; if (k == list2.size()){ for (i=i+1;i<length/*&&j!=list1.size()*/;i++,j++){ array[i]=list1.get(j).asIntegerValue().getInt(); } break; } } } /* for (int i = 0;i<length;i++){ System.out.println(array[i]); } */ int num = (keyNum1-1)/2; String key = Integer.toString(num); ods.put("local", key, array); } }