該項目的主要思想是並行氣泡排序。我的這個項目的方法是,創建一個大數組,然後根據線程數將它分成幾部分(4或5)。例如,如果數組的大小爲10,我將它分成2個子數組,0-4和5-9,然後一個線程必須掃描大數組,如果數值在0-4之間,則分配第一個子數組,如果沒有分配給下一個子數組。然後將泡泡排序算法同時應用於所有子陣列。最後,所有的子數組應該被添加到線程安全隊列中。 現在我有三個類,我創建數組的主類,用於洗牌數組的U tiles類,查找數組的最小和最大值以及具有泡沫排序算法的冒泡排序類。 我現在面臨的挑戰是,如何將大數組分成小的子數組,並用值填充子數組。 我會感謝所有的建議和幫助。下是我的課程。並行實現氣泡排序
package com.company;
import javax.xml.transform.sax.SAXSource;
import java.util.Random;
public class Main {
public static void main(String[] args) {
// filling the array with integer values
int[] anArray = Utils.fillArray((int) 10);
Utils.shuffleArray(anArray);
// find the min and max of the array
System.out.println("************************");
BubbleSort sort = new BubbleSort(anArray);
profiler.start();
sort.sortMethod();
// Utils.printArray(anArray);
Utils.findMinMax(anArray);
}
}
package com.company;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class Utils {
private static volatile int max;
private static volatile int min;
private static int[][] arrays;
public Utils(int[][] arrays,int[] array) {
max = array[0];
min = array[0];
}
// taken from the kings class
// source: http://stackoverflow.com/questions/1519736/random-shuffling-of-an-array
// Implementing Fisher�Yates shuffle
public static void shuffleArray(int[] ar) {
// If running on Java 6 or older, use `new Random()` on RHS here
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
public static void printArray(int[] anArray) {
System.out.print("Array: ");
for (int i=0; i< anArray.length; i++){
System.out.print(anArray[i]+" ");
}
System.out.println();
}
public static int[] fillArray(int amount) {
int[] result = new int[amount];
for (int i=0; i<amount; i++){
result[i] = i;
}
return result;
}
public static void findMinMax(int[] array) {
int i = 0;
for (; i < (array.length)/2; i++) {
int num1 = array[1 * 2];
int num2 = array[i * 2 + 1];
if (num1 >= num2) {
if (num1 > max)
max = num1;
if (num2 < min)
min = num2;
} else {
if (num2 > max) {
max = num2;
if (num1 < min)
min = num1;
}
}
}
if (i * 2 < array.length) {
int num = array[i * 2];
if (num > max)
max = num;
if (num < min)
min = num;
}
System.out.println("min is: " + min);
System.out.println("max is : " + max);
}
}
public static int getMax() {
return max;
}
public static int getMin() {
return min;
}
public static void print(int[] anArray, int i) {
}
}
你知道什麼分而治之意味着什麼嗎? – Stefan