Saturday, March 13, 2010

Merge Sort

package com.newproject.model;

public class MergeSort
{

private static int[] arryOne = { 3, 8, 10, 10, 12, 90, 111, 222, 333, 444, 555, 777, 888, 999, 1111, 2222, 3333 };
private static int[] arryTwo = { 1, 2 };
private static int arryOneCounter = 0;
private static int arryTwoCounter = 0;
private static boolean endOfSmallArrayFlag = false;
private static int[] resultArry = new int[arryOne.length + arryTwo.length];

public static void main(String[] args)
{
for (int iCnt = 0; iCnt < resultArry.length; iCnt++)
{
if (arryOne[arryOneCounter] < arryTwo[arryTwoCounter])
{
if (!endOfSmallArrayFlag)
resultArry[iCnt] = arryOne[arryOneCounter];
if (arryOneCounter < arryOne.length - 1)
arryOneCounter++;
else
{
if (arryTwoCounter <= arryTwo.length - 1)
{
if (!endOfSmallArrayFlag)
iCnt++;
resultArry[iCnt] = arryTwo[arryTwoCounter];
arryTwoCounter++;
endOfSmallArrayFlag = true;
}
}
}
else
{
if (!endOfSmallArrayFlag)
resultArry[iCnt] = arryTwo[arryTwoCounter];
if (arryTwoCounter < arryTwo.length - 1)
arryTwoCounter++;
else
{
if (arryOneCounter <= arryOne.length - 1)
{
if (!endOfSmallArrayFlag)
iCnt++;
resultArry[iCnt] = arryOne[arryOneCounter];
arryOneCounter++;
endOfSmallArrayFlag = true;
}
}
}
}
System.out.println("Total expected values are " + (arryOne.length + arryTwo.length));
for (int iCnt = 0; iCnt < resultArry.length; iCnt++)
{
System.out.println("resultArray at poosition " + iCnt + " is " + resultArry[iCnt]);
}
}
}

No comments:

Post a Comment