zoukankan      html  css  js  c++  java
  • 冒泡排序选择排序插入排序

    在这里插入图片描述

    package com.smile.test.sort.bubble;
    
    /**
     * 冒泡排序  时间复杂度O(n^2)
     */
    
    public class Bubble {
        static void sort(Comparable[] a){
            for (int i = a.length-1; i>0; i--){
                for (int j=0; j<i; j++){
                    if(greater(a[j],a[j+1])){
                        changeIndex(a, j, j+1);
                    }
                }
    
            }
        }
        private static boolean greater(Comparable a,Comparable b){
            return a.compareTo(b) > 0;
        }
        private static void changeIndex(Comparable[] a,int i,int j){
            Comparable temp;
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
    
    import java.util.Arrays;
    
    public class BubbleTest {
        public static void main(String[] args) {
            Integer[] a = {6,5,4,3,2,1};
            Bubble.sort(a);
            System.out.println(Arrays.toString(a));
        }
    }
    
    
    [1, 2, 3, 4, 5, 6]
    
    Process finished with exit code 0
    

    选择排序

    在这里插入图片描述

    public class Selections {
        static void sort(Comparable[] a){
            for (int i = 0; i <= a.length - 2; i++){
                int minIndex = i;
                for (int j = i+1; j < a.length; j++){
                    if (greater(a[minIndex], a[j])){
                        minIndex = j;
                    }
                }
                changeIndex(a, minIndex,i);
            }
        }
        // 比较a,b的大小
        private static boolean greater(Comparable a,Comparable b){
            return a.compareTo(b) > 0;
        }
        // 交换索引i 和 索引j处的值
        private static void changeIndex(Comparable[] a,int i,int j){
            Comparable temp;
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
    
    import java.util.Arrays;
    
    public class SelectionsTest {
        public static void main(String[] args) {
            Integer[] arr = {4,5,8,9,6,3,1};
            Selections.sort(arr);
            System.out.println(Arrays.toString(arr));
        }
    }
    
    [1, 3, 4, 5, 6, 8, 9]
    
    Process finished with exit code 0
    

    插入排序

    在这里插入图片描述

    public class InsertSort {
        public static void sort(Comparable[] a){
            for (int i = 1; i < a.length; i++){
                for(int j = i; j>0; j--){
                    if (greater(a[j-1], a[j])){
                        changeIndex(a,j-1,j);
                    }
                }
            }
        }
        // 比较a,b的大小
        private static boolean greater(Comparable a,Comparable b){
            return a.compareTo(b) > 0;
        }
        // 交换索引i 和 索引j处的值
        private static void changeIndex(Comparable[] a,int i,int j){
            Comparable temp;
            temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
    
    import java.util.Arrays;
    
    public class InsertTest {
        public static void main(String[] args) {
            Integer[] a = {4,3,2,10,12,1,5,6};
            InsertSort.sort(a);
            System.out.println(Arrays.toString(a));
        }
    }
    
    [1, 2, 3, 4, 5, 6, 10, 12]
    
    Process finished with exit code 0
    
  • 相关阅读:
    C#GridViewExport帮助类,美化导出
    C#EXCEL 操作类--C#DataToExcel帮助类
    C#EXCEL 操作类--C#ExcelHelper操作类
    VS2010 项目引用了DLL文件,也写了Using,但是编译时提示:未能找到类型或命名空间名称 <转>
    FPGA Verilog HDL 系列实例--------步进电机驱动控制
    C++使用VARIANT实现二维数组的操作
    VS2010+VMWare8+VisualDDK1.5.6 创建并调试你的第一个驱动程序
    USB编程研究之二(常见设备类型的GUID)
    C++——CString用法大全
    C++ 如何有效地使用对话框
  • 原文地址:https://www.cnblogs.com/lvzl/p/14664414.html
Copyright © 2011-2022 走看看