Loading

排序

冒泡排序

image

package com.company;

import java.util.Arrays;

/**
 * @author :      Victor
 * @className :   TestMaoPao
 * @Date :        2021-04-04 15:20
 * @deprecated : 冒泡排序
 */
public class TestMaoPao {

    public static void main(String[] args) {

        int[] arr = {1, 2, 3, 4, 5, 7, 100, 87, 99, 123};
        int[] array = bubbleSort(arr);
        System.out.println(Arrays.toString(array));
    }

    public static int[] bubbleSort(int[] arr) {

        int count = 0;

        for (int i = 0; i < arr.length - 1; i++) {
            boolean flag = false;
            //这个循环保证了每两个数,前者要小于后者
            for (int j = 0; j < arr.length - 1 - i; j++) {

                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];

                    arr[j] = arr[j + 1];

                    arr[j + 1] = temp;

                    flag = true;
                }
            }
            count++;
            if (!flag) {
                break;
            }

        }
        return add(arr, count);
    }


    static int[] add(int[] arr, int value) {
        int[] arrs = new int[arr.length + 1];
        int index = 0;
        for (int j : arr) {
            arrs[index] = j;
            index++;
        }
        arrs[arrs.length - 1] = value;

        return arrs;
    }


}

快速排序

package com.company;

import java.util.Arrays;

/**
 * @author :      Victor
 * @className :   TestQuick
 * @description : 快速排序
 * @Date :        2021-04-06 10:39
 */
public class TestQuick {
    public static void main(String[] args) {

        int[] arr = {100, 34, 23, 67, 12, 45, 42, 45, 34};
        quickSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    private static void quickSort(int[] arr) {
        if (null == arr || arr.length == 0) {
            return;
        }
        doSort(arr, 0, arr.length - 1);


    }

    private static void doSort(int[] arr, int low, int high) {
        if (low < high) {
            //将数组分为两部分
            int index = partition(arr, low, high);
            //递归左子
            doSort(arr, low, index - 1);
            //递归右子
            doSort(arr, index + 1, high);


        }
    }

    private static int partition(int[] arr, int low, int high) {
        int pivot = arr[low];
        while (low < high) {
            while (low < high && arr[high] >= pivot) --high;

            arr[low] = arr[high];

            while (low < high && arr[low] <= pivot) low++;

            arr[high] = arr[low];

        }
        arr[low] = pivot;
        return low;
    }


}

选择排序

image

/**
     * 选择排序
     * @param array
     * @return
     */
    public static int[] selectionSort(int[] array){
        if(array.length > 0){
            for(int i = 0;i<array.length;i++){
                int minIndex = i;
                for(int j = i;j<array.length;j++){//遍历未剩余未排序元素中继续寻找最小元素
                    if(array[j] < array[minIndex]){
                        minIndex = j;
                    }
                }
                if(minIndex != i){
                    int temp = array[minIndex];
                    array[minIndex] = array[i];
                    array[i] = temp;
                }
            }
        }
        return array;
    }

posted @ 2021-04-04 17:56  黑鱼非鱼  阅读(29)  评论(0)    收藏  举报