Sunday, November 7, 2010

Recursive Merge Sort

import java.io.*;

public class merge
{
public static void main(String a[])
{
int i;
int array[] = {12,9,4,99,120,1,3,10};
System.out.println(" Selection Sort\n\n");
System.out.println("Values Before the sort:\n");
for(i = 0; i < array.length; i++)
System.out.print( array[i]+" ");
System.out.println();
mergeSort_srt(array,0, array.length-1);
System.out.print("Values after the sort:\n");
for(i = 0; i System.out.print(array[i]+" ");
System.out.println();
System.out.println("PAUSE");
}

public static void mergeSort_srt(int array[],int low, int high)
{
if (low >= high)
{
return;
}

int middle = (low + high) / 2;
mergeSort_srt(array, low, middle);
mergeSort_srt(array, middle + 1, high);
int end_low = middle;
int start_high = middle + 1;
while(low<=end_low && start_high<=high)
{
if(array[low] < array[start_high])
low++;
else
{
int temp=array[start_high];
int k;
for(k=start_high;k>low;k--)
array[k]=array[k-1];
array[k]=temp;
end_low++;
low++;
end_low++;
start_high++;
}
}
}
}

No comments:

Post a Comment