The main idea of this project is to parallel bubble sort. my approach to this project is,to create one big array and then divide it in to parts ( 4 or 5) based on the number of threads. for instance if the size of the array is 10, i divide it into 2 sub arrays, 0-4 and 5-9, then one thread has to scan the big arrays, and if the value is between 0-4 , assign the the the first sub arrays, if not assign to the next sub-array. then apple the bubble sort algorithm to all sub-arrays simultaneously. finally, all the sub arrays should be added to a thread safe queue. for now i have three class, the main class where i created the array, the U tiles class for shuffling the array, find the min and max of the array and the bubble sort class that has a bubble sorting algorithms. my challenge for now is, how divide the big array into small sub-arrays and fill the sub-array with values. i will appreciate all suggestions and helps. under is my classes.
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) {
}
}