第二天学习笔记之简单排序(冒泡排序、选择排序、插入排序)

public class demo {
    public static void main(String[] args) {
        //数组长度 方便集成测试
        int length = 10000;
        //生成随机数long测试 边界1--1000
        long min = 1;
        long max = 1000000;

        BatSort batSort = new BatSort(length);
        for (int i = 0; i < length; i++) {
            batSort.insert(min + (long) (new Random().nextDouble() * (max - min)));
        }
        batSort.display();
        long start,end;
        start = System.currentTimeMillis();
        //batSort.bubbleSort();
        batSort.selectSort();
        //batSort.insertSort();
        end = System.currentTimeMillis();
        System.out.println("Run Time:" + (end - start) + "(ms)");
        batSort.display();
    }
}
public class BatSort {
    private long[] arr ;
    private int elements = 0;

    public BatSort(int arraylength){
        if (arraylength > 0){
            arr = new long[arraylength];
        }else{
            arr = new long[100];
        }
    }

    public void insert(long value){
        arr[elements] = value;
        elements++;
    }
    /*冒泡排序思想  把最小的数据依次冒泡最前面 举例 5 2 4 1 3
     * 第一次排序 1和3做比较不变
     * 第二次排序 4和1做比较 变成 5 2 1 4 3
     * 第三次排序 2和1做比较 变成 5 1 2 4 3
     * 第四次排序 5和1做比较 变成 1 5 2 4
     * 范围循环排序依次类推
     */
    public void bubbleSort(){
        long temp = 0;
        for (int i=0;i<elements;i++){
            for(int j=arr.length -1;j>i;j--){
                if(arr[j] < arr[j-1]){
                    temp=arr[j-1];
                    arr[j-1]=arr[j];
                    arr[j]=temp;
                }
            }
        }
    }

    /*选择排序思想 每次排序把最小的元素放在最前面即和待排序的位置对调 举例 5 2 4 1 3
     * 第一次排序 1 2 4 5 3  5和1调换位置
     * 第二次排序 1 2 4 5 3
     * 第三次排序 1 2 3 5 4
     * 第四次排序 1 2 3 4 5
     */
    public void selectSort(){
        long temp = 0;
        long min = 0;
        int index = 0;
        for(int i=0;i<elements;i++){
            min = arr[i];
            for(int j=elements-1;j>i;j--){
                if(min > arr[j]){
                    min = arr[j];
                    index = j;
                }
            }
            if(arr[i] > min){
                temp = arr[i];
                arr[i] = min;
                arr[index] = temp;
            }
        }
    }

    /*插入排序思想 取其中一位作为分割位,跟左边的值比较,如果大于就停止 举例 5 4 3 2 1
     * 第一次排序 4 5 3 2 1   2和5调换位置
     * 第二次排序 4 3 5 2 1   4和5调换位置
     *           3 4 5 2 1   3和4调换位置
     * 第三次排序 3 4 2 5 1   5和2调换位置
     * 依次类推
     */
    public void insertSort(){
        long temp = 0;
        int index = 0;
        for(int i=1;i<elements;i++){
            temp = arr[i];
            index = i - 1;
            while (index>=0&&arr[index] > temp){
                arr[index+1] = arr[index];
                index--;
            }
            arr[index+1] = temp;
        }
    }

    public void display(){
        for(int i=0;i<elements;i++){
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
}

猜你喜欢

转载自www.cnblogs.com/mutong1228/p/10235214.html
今日推荐