Comment

AK
Error!!,
January 15, 2010 at 5:22 PM

import java.io.*;
import java.util.*;

public class mergeSort{
public static void main(String a[])throws IOException{

BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the lenght of the array");
String len = bufferedreader.readLine();
int length = Integer.parseInt(len);
int[] arr = new int[length];

createArray(arr,length);
findPrimes(arr,length);
mergeSort1(arr,0,length);
}
public static void createArray(int arr[],int len)
{

int i;
Random random = new Random();
for(i=0;i<len;i++)
{
arr[i]=random.nextInt(101);
System.out.println(arr[i]+"");
}
}
public static void findPrimes(int arr[],int len)
{
int i;
for(i=0;i<len;i++)
{
if(isPrime(arr[i])==1)
System.out.println("Prime");
else
System.out.println("not prime");
}
}


public static int isPrime(int num)
{
double sqrt;
sqrt=Math.sqrt(num);
int i;
for(i=2;i<=(int)sqrt;i++)
if(num%i==0)
return 0;
return 1;
}

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

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

This code gives error out of bounds in mergeSort
View All Comments | View Tutorial
Related Tutorial and Articles

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.