【问题标题】:Parallel implementation of bubble sort冒泡排序的并行实现
【发布时间】:2017-10-10 08:31:54
【问题描述】:

这个项目的主要思想是并行冒泡排序。我对这个项目的方法是,创建一个大数组,然后根据线程数将其分成几部分(4 或 5)。例如,如果数组的大小是 10,我将它分成 2 个子数组,0-4 和 5-9,然后一个线程必须扫描大数组,如果值在 0-4 之间,则分配第一个子数组,如果没有分配给下一个子数组。然后将冒泡排序算法同时应用于所有子数组。最后,应将所有子数组添加到线程安全队列中。 现在我有三个类,我创建数组的主类,用于洗牌数组的 U 瓦片类,找到数组的最小值和最大值以及具有冒泡排序算法的冒泡排序类。 我现在的挑战是,如何将大数组分成小的子数组并用值填充子数组。 我会感谢所有的建议和帮助。下面是我的课。

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) {
    }
}

【问题讨论】:

  • 你知道分而治之是什么意思吗?

标签: java arrays


【解决方案1】:

我认为你应该尝试合并排序,使用 Fork/Join。

喜欢这里:

import java.util.Random;
import java.util.concurrent.*;

/**
 * Example of Merge Sort using Fork/Join framework.
 *
 * @author L.Gobinath
 */
public class ForkJoinArraySort {
// From Java 7 '_' can be used to separate digits.
public static final int ARRAY_SIZE = 10_000_000;

public static void main(String[] args) {
    // Create a pool of threads
    ForkJoinPool pool = new ForkJoinPool();
    int[] array = createArray(ARRAY_SIZE);
    long startTime;
    long endTime;

    MergeSort mergeSort = new MergeSort(array, 0, array.length - 1);
    startTime = System.currentTimeMillis();

    pool.invoke(mergeSort); // Start execution and wait for result/return

    endTime = System.currentTimeMillis();
    System.out.println("Time taken: " + (endTime - startTime) + " millis");
}

/**
 * Create an array with random numbers.
 * @param  size Size of the array.
 * @return      An array with the given size.
 */
private static int[] createArray(final int size) {
    int[] array = new int[size];
    Random rand = new Random();
    for (int i = 0; i < size; i++) {
        array[i] = rand.nextInt(1000);
    }
    return array;
}
}

/**
 * Extends RecursiveAction.
 * Notice that the compute method does not return anything.
 */
class MergeSort extends RecursiveAction {
private int array[];
private int left;
private int right;

public MergeSort(int[] array, int left, int right) {
    this.array = array;
    this.left = left;
    this.right = right;
}

/**
 * Inherited from RecursiveAction.
 * Compare it with the run method of a Thread.
 */
@Override
protected void compute() {
    if (left < right) {
        int mid = (left + right) / 2;
        RecursiveAction leftSort = new MergeSort(array, left, mid);
        RecursiveAction rightSort = new MergeSort(array, mid + 1, right);
        invokeAll(leftSort, rightSort);
        merge(left, mid, right);
    }
}

/**
 * Merge two parts of an array in sorted manner.
 * @param left  Left side of left array.
 * @param mid   Middle of separation.
 * @param right Right side of right array.
 */
private void merge(int left, int mid, int right) {
    int temp [] = new int[right - left + 1];
    int x = left;
    int y = mid + 1;
    int z = 0;

//There some kind of sort at the leaf
//You can use your BubbleSort class
//***************************************************************
    while (x <= mid && y <= right) {
        if (array[x] <= array[y]) {
            temp[z] = array[x];
            z++;
            x++;
        } else {
            temp[z] = array[y];
            z++;
            y++;
        }
    }
//***************************************************************


    while (y <= right) {
        temp[z++] = array[y++];
    }
    while (x <= mid) {
        temp[z++] = array[x++];
    }

    for (z = 0; z < temp.length; z++) {
        array[left + z] = temp[z];
    }
}
}

(我有自己的算法,在我学习算法的时候使用归并排序,在叶子节点排序中使用冒泡排序。但是很久以前,我失去了它,对不起。)

附:我认为回忆 Array.sort 仍然比个人实现冒泡排序更快是多余的。

【讨论】:

  • OP 专门要求冒泡排序,而不是合并排序。
  • @cello 实际上他被要求进行并行排序 - 实际上是合并排序。在叶子上,他想使用冒泡排序。
猜你喜欢
  • 2017-03-11
  • 2012-07-23
  • 2021-12-25
  • 1970-01-01
  • 2014-02-20
  • 1970-01-01
  • 2020-08-23
  • 1970-01-01
相关资源
最近更新 更多