數據結構之八大排序java版

Time: 13/09/2019

Tool : IntelliJ IDEA2019.2.1x64

Reference  :https://blog.csdn.net/wangshuminjava/article/details/80040673

                    BILIBILI 《Java 數據結構與算法視頻教程全集(195P)| 50 小時從入門到精通》

 

No1>BubbleSort

package SortAlgorithm;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

//加flag優化 的冒泡排序 (extends 交換排序)
public class TestBubble {
    public static void main(String[] args) {

        int[] arr = {5, 1, 2, 4, 7, 3};
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str1  = simpleDateFormat.format(date);
        System.out.println(str1);

        bubble(arr);
        System.out.println(Arrays.toString(arr));

        Date date2 = new Date();
        String str2  = simpleDateFormat.format(date);
        System.out.println(str2);

    }

    //O(n^2)
    public static void bubble(int[] arr) {

        if (arr.length == 0 || arr.length == 1) {
            return;
        }

        boolean flag = false;

        for (int i = 0; i < arr.length - 1; i++) {
            int temp = 0;
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    flag = true;
                    temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
            System.out.println(i+1+Arrays.toString(arr));
            if (!flag) {
                break;
            } else {
                flag = false;
            }
        }
    }
}

No2>QuickSort

package SortAlgorithm;

import java.util.Arrays;

//快速排序 (extends 交換排序)
public class TestQuick {
    public static void main(String[] args) {
        int[] arr = {5, 6, 27, 8, 3, 92, 6, 9};
        System.out.println(Arrays.toString(arr));
        quickSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    private static void quickSort(int[] arr) {

        if (arr == null || arr.length == 0) {
            return;
        }

        recursiveQuickSort(arr, 0, arr.length - 1);
    }

    private static void recursiveQuickSort(int[] arr, int lowIndex, int highIndex) {

        if (lowIndex >= highIndex) {
            return;
        }

        int pivot = arr[lowIndex];
        int pivotIndex = partition(arr, lowIndex, highIndex, pivot);

        recursiveQuickSort(arr, lowIndex, pivotIndex - 1);
        recursiveQuickSort(arr, pivotIndex + 1, highIndex);
    }

    private static int partition(int[] arr, int lowIndex, int highIndex, int pivot) {

        while (lowIndex < highIndex) {

            while (lowIndex < highIndex && arr[highIndex] >= pivot) {
                highIndex--;
            }

            swap(arr, lowIndex, highIndex);

            while (lowIndex < highIndex && arr[lowIndex] <= pivot) {
                lowIndex++;
            }

            swap(arr, lowIndex, highIndex);
        }

        return lowIndex;
    }

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

No3>InsertSort

package SortAlgorithm;

import java.util.Arrays;

public class TestInsert {
    public static void main(String[] args) {
        int[] arr = {5, 6, 8, 0, 4, 5, 3};
        insertSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    private static void insertSort(int[] arr) {

//        方法一
//        int temp = 0;
//        for (int i = 1; i < arr.length; i++) {
//            for (int j = i - 1; j >= 0; j--) {
//                if (arr[i] < arr[j]) {
//                    temp = arr[i];
//                    while (i != j) {
//                        arr[i] = arr[--i];
//                    }
//                    arr[i] = temp;
//                }
//            }
//        }

        int temp = 0;
        int tempIndex = 0;

        for (int i = 1; i < arr.length; i++) {
            temp = arr[i];
            tempIndex = i - 1;

            //若 while (tempIndex >= 0 && arr[tempIndex] < temp) 此時從大到小
            while (tempIndex >= 0 && arr[tempIndex] > temp) {
                arr[tempIndex + 1] = arr[tempIndex--];
                //相當於以下兩步
                //arr[tempIndex+1] = arr[tempIndex];
                //tempIndex--;
            }

            if(tempIndex+1 != i){
                arr[tempIndex + 1] = temp;
            }
        }
    }
}

No4>ShellSort

package SortAlgorithm;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

//希爾排序 (extends 插入)
public class TestShell {
    public static void main(String[] args) {
        int[] arr = {8, 9, 1, 7, 2, 3, 5, 4, 6, 0};
        shellSort(arr);
        System.out.println();
        int[] arr2 = {8, 9, 1, 7, 2, 3, 5, 4, 6, 0};
        shellSort2(arr2);

        //消耗時間測試
        Date dateStart =new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strStart = dateFormat.format(dateStart);
        System.out.println("Start:"+strStart);

        Date dateEnd =new Date();
        String strEnd = dateFormat.format(dateEnd);
        System.out.println("Start:"+strEnd);

    }

    //交換法 速度很慢
    private static void shellSort(int[] arr) {

        int temp = 0;
        int count = 0;
        for (int gap = arr.length / 2; gap > 0; gap /= 2) {
            for (int i = gap; i < arr.length; i++) {
                for (int j = i - gap; j >= 0; j -= gap) {
                    if (arr[j] > arr[j + gap]) {
                        temp = arr[j];
                        arr[j] = arr[j + gap];
                        arr[j + gap] = temp;
                    }
                }
            }
            System.out.println("The count order is:" + Arrays.toString(arr));
        }
    }

    //位移法,直接插入法
    private static void shellSort2(int[] arr){
        int cursor = 0;
        int min = 0;
        for (int gap = arr.length / 2; gap > 0; gap /= 2){
            for (int i = gap; i < arr.length; i++){

                cursor = i;
                min = arr[i];

                while(cursor-gap >=0 && min<arr[cursor-gap]){
                    arr[cursor] = arr[cursor - gap];
                    cursor -= gap;
                }

                if(cursor!=i){
                    arr[cursor] = min;
                }
            }
            System.out.println("The count order is:" + Arrays.toString(arr));
        }
    }

    public void timeConsume( ){

    }
}
No5>SelectSort

package SortAlgorithm;

import java.util.Arrays;

//選擇排序 (extends 選擇排序)
public class TestSelect {
    public static void main(String[] args) {
        int[] arr = {5, 6, 7, 8, 3, 2, 6, 9};
        selectSort(arr);
        System.out.println(Arrays.toString(arr));
//        for (int i:arr){
//            System.out.print(i+" ");
//        }
    }

    private static void selectSort(int[] arr) {

        int minIndex = 0;
        int swapTemp = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            if (minIndex != i) {
                swapTemp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = swapTemp;
            }
        }
    }
}

No6>HeapSort

package SortAlgorithm;

import java.util.Arrays;

public class TestHeap {
    public static void main(String[] args) {
        int[] arr = {4, 6, 8, 5, 9};
        heapSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void heapSort(int[] arr) {
        System.out.println("heap sort");

        int temp = 0;
        for (int i = arr.length / 2 - 1; i >= 0; i--) {
            adjustHeap(arr, i, arr.length);
        }

        //交換,最後一個數的位置自然確定
        for (int i = arr.length - 1; i > 0; i--) {
            temp = arr[i];
            arr[i] = arr[0];
            arr[0] = temp;
            adjustHeap(arr, 0, i);
        }

        //打印結果
//        System.out.println(Arrays.toString(arr));
    }

    /*
     * 以 i 對應的非葉子節點的樹調整成大頂堆
     * i 非葉子節點在數組中的索引
     * length 元素個數下界
     * */
    public static void adjustHeap(int[] arr, int i, int length) {
        int temp = arr[i];

        for (int k = i * 2 + 1; k < length; k = k * 2 + 1) {
            if (k + 1 < length && arr[k] < arr[k + 1]) {
                k++;
            }

            if (arr[k] > temp) {
                arr[i] = arr[k];
                i = k;
            } else {
                break;
            }
        }

        //for循環結束後 已經將i 爲父節點的樹的最大值放在了頂部(局部)
        arr[i] = temp;
    }
}

No7>MergeSort

package SortAlgorithm;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

//歸併排序
public class TestMerge {
    public static void main(String[] args) {
        int[] arr = {5, 6, 8, 0, 4, 5, 3};
        int[] arr2 = new int[8000];
        for (int i = 0; i < 8000; i++) {
            arr2[i] = (int) (Math.random()*8000);
        }
        mergeSort(arr);
        System.out.println(Arrays.toString(arr));

        Date dateStart =new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strStart = dateFormat.format(dateStart);
        System.out.println("Start:"+strStart);

        mergeSort(arr2);

        Date dateEnd =new Date();
        String strEnd = dateFormat.format(dateEnd);
        System.out.println("Start:"+strEnd);

    }

    private static void mergeSort(int[] arr) {

        recursiveMergeSort(arr, 0, arr.length - 1);

    }

    private static void recursiveMergeSort(int[] arr, int lowIndex, int highIndex) {

        if (lowIndex >= highIndex) {
            return;
        }

        int mid = (lowIndex + highIndex) / 2;
        recursiveMergeSort(arr, lowIndex, mid);
        recursiveMergeSort(arr, mid + 1, highIndex);
        merge(arr, lowIndex, highIndex, mid);
    }

    private static void merge(int[] arr, int lowIndex, int highIndex, int mid) {
        int lowBegin = lowIndex;
        int lowEnd = mid;

        int highBegin = mid + 1;
        int highEnd = highIndex;

        int[] supportArr = new int[highIndex - lowIndex + 1];

        int tempIndex = 0;

        while (lowBegin <= lowEnd && highBegin <= highEnd) {

            if (arr[lowBegin] < arr[highBegin]) {
                supportArr[tempIndex++] = arr[lowBegin++];
            } else {
                supportArr[tempIndex++] = arr[highBegin++];
            }
        }
        while (lowBegin <= lowEnd) {
            supportArr[tempIndex++] = arr[lowBegin++];
        }

        while (highBegin <= highEnd) {
            supportArr[tempIndex++] = arr[highBegin++];
        }

        for (int i = 0; i < tempIndex; i++) {
            arr[lowIndex++] = supportArr[i];

        }

    }

}

No8>RadixSort

package SortAlgorithm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//基數排序
public class TestRadix {

    public static void main(String[] args) {
        int[] arr = {5, 6, 27, 8, 3, 92, 6, 9};
        radixSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    private static void radixSort(int[] arr){

        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if(arr[i]>max){
                max = arr[i];
            }
        }

        int turn = 0;
        while(max>0){
            max /= 10;
            turn++;
        }

        List<ArrayList<Integer>> bucketDouble = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            bucketDouble.add (new ArrayList<>());
        }

        for (int i = 0; i < turn; i++) {
            for (int j = 0; j < arr.length; j++) {
                int key = arr[j] % (int)Math.pow(10,i+1) / (int)Math.pow(10,i);
                bucketDouble.get(key).add(arr[j]);
            }

            int  count = 0;
            for (int j = 0; j < 10; j++) {
                ArrayList<Integer> b = bucketDouble.get(j);
                while(b.size() > 0){
                    arr[count++] = b.remove(0);
                }
            }
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章