[算法]冒泡+选择+插入排序+java

package com.dongshuo.test.arithmetic;

/**
 * @author dongshuo
 * @data 2018/7/20 17:05
 * @Description
 */
public class XuanZeMaopao {
    private static int[] array = {12,53,1,43,6,8,2,9,12,6,32};
    //由小到大排序  选择排序
    public static void sort(){
        int size = array.length;
        int temp = 0;
        for (int i = 0;i <size-1;i++) {
            for (int j = i+1;j <=size -1;j++) {
                if (array[i] > array[j]) {
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }
    }
    //从小到大冒泡排序
    public static void maopao() {
        int size = array.length;
        int temp = 0;
        for (int i = 0;i<size-1;i++) {
            for (int j = 0;j < size -1-i;j++) {
                if (array[j] >array[j+1]) {
                    temp = array[j+1];
                    array[j+1] = array[j];
                    array[j] = temp;
                }
            }
        }
    }
    //插入排序由小到大
    public static void charu() {
        int temp;
        for (int i = 1; i < array.length; i++) {
            int j = i;
            while(j > 0 && array[j]<array[j-1]) {
                temp = array[j-1];
                array[j-1] = array[j];
                array[j] = temp;
                j--;
            }
        }
    }

    public static void main(String[] args) {
        for (int i : array) {
            System.out.print(i+" ");
        }
        System.out.println("排序后");
        //sort();
        //maopao();
        charu();
        for (int i : array) {
            System.out.print(i+" ");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/drdongshiye/article/details/81190193