• Bubble Sort


    向大端冒泡排序

    public class Bubble {
        /**
         * 向大端冒泡排序
         *
         * @param arr
         * @param <T>
         */
        public static <T extends Comparable<? super T>> void bubbleSort(T[] arr) {
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr.length - i - 1; j++) {
                    if (arr[j].compareTo(arr[j + 1]) > 0) {
                        swap(arr, j, j + 1);
                    }
                }
            }
        }
    
        public static void swap(Object[] arr, int i, int j) {
            Object temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    
        public static <T extends Comparable<? super T>> void printArray(T[] arr) {
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }
            System.out.println();
        }
    
        public static void main(String[] args) {
            Integer[] arr = {1, 5, 9, 7, 3, 10, 2, 8, 6, 4};
            bubbleSort(arr);
            printArray(arr);
        }
    }
    

      

    向小端冒泡排序

    public class Bubble {
        /**
         * 向小端冒泡排序
         *
         * @param arr
         * @param <T>
         */
        public static <T extends Comparable<? super T>> void bubbleSort(T[] arr) {
            for (int i = 0; i < arr.length; i++) {
                for (int j = arr.length - 1; j > 0; j--) {
                    if (arr[j].compareTo(arr[j - 1]) < 0) {
                        swap(arr, j - 1, j);
                    }
                }
            }
        }
    
        public static void swap(Object[] arr, int i, int j) {
            Object temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    
        public static <T extends Comparable<? super T>> void printArray(T[] arr) {
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }
            System.out.println();
        }
    
        public static void main(String[] args) {
            Integer[] arr = {1, 5, 9, 7, 3, 10, 2, 8, 6, 4};
            bubbleSort(arr);
            printArray(arr);
        }
    }
    

      

    鸡尾酒排序

    public class Bubble {
        /**
         * 鸡尾酒排序
         *
         * @param arr
         * @param <T>
         */
        public static <T extends Comparable<? super T>> void bubbleSort(T[] arr) {
            int low = 0;
            int high = arr.length - 1;
            while (low < high) {
                for (int i = low; i < high; i++) {
                    if (arr[i].compareTo(arr[i + 1]) > 0) {
                        swap(arr, i, i + 1);
                    }
                }
                high--;
                for (int j = high; j > low; j--) {
                    if (arr[j].compareTo(arr[j - 1]) < 0) {
                        swap(arr, j - 1, j);
                    }
                }
                low++;
            }
        }
    
        public static void swap(Object[] arr, int i, int j) {
            Object temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    
        public static <T extends Comparable<? super T>> void printArray(T[] arr) {
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }
            System.out.println();
        }
    
        public static void main(String[] args) {
            Integer[] arr = {5, 1, 9, 7, 3, 10, 2, 8, 6, 4};
            bubbleSort(arr);
            printArray(arr);
        }
    }  

    冒泡排序优化--标识符

    public class Bubble {
        /**
         * 冒泡排序优化--标记符
         *
         * @param arr
         * @param <T>
         */
        public static <T extends Comparable<? super T>> void bubbleSort(T[] arr) {
            for (int i = 0; i < arr.length; i++) {
                boolean flag = true;
                for (int j = 0; j < arr.length - i - 1; j++) {
                    if (arr[j].compareTo(arr[j + 1]) > 0) {
                        swap(arr, j, j + 1);
                        flag = false; //这层循环有交换
                    }
                }
                if (flag) {//可以退出循环了,已有序
                    break;
                }
            }
        }
    
        public static void swap(Object[] arr, int i, int j) {
            Object temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    
        public static <T extends Comparable<? super T>> void printArray(T[] arr) {
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }
            System.out.println();
        }
    
        public static void main(String[] args) {
            Integer[] arr = {5, 1, 9, 7, 3, 10, 2, 8, 6, 4};
            bubbleSort(arr);
            printArray(arr);
        }
    }

    冒泡排序优化--标记位

    public class Bubble {
        /**
         * 冒泡排序优化--标记符
         *
         * @param arr
         * @param <T>
         */
        public static <T extends Comparable<? super T>> void bubbleSort(T[] arr) {
            for (int i = 0, pos = 0; i < arr.length; i = arr.length - pos) {
                pos = 0;
                for (int j = 0; j < arr.length - i - 1; j++) {
                    if (arr[j].compareTo(arr[j + 1]) > 0) {
                        swap(arr, j, j + 1);
                        pos = j + 1;//说明pos后面的元素都已有序,不需要进行比较了
                    }
                }
            }
        }
    
        public static void swap(Object[] arr, int i, int j) {
            Object temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    
        public static <T extends Comparable<? super T>> void printArray(T[] arr) {
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }
            System.out.println();
        }
    
        public static void main(String[] args) {
            Integer[] arr = {5, 1, 9, 7, 3, 10, 2, 8, 6, 4};
            bubbleSort(arr);
            printArray(arr);
        }
    }
    

      

  • 相关阅读:
    【缺少kubernetes权限】 namespaces "xxx" is forbidden: User "xxx" cannot xxx resource "xxx" in API group "xxx" in the namespace "xxx"
    【kubernetes secret 和 aws ecr helper】kubernetes从docker拉取image,kubernetes docker私服认证(argo docker私服认证),no basic auth credentials错误解决
    win10老提示系统错误,要注销
    win10无法访问局域网共享文件?解决如此简单。。。。。
    filezilla server老提示connect server
    代理ARP
    win10用filezilla server搭建ftp服务器一直无法访问
    华为路由器GRE配置
    spring笔记--使用springAPI以及自定义类 实现AOP的一个例子
    spring笔记--依赖注入之针对不同类型变量的几种注入方式
  • 原文地址:https://www.cnblogs.com/Hangtutu/p/8022008.html
Copyright © 2020-2023  润新知