• 简单排序——冒泡,选择,插入


        // 冒泡排序 每一次内循环将相邻的元素进行比较交换,直到所有元素比较完毕
        public static void bubbleSort(int arr[]) {
            int temp;
            for (int i = 0; i < arr.length - 1; i++) {
                for (int j = 0; j < arr.length - i - 1; j++) {
                    if (arr[j] > arr[j + 1]) {
                        temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
            }
        }
    
        // 选择排序 优化后的冒泡排序,
        // 内循环进行比较找出最大或最小元素的坐标;在内循环结束后进行交换
        // 相对冒泡排序来说,优化了内存使用空间
        public static void selectSort(int[] arr) {
            int minBound, temp;
            for (int i = 0; i < arr.length - 1; i++) {
                minBound = i;
                for (int j = i + 1; j < arr.length - 1; j++) {
                    if (arr[j] < arr[minBound]) {
                        minBound = j; // 内循环只负责找出最小或最大值的坐标
                    }
                }
                temp = arr[i]; // 循环结束后进行交换
                arr[i] = arr[minBound];
                arr[minBound] = temp;
            }
        }
    
        /**
         * 插入排序 选定一个顺序。从左至右 或是从右到左, 默认他们已经是有序的数据 在判断数据时 进行交换位置 实现排序功能
         */
        public static void insertSort(int[] arr) {
            int temp;
            for (int i = 1; i < arr.length; i++) {
                for (int j = i; j > 0; j--) {
                    if (arr[j] < arr[j - 1]) {
                        temp = arr[j];
                        arr[j] = arr[j - 1];
                        arr[j - 1] = temp;
                    }
                }
            }
    
        }

     附插入排序模拟手稿

     

  • 相关阅读:
    (floyd+DP) zoj 3027
    (树形DP) acdream 1028
    acdream 1032
    (BFS) acdream 1191
    (树形DP) bzoj 1060
    (状态压缩DP) poj 2978
    (DP) codeforces 358D
    (DP+二分) hdu 3433
    (最大生成树) hdu 3367
    hdoj 3501
  • 原文地址:https://www.cnblogs.com/shenwenbo/p/8306881.html
Copyright © 2020-2023  润新知