About
1) import java.util.Arrays;
public class ParallelBubbleSort {
public static void main(String[] args) {
int[] arr = generateArray(40000);
int numThreads = 4;
int chunkSize = arr.length / numThreads;
long startTime = System.currentTimeMillis();
parallelBubbleSort(arr, numThreads, chunkSize);
long endTime = System.currentTimeMillis();
System.out.println("Parallel Bubble Sort took " + (endTime - startTime) + "ms");
startTime = System.currentTimeMillis();
sequentialBubbleSort(arr);
endTime = System.currentTimeMillis();
System.out.println("Sequential Bubble Sort took " + (endTime - startTime) + "ms");
}
public static void parallelBubbleSort(int[] arr, int numThreads, int chunkSize) {
Thread[] threads = new Thread[numThreads];
for (int i = 0; i < numThreads; i++) {
int startIndex = i * chunkSize;
int endIndex = (i == numThreads - 1) ? arr.length : (i + 1) * chunkSize;
threads[i] = new Thread(new BubbleSortTask(arr, startIndex, endIndex));
threads[i].start();
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
merge(arr, numThreads, chunkSize);
}
public static void sequentialBubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void merge(int[] arr, int numThreads, int chunkSize) {
int[] temp = new int[arr.length];
int[] indexes = new int[numThreads];
Arrays.fill(indexes, -1);
for (int i = 0; i < arr.length; i++) {
int minIndex = -1;
int minValue = Integer.MAX_VALUE;
for (int j = 0; j < numThreads; j++) {
int index = j * chunkSize + indexes[j] + 1;
if (index < (j + 1) * chunkSize && arr[index] < minValue) {
minIndex = j;
minValue = arr[index];
}
}
indexes[minIndex]++;
temp[i] = minValue;
}
for (int i = 0; i < arr.length; i++) {
arr[i] = temp[i];
}
}
public static int[] generateArray(int size) {
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = (int) (Math.random() * size);
}
return arr;
}
}
class BubbleSortTask implements Runnable { int[] arr; int startIndex; int endIndex;
public BubbleSortTask(int[] arr, int startIndex, int endIndex) {
this.arr = arr;
this.startIndex = startIndex;
this.endIndex = endIndex;
}
@Override
public void run() {
for (int i = startIndex; i < endIndex - 1; i++) {
for (int j = startIndex; j < endIndex - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
2) import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors;
public class SumOfPrimes { private static final int NUM_THREADS = 4; private static final int LIMIT = (int) Math.pow(10, 7);
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
long sum = sumOfPrimes(LIMIT);
long endTime = System.currentTimeMillis();
System.out.println("Sum of primes: " + sum);
System.out.println("Time taken: " + (endTime - startTime) + "ms");
}
public static long sumOfPrimes(int limit) {
ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
int chunkSize = limit / NUM_THREADS;
PrimeSumTask[] tasks = new PrimeSumTask[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++) {
int start = i * chunkSize + 1;
int end = (i == NUM_THREADS - 1) ? limit : (i + 1) * chunkSize;
tasks[i] = new PrimeSumTask(start, end);
executor.execute(tasks[i]);
}
executor.shutdown();
while (!executor.isTerminated());
long sum = 0;
for (int i = 0; i < NUM_THREADS; i++) {
sum += tasks[i].getSum();
}
return sum;
}
}
class PrimeSumTask implements Runnable { private final int start; private final int end; private long sum;
public PrimeSumTask(int start, int end) {
this.start = start;
this.end = end;
}
public long getSum() {
return sum;
}
@Override
public void run() {
for (int i = start; i <= end; i++) {
if (isPrime(i)) {
sum += i;
}
}
}
private boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}