常見的排序算法 (上)

深刻理解算法, 會讓你很好的理解其餘算法思想 ,其實對於算法大多數都是很好理解的java

時間複雜度 , 空間複雜度算法

1. 選擇排序

/**
 * 選擇排序 , 從開始位置每次找出最小那個數字, 放到起始位置 ,而後起始位置迭代重複操做
 *
 * @author: <a href='mailto:fanhaodong516@qq.com'>Anthony</a>
 */
public class SelectSort {

    private static void sort(int[] arr, int start, int len) {
        for (int i = start; i < len; ++i) {
            int min = i;
            for (int j = i + 1; j < len; ++j) {
                if (arr[min] > arr[j]) {
                    min = j;
                }
            }
            Common.swap(arr, i, min);
        }
    }
}

2. 冒泡排序

/**
 * 冒泡排序 ; 就是將大的數組往上冒(我這裏上指的是數組尾端) , 因此冒一次就能夠將一個最大的數冒的最上面, 因此不須要每次都所有冒
 *
 * 
 * @author: <a href='mailto:fanhaodong516@qq.com'>Anthony</a>
 */
public class BubbleSort {

    private static void sort(int[] arr, int start, int len) {
        for (int i = start; i < len; i++) {
            for (int j = start; j < len - start - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    Common.swap(arr, j, j + 1);
                }
            }
        }
    }
}

3. 插入排序

/**
 * 插入排序  好比 2,3,1 數組 , 假如此時到1了進行插入排序,  1先拿出來, 3和1比較大,就向後移動一下,而後2和1比較仍是大就繼續向後移動, 此時就能夠找到合適位置插入了
 * 
 * @author: <a href='mailto:fanhaodong516@qq.com'>Anthony</a>
 */

public class InsertSort {

    private static void sort(int[] arr) {

        /**
         * 空 或者  0 / 1 都直接返回
         */
        if (null == arr || arr.length <= 1) {
            return;
        }

        // 2 3 1
        for (int index = 1; index < arr.length; index++) {

            // 當前位置 , 開始必須從第二個開始
            int temp = arr[index];

            // 左邊位置
            int left = index - 1;

            // 移動座標其實就是 ...
            while (left >= 0 && arr[left] > temp) {

                // 互換位置
                arr[left + 1] = arr[left];

                // 向前移動
                left--;
            }

            // 最後保存數據數據所在位置
            arr[left + 1] = temp;
        }
    }
}

4. 快速排序

他利用遞歸的思想, 找到一個數, 那個數 ,他左邊所有比他小, 右邊所有比他大 , 而後無限遞歸下去 . 就能夠快速排序了數組

/**
 * 快速排序
 * 
 * @author: <a href='mailto:fanhaodong516@qq.com'>Anthony</a>
 */
public class QuickSort {
    public static void main(String[] args) {
        int[] arr_1000 = Common.generate_Arr_1000();
        quickSort(arr_1000, 0, arr_1000.length);
        showarr(arr_1000);
    }


    private static void quickSort(int[] arr, int low, int high) {
        if (low >= high) return;
        int index = getIndex2(arr, low, high);
        quickSort(arr, low, index - 1);
        quickSort(arr, index + 1, high);
    }


    /**
     * 方法一 目的就是找到一個值 , 其實就是low索引所對應的的那個值
     * 他的左邊所有小於他的右邊, 而後返回他的位置, 繼續遞歸
     *
     * @return 返回low對應的數組中的數據, 所應該對應的真正索引位置
     */
    private static int getIndex(int[] arr, int low, int high) {
        int tmp = arr[low];
        while (low < high) {
            while (low < high && arr[high] >= tmp) {
                high--;
            }
            arr[low] = arr[high];
            while (low < high && arr[low] <= tmp) {
                low++;
            }
            arr[high] = arr[low];
        }
        arr[low] = tmp;
        return low;
    }


    /**
     * 方法二  : 傳統交換方法 和上述同樣, 看喜歡用哪一個
     *
     * @return
     */
    private static int getIndex2(int[] arr, int low, int high) {
        int start = low;
        int tmp = arr[low];
        while (low < high) {
            while (low < high && arr[high] >= tmp) {
                high--;
            }
            while (low < high && arr[low] <= tmp) {
                low++;
            }
            swap(arr, low, high);
        }
        // 這裏最好本身試一試
        arr[start] = arr[low];
        // 這裏也是想想就理解了
        arr[low] = tmp;
        return low;
    }


    private static void swap(int[] arr, int low, int high) {
        int temp = arr[low];
        arr[low] = arr[high];
        arr[high] = temp;
    }

    private static void showarr(int[] arr) {
        int count = 0;
        for (int i : arr) {
            count++;
            System.out.printf("%d\t", i);
            if (count % 10 == 0) {
                System.out.println();
            }
        }
    }
}
相關文章
相關標籤/搜索