Я пытаюсь отсортировать число целых чисел, используя 4 потока, разбивая массив и затем повторно объединяя. Я думаю, что я справился с использованием 2 потоков, может кто-нибудь помочь отсюда, как я бы перейти к использованию 4?
import java.util.Random;
import java.util.Arrays;
public class threadSort implements Runnable {
private static final int L = 64;
private static int[] nums;
private static Random rand;
public void run() {
//sorting upper half
Arrays.sort(nums, nums.length/2, nums.length);
}
public static void main(String args[]) throws InterruptedException {
nums = new int[L];
rand = new Random();
//fill the array
for(int i=0; i<nums.length; i++) {
nums[i] = rand.nextInt(10);
}
Thread t = new Thread(new threadSort());
t.start();
//sorting lower half
Arrays.sort(nums, 0, nums.length/2);
t.join();
/* merge */
int j = 0;
int k = nums.length/2;
int[] tmp = new int[nums.length];
for (int i=0; i<tmp.length; i++){
if (j < nums.length/2) {
if (k < nums.length) {
if (nums[j] < nums[k]) {
tmp[i] = nums[j++];
} else {
tmp[i] = nums[k++];
}
} else {
/* reached end of second half, copy first*/
tmp[i] = nums[j++];
}
} else {
/* reached end of 1st half, copy 2nd*/
tmp[i] = nums[k++];
}
}
nums = tmp;
//Testing Sort and Total
int count = 0;
for(int i=0; i<nums.length; i++){
System.out.print(nums[i] + " ");
count++;
}
System.out.println();
System.out.println("Total amount of Integers = " + count);
}
}
вот что у меня есть
Я хочу придерживаться того, что у меня есть, но добавить еще 2 темы в