Saturday, January 5, 2013

Non recursive implementation of mergesort

It took me a while to implement the mergesort algorithm . The algorithm was an easier one to understand but a difficult one to write .
Here is a great video on youtube which explains the mergesort
http://www.youtube.com/watch?v=GCae1WNvnZM


The question is why did I write a non-recursive program not a recursive one. I would say I have always hated recursion because of my inability to comprehend it .Whenever I try to understand recursion , I get limited only to factorial of a number like program or a fibonacci sequence number . I am never able to completely understand the complicated recursive programs . I always feel that this is something that I would never be able to think of while writing a program .So for me recursion is a technique which if you can implement, then that is well and good and if you can not,  then think of a non recursive approach which is more fun.

Below is the code of program.

 /*Author:Ashish Sharma
The program does the sorting of integers using MergeSort algorithm
I have not assigned any variables for Math functions used in the program so as to make the program look easy
The following test cases are covered
1.Count of numbers given as input to the program is a number equal to Math.pow(2,k)
2.Count of numbers given as input to the program is a number not equal to Math.pow(2,k)
3.Same number is repeated multiple times in the input
4.Count of input numbers  is odd in number */
import java.util.*;
import java.lang.Math;

public class MergeSort {
public static void main (String args[])
{
/*Take the input into ArrayList using scanner*/
ArrayList InputArrayList = new ArrayList();
System.out.println("Give the input array, It will take the input until you press a non numeric key");
Scanner scan = new Scanner(System.in);
while(scan.hasNextInt())
{
InputArrayList.add(scan.nextInt());
}
System.out.println("ArrayList is " + InputArrayList);
/*Convert the ArrayList to IntegerArray */
int i, SIZE=InputArrayList.size();
int[] InputArrayInt= new int[SIZE];
int[] OutputArrayInt= new int[SIZE];
for(i=0;i{InputArrayInt[i]=InputArrayList.get(i);}

/*pass this array to mergesort function for sorting and collect it to OutputArrayInt*/
OutputArrayInt=mergesort(InputArrayInt);
/*Display the output array*/
System.out.println("The sorted array is ");
for(i=0;iSystem.out.println(OutputArrayInt[i]);
}

public static int[] mergesort(int[] IAInt)
{
int lenth= IAInt.length;
int i, count,k,l,m,n,temp,index=0;
int[] OAInt=new int[lenth];
/*Initially the each element is taken as a subarray when k=0,
The array is divided into subarrays of two elements each when k=1,
Then , the array is divided into subarrays of four elments each when k=2;
and so on..
The i sets a limit to the length , inside which there are two subarrays of Math.pow(2,k) length which are merged .
So, the merging at any time is done for two subarrays the one that begins at i+(even number)*Math.pow(2,k) which is l and the one that begins
i+(odd number )*Math.pow(2,k) which is m
The importance of while loop inside if conditions is when l and m have reached their maximum limit and you want to just put values to an output array from a sorted array and increment index and l or m.
Count is the maximum number of operations that needs to be done while merging two sorted arrays.*/
for(k=0;k<=Math.floor(Math.log(lenth)/Math.log(2));k++)
{
for(i=0;i{
for(count=1,l=i,m=i+(int)Math.pow(2,k);count <=(Math.pow(2,k+1));count++)
{
//System.out.println("The values of k, i,l ,m and count are "+k+" "+i+" " +l+" "+m+" "+count);
if(index{
if(IAInt[l]<=IAInt[m] && l< i+Math.pow(2,k) && m {
OAInt[index]=IAInt[l];
l=l+1;
index=index+1;
if(l==i+Math.pow(2,k) )
{
while(m{
OAInt[index]=IAInt[m];
index=index+1;
m=m+1;
count=count+1;
}
}
}
else if(IAInt[l]>IAInt[m] && l< i+Math.pow(2,k) && m {
OAInt[index]=IAInt[m];
m=m+1;
index=index+1;
if(m==i+2*Math.pow(2,k)||m==lenth)
{
while(l{
OAInt[index]=IAInt[l];
index=index+1;
l=l+1;
count=count+1;
}
}
}
}
/*This else condition is added for the case when m has exceeded the length of the array while assigning value to m in the count loop */
else if (index=lenth )
{
while(l < lenth)
{
OAInt[l]=IAInt[l];
l=l+1;
}
}
}//end of count loop
}//end of i loop

/*This for loop is to assign the sorted values to IAInt because mergesort operations are performed on IAInt.*/
for(n=0;n{
IAInt[n]=OAInt[n];
}
index=0;
}//end of k loop
/*The last return can be either of IAInt of OAInt because IAInt and OAint are the same at the end of completion of for loops.*/
return(OAInt);
}
}