淺談排序算法之桶排序(9)

本篇博客是今天筆者寫的最後一篇排序算法的博客了。桶排序(bucket sort)假設輸入數據服從均勻分佈,平均情況下其時間代價爲O(n)。計數排序假設數據都屬於一個小區間的整數,而桶排序則假設數據均勻、獨立分佈在[0,1)上。
桶排序將區間[0,1)劃分爲n各大小相同的子區間,稱之爲桶,然後將數組中的所有元素都放在對應的桶中。爲了得到排序結果,先對桶中的元素進行排序,然後依次遍歷每個桶把桶中的元素依次列出來即可。將元素放置對應的桶的過程是數組元素乘上桶的個數並向下取整。爲了靈活起見,桶採用鏈表實現。
示例代碼如下:

package org.vimist.pro.Algorithm.Sort;

import org.jetbrains.annotations.NotNull;

import java.util.*;

/**
 * An demonstration of {@code BucketSort}.
 *
 * @author Mr.K
 */
public class BucketSort {

    public static void main(String[] args) {
        int N = 10;
        float[] arr = new float[N], copies = new float[N];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextFloat();
        }
        System.arraycopy(arr, 0, copies, 0, N);
        System.out.println("待排序數組: " + Arrays.toString(arr));
        Bucket_Sort(arr);
        System.out.println("已排序數組: " + Arrays.toString(arr));
        Arrays.sort(copies);
        System.out.println("參考  數組: " + Arrays.toString(copies));
    }

    /**
     * Accepts an array and sorts the specified array using {@code BucketSort}.
     * In the example, each element locates in the range from 0(inclusive) to 1
     * (exclusive) and random uniformly distributes. The process compromise 4
     * steps, which are:
     * <ul>
     *     <li>The first step is to initialize an array, each element is a list, and
     *     initialize each list, which can be called a <em>Bucket</em></li>
     *     <li>The second step is to place each element in the specified array
     *     into the rightful bucket.</li>
     *     <li>The third step is to sort each list in the newly-established array.
     *     </li>
     *     <li>The last step is to reassign value from each non-empty list to the
     *     specified array by order.</li>
     * </ul>
     * {@code BucketSort} is a liner sort. In this sort, there exists some assumptions
     * like {@link CountingSort#Counting_Sort(int[])}.
     *
     * @param arr specified array to be sorted
     */
    private static void Bucket_Sort(@NotNull float[] arr) {
        int index = 0;
        LinkedList<Float>[] lists = new LinkedList[10];
        for (int i = 0; i < lists.length; i++) {
            lists[i] = new LinkedList<>();
        }
        for (int i = 0; i < arr.length; i++) {
            lists[(int) (arr[i] / 0.1)].add(arr[i]);
        }
        for (int i = 0; i < lists.length; i++) {
            Collections.sort(lists[i]);
        }
        for (int i = 0; i < lists.length; i++) {
            if (!lists[i].isEmpty()) {
                Iterator iterator = lists[i].iterator();
                while (iterator.hasNext()) {
                    arr[index++] = (float) iterator.next();
                }
            }
        }
    }

}

運行結果如下:

待排序數組: [0.1410836, 0.64014935, 0.68507814, 0.488932, 0.8883444, 0.63293314, 0.4323581, 0.098573446, 0.21807796, 0.36170465]
已排序數組: [0.098573446, 0.1410836, 0.21807796, 0.36170465, 0.4323581, 0.488932, 0.63293314, 0.64014935, 0.68507814, 0.8883444]
參考  數組: [0.098573446, 0.1410836, 0.21807796, 0.36170465, 0.4323581, 0.488932, 0.63293314, 0.64014935, 0.68507814, 0.8883444]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章