Hello!!!!
I want your help for this program!!!
Sorry for my english!
I want to create a pro that will sort an array[1024*16], i want to have 3 classes one for sortingthread, one for mergingthread and one for the main.
I wrote the code for sortingthread and mergingthread class but i have problem for the main class
Code :/** * * @author fakie */ public class MergingThread extends Thread { double[] mergedArray; double[] A1; double[] A2; public MergingThread(double[] Array1, double[] Array2) { A1 = Array1; A2 = Array2; mergedArray = new double[A1.length + A2.length]; } public double[] getMergedArray() { return mergedArray; } @Override public void run() { int i, j, k, m, n; i = 0; j = 0; k = 0; m = A1.length; n = A2.length; while (i < m && j < n) { if (A1[i] <= A2[j]) { mergedArray[k] = A1[i]; i++; } else { mergedArray[k] = A2[j]; j++; } k++; } if (i < m) { for (int p = i; p < m; p++) { mergedArray[k] = A1[p]; k++; } } else { for (int p = j; p < n; p++) { mergedArray[k] = A2[p]; k++; } } System.out.println("Merged"); for(k=0;k<8;k++){ System.out.println(mergedArray[k]); } } }
Code :/** * * @author fakie */ public class SortingThread extends Thread { double[] unsrtArray, sortedArray; private double threadId; private double arraySize; public SortingThread(double[] unsortedArray) { unsrtArray = unsortedArray; arraySize = unsortedArray.length; } public double[] getSortedArray() { return sortedArray; } public void setThreadId(int id) { threadId = id; } @Override public void run() { long startTime = System.currentTimeMillis(); int n = unsrtArray.length; for (int pass = 1; pass < n; pass++) { for (int i = 0; i < n - pass; i++) { if (unsrtArray[i] > unsrtArray[i + 1]) { double temp = unsrtArray[i]; unsrtArray[i] = unsrtArray[i + 1]; unsrtArray[i + 1] = temp; } } } sortedArray = unsrtArray; long endTime = System.currentTimeMillis(); System.out.println("Unique Thread Number: " + threadId + ", Array Size: " + arraySize + ", Sorting took: " + (endTime - startTime) + "ms"); } }
i want to give in main's args the number of threads
if i want to sort an array[16] and i want 4 threads
array[0]-array[3]
array[4]-array[7]
array[8]-array[11]
array[12]-array[15]
the sortingthread will sort the 4 arrays
and then the mertgingthread array will combine the 4 arrays
array[0]-array[3]
------------------------------mergingthread(1)
array[4]-array[7]
-------------------------------------------------------------mergingthread(3)
array[8]-array[11]
------------------------------mergingthread(2)
array[12]-array[15]
The mergingthread(3) it will use the function join to wait to finish mergingthread(1) and mergingthread(2)
i want only the code for soringthread and mergingthread in class main
Please if you understand my problem help me!!!!!
Thanks!!!!!!!!!!!!!
