Sunday, December 14, 2008

ARRAY SORTING WITH OUT USING FREE DEFINE METHODS

import java.io.*;

public class kk1
{

//........ Array sorthing...........
static void sortarr(int[] a)
{
int i,j,temp=0;
for (i = 0; i < a.length-1; i++)
for (j = i + 1; j < a.length; j++)
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}


//.........Program Main................

public static void main(String args[])
{
int asize=0;
int bsize=0;



int i;


System.out.println("\nEnter the First array Size...");

try
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

String s = br.readLine();
asize = Integer.valueOf(s).intValue();
} catch(Exception e)
{
System.out.println(" Exception :"+e);
}
System.out.println("\nEnter the Second array Size...");

try
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);

String s = br.readLine();
bsize = Integer.valueOf(s).intValue();
} catch(Exception e)
{
System.out.println(" Exception :"+e);
}

int arr1[] = new int[asize];
int arr2[] = new int[bsize];

System.out.println("\nEnter values of First array...");

for(i=0;i {
try
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Enter arr1["+i+"]: ");
String s = br.readLine();
arr1[i] = Integer.valueOf(s).intValue();
} catch(Exception e)
{
System.out.println(" Exception :"+e);
}
}

System.out.println("\nEnter values of Second array...");

for(i=0;i {
try
{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("Enter arr2["+i+"]: ");
String s = br.readLine();
arr2[i] = Integer.valueOf(s).intValue();
} catch(Exception e)
{
System.out.println(" Exception :"+e);
}
}

System.out.println("\nInputs of First array : ");
for(i = 0; i < arr1.length; i++)
System.out.print(arr1[i]+" ");
System.out.println("\nInputs of second array : ");
for(i = 0; i < arr2.length;i++)
System.out.print(arr2[i]+" ");



sortarr(arr1);
System.out.println("\n Ofter sorting First array : ");
for(i = 0; i < arr1.length; i++)
System.out.print(arr1[i]+" ");

sortarr(arr2);
System.out.println("\n Ofter sorting Second array : ");
for(i = 0; i < arr2.length; i++)
System.out.print(arr2[i]+" ");


msort(arr1,arr2);


}


//.........Compare and Sort the two arrays..........

public static void msort(int[] arr1,int[] arr2)
{


int count=arr1.length+arr2.length;
int arr3[]=new int[count];
int k=0,i;
int counter1=0;int counter2=0;

for(k=0;k
if(arr1[counter1] {
arr3[k++]=arr1[counter1];
counter1++;
if(counter1==arr1.length)
{
for(;counter2 arr3[k++]=arr2[counter2];
break;
}
}
else
{
arr3[k++]=arr2[counter2];
counter2++;
if(counter2==arr2.length)
{
for(;counter1 arr3[k++]=arr1[counter1];
break;
}
}

System.out.println("\n Ofter sorting Third array : ");
for(i = 0; i < count; i++)
System.out.print(arr3[i]+" ");

}
}






No comments: