|
how to wrk with twa threads then compile both to 1 thread
hi, here is the code i've written
import java.io.*;
import java.util.Random;
import java.util.Vector;
public class QuickSortApp
{
int LEN = 20;
int[] arr1 = new int[LEN];
int[] arr2 = new int[LEN];
public static void main(String[] args)
{
int LEN = 20;
int[] arr1 = new int[LEN];
int[] arr2 = new int[LEN];
Random r = new Random();
for (int i = 0; i<arr1.length; i++)
arr1[i] = Math.abs(r.nextInt()%101);
for (int i = 0; i<arr2.length; i++)
arr2[i] = Math.abs(r.nextInt()%101);
System.out.println("Unsorted array1:");
printArray(arr1);
System.out.println("Unsorted array2:");
printArray(arr2);
int[] sorted1 = sort(arr1);
System.out.println("\n\nSorted array1:");
printArray(sorted1);
int[] sorted2 = sort(arr2);
System.out.println("\n\nSorted array2:");
printArray(sorted2);
int array1[]=select(arr1, 70, 80);
if(array1!=null)
System.out.println();
System.out.println("between 70-80 of array1");
printArray(array1);
int array2[]=select(arr2, 70, 80);
if(array2!=null)
System.out.println();
System.out.println("between 70-80 of array2");
printArray(array2);
System.out.println();
System.out.println("Common in both arrays");
if ((common(array1,array2))!=null)
{
printArray(common(array1,array2));
}
else
{
System.out.println("No common number(s) found...");
}
}
private static void printArray(int[] array)
{
System.out.println();
for (int i = 0; i < array.length; i++)
{
if (array[i] < 10)
System.out.print(" ");
System.out.print(array[i] + " ");
if ((i+1) % 20 == 0)
System.out.println();
}
}
private static int[] a;
public static int[] sort(int[] array)
{
a = array;
sort(0, a.length - 1);
return a;
}
public static void sort(int low, int high)
{
if (low >= high)
return;
int p = partition(low, high);
sort (low, p);
sort (p+1, high);
}
public static int partition(int low, int high)
{
int pivot = a[low];
int i = low - 1;
int j = high + 1;
while (i < j)
{
for (i++; a[i] < pivot; i++);
for (j--; a[j] > pivot; j--);
if (i < j)
swap(i, j);
}
return j;
}
public static void swap(int i, int j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static boolean search(int a[], int x)
{
boolean found = false;
for(int i=0; i<a.length; i++)
if(a[i]==x)
{
found=true;
break;
}
return found;
}
public static int[] common(int a[], int b[])
{
Vector v = new Vector();
for(int i=0; i<a.length; i++)
if(search(b, a[i]))
v.add(a[i]);
int[] c=new int[v.size()];
for(int i=0; i<c.length; i++)
c[i] =((Integer) v.elementAt(i)).intValue();
return c;
}
public static int[] select(int a[], int x, int y)
{
int count = 0;
for(int i=0; i<a.length; i++)
if(a[i]>=x && a[i]<=y)
count++;
int [] b;
if(count!=0)
{
b = new int[count];
int j = 0;
for(int i=0; i<a.length; i++)
if(a[i]>=x && a[i]<=y)
b[j++]=a[i];
}
else
b = null;
return b;
}
}
i want to implement arr1 and arr2 in threads. theses threads will list the random numbers. after that thread 3 will take the values from both and sort them
please help..
|