public class SortMethod {
    //1. 冒泡排序法:两两比较,把小的放在左边,大的放在右边!冒出的的数!
    public int[] bubbleSort(){
        int[] arr = {1,5,5,4,7,9,3,2,5,2,0};
        System.out.println("排序前:");
        int temp = 0;
        for(int m = 0;m< arr.length;m++){
            System.out.print(arr[m]+",");
        }
        for (int i = 0;i < arr.length;i++){
            for (int j = 0;j < arr.length - i - 1;j++){
                 temp = arr[j+1];
                if(arr[j] > arr[j+1]){
                    arr[j+1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        System.out.println("\n"+"排序后:");
        for(int m = 0;m<arr.length;m++){
            System.out.print(arr[m]+",");
        }
        return arr;
    }
    //选择排序法:在剩下【右边未排序】的集合中找到最小的一个放在左测已经排好的序列的最后面!记住最小的下标!
    public int[] selectSort(){
        int[] arr = {6,1,5,4,7,0,9,3,2,5,2};
        System.out.println("排序前:");
        int temp = 0;
        int minIndex = 0;
        for(int m = 0;m< arr.length;m++){
            System.out.print(arr[m]+",");
        }
        for (int i = 0;i < arr.length;i++){
            temp = arr[i];
            minIndex = i;
            for (int j = i;j < arr.length;j++){
                if(arr[j] < temp){
                    temp = arr[j];
                    minIndex = j;
                }
            }
            arr[minIndex] = arr[i];
            arr[i] = temp;

        }
        System.out.println("\n"+"排序后:");
        for(int m = 0;m<arr.length;m++){
            System.out.print(arr[m]+",");
        }
        return arr;
    }
    //插入排序法:用一个while来倒插! --
    public int[] insertSort(){
        int[] arr = {6,1,5,4,7,0,9,3,2,5,2};
        System.out.println("排序前:");
        int temp = 0;
        int preIndex = 0;
        for(int m = 0;m< arr.length;m++){
            System.out.print(arr[m]+",");
        }
        for (int i = 0;i < arr.length -1;i++){
            temp = arr[i+1];
            preIndex = i;
            while (preIndex >=0 && temp < arr[preIndex]){
                arr[preIndex+1] = arr[preIndex];
                preIndex--;
            }
            arr[preIndex+1] = temp;

        }
        System.out.println("\n"+"排序后:");
        for(int m = 0;m<arr.length;m++){
            System.out.print(arr[m]+",");
        }
        return arr;
    }


}
02-13 01:12