关于几种排序算法的时间复杂度以及Java实现

1、冒泡排序

  • 最优的时间复杂度为:O( n) ;
  • 最差的时间复杂度为:O( n^2 );
  • 平均的时间复杂度为:O( n^2 );
  • package AC;
    import java.util.Arrays;
    
    public class maopao {
        public static void main(String []args){
            int []a = {11,12,762,42,13,43,323,-1,0,24};
            maopao1(a);
            Arrays.sort(a);
            System.out.println(Arrays.toString(a));
        }
        public static void maopao1(int[]a){
            for(int i = 0;i<a.length;i++){
                for(int j = 0;j<a.length-i-1;j++){
                    if(a[j]>a[j+1]){
                        int temp = a[j];
                        a[j] = a[j+1];
                        a[j+1] = temp;
                    }
                    System.out.println(Arrays.toString(a));
                }
            }
        }
    }
    

2、插入排序

  • 最优的时间复杂度为:O( n ) ;
  • 最差的时间复杂度为:O( n^2 );
  • 平均的时间复杂度为:O( n^2 );
  • package AC;
    import java.util.Arrays;
    
    public class charu {
        public static void main(String []args){
            int []a = {11,12,762,42,13,43,323,-1,0,24};
            charu1(a);
            Arrays.sort(a);
            System.out.println(Arrays.toString(a));
        }
        public static void charu1(int []a){
            for(int i = 0;i<a.length-1;i++){
                for(int j = 0;j<i;j++){
                    if(a[j]>a[i+1]){
                        int temp = a[j];
                        a[j] = a[i+1];
                        a[i+1] = temp;
                    }
                }
                System.out.println(Arrays.toString(a));
            }
        }
    }
    

3、选择排序

  • 最优的时间复杂度为:O( n ) ;
  • 最差的时间复杂度为:O( n^2 );
  • 平均的时间复杂度为:O( n^2 );
  • package AC;
    import java.util.Arrays;
    
    public class xuanze {
            public static void main(String []args){
            int []a = {11,12,762,42,13,43,323,-1,0,24};
            xuanze1(a);
            Arrays.sort(a);
            System.out.println(Arrays.toString(a));
        }
        public static void xuanze1(int []a){
            
            for(int i = 0;i<a.length;i++){
                for(int j = i+1;j<a.length;j++){
                    int min = a[i];
                    int idex = i;
                    if(a[j]<min){
                        idex = j;
                        min = a[j];
                    }
                    a[idex] = a[i];
                    a[i] = min;
                }System.out.println(Arrays.toString(a));
            }
            
        }
    }
    

4、希尔排序

  • 最优的时间复杂度为:O( n*log( n)) ;
  • 最差的时间复杂度为:O( n*log( n) ^2 );
  • 平均的时间复杂度为:O(n*log( n)^2 );
  • package AC;
    import java.util.Arrays;
    
    public class shell {
        public static void main(String []args){
            int []a = {11,12,762,42,13,43,323,-1,0,24};
            shell1(a);
            Arrays.sort(a);
            System.out.println(Arrays.toString(a));
        }
        public static void shell1(int []a){
            int buchang = a.length/2;
            while(buchang!=0){
                for(int i = 0;i<a.length-buchang;i++){
                    int j = i+buchang;
                    while(j<a.length){
                        if(a[i]>a[j]){
                            int temp = a[j];
                            a[j] = a[i];
                            a[i] = temp;
                        }
                        j = j+buchang;
                    }
                }
                buchang = buchang/2;
                System.out.println(Arrays.toString(a));
            }
        }
        // public static void charu(int []a){
        //     for(int i = 0;i < a.length;i++){
        //         for(int j = 0;j<i;j++){
        //             if(a[j]>a[i]){
        //                 int temp = a[j];
        //                 a[j] = a[i];
        //                 a[i] = temp;
        //             }
        //         }
                
        //     }
        // }
    }
    

5、基数排序

  • 最优的时间复杂度为:O( n*k ) ;
  • 最差的时间复杂度为:O( n*k  );
  • 平均的时间复杂度为:O(n*k  );
  • package AC;
    import java.util.Arrays;
    
    public class jishu {
            public static void main(String []args){
            int []a = {28,34,30,26,36,27,28,20,22};
            jishu1(a);
            Arrays.sort(a);
            System.out.println(Arrays.toString(a));
        }
        public static void jishu1(int []a){
            int min = a[0];
            int max = a[0];
            for(int i = 0;i<a.length;i++){
                if(a[i]>max) max = a[i];
                if(a[i]<min) min = a[i];
            }
            int []new1 = new int[max-min+1];
            for(int i = 0;i<a.length;i++){
                new1[a[i]-min]++;
            }
            System.out.println(Arrays.toString(new1));
            int []new2 = new int[max-min+1];
            new2[0] = new1[0];
            for(int i = 0;i<max-min;i++){
                new2[i+1] = new2[i]+new1[i+1];
            }
                    System.out.println(Arrays.toString(new2));
        }
    }
    

6、对象排序

  • 最优的时间复杂度为:O( n) ;
  • 最差的时间复杂度为:O( n^2 );
  • 平均的时间复杂度为:O( n^2 );
  • package AC;
    import java.lang.reflect.Array;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class duixiangpaixu {
        public static void main(String []args){
            ArrayList<student> studenList = new ArrayList<student>();
            studenList.add(new student("张三",100,59));
            studenList.add(new student("李四",59,100));
            studenList.add(new student("王五",98,96));
            studenList.add(new student("赵六",31,59));
            studenList.add(new student("陈七",59,31));
            Collections.sort(studenList);
            for(int i = 0;i<studenList.size();i++){
                System.out.println(studenList.get(i));
            }
        }
        static class student implements Comparable<student>{
            private String studentname;
            private int java;
            private int SQL;
            private int scoure;
            public student(){}
            public student(String studentname,int java,int SQL){
                this.studentname = studentname;
                this.java = java;
                this.SQL = SQL;
                this.scoure =java+SQL;
            }
    
            public String toString(){
                return "studentname:"+studentname+"\t java"+java+"\t SQL"+SQL+"\t scoure"+scoure+"\n";
            }
    
            @Override
            public int compareTo(duixiangpaixu.student o) {
    
                if(this.scoure==o.scoure) return this.java-o.java;
                else return this.scoure-o.scoure;
            }
            
        }
    }
    

7、堆排序

  • 最优的时间复杂度为:O( n*log( n) ) ;
  • 最差的时间复杂度为:O( n*log( n) );
  • 平均的时间复杂度为:O( n*log( n) );
    package AC;
    import java.lang.reflect.Array;
    import java.util.Arrays;
    
    public class duipaixu {
        public static void main(String []args){
            int [] a = {57,68,59,52,72,28,96,33,24};
            for(int length = a.length-1;length>=0;length--){
                Myduipaixu(a, length);
                int temp = a[length];
                a[length] = a[0];
                a[0] = temp;
                System.out.println(Arrays.toString(a));
    }
        
        }
        private static void Myduipaixu(int []a,int length){
            for(int i= (length-1)/2-1;i>=0;i--){
                int max = i;
                if(a[i*2+1]>a[i*2+2]){
                    max = i*2+1;
                }
                else{
                    max = i*2+2;
                }
                if(a[max]>a[i]){
                    int temp = a[max];
                    a[max] = a[i];
                    a[i] = temp;
                    Myduipaixu(a, max);
                }
            }
        }
    }
    

8、归并排序

  • 最优的时间复杂度为:O( n*log( n)) ;
  • 最差的时间复杂度为:O( n*log( n) );
  • 平均的时间复杂度为:O( n*log( n) );
package AC;
import java.util.Arrays;

public class guiBing {
    public  static void main(String []args){
        int []a = {11,12,762,42,13,43,323,-1,0,24};
        guibing1(a, 0, a.length-1);
    }
    public static void guibing1(int []a,int left,int right){
        int mid = (left+right)/2;
        if(right>left){
            guibing1(a, left, mid);
            guibing1(a, mid+1, right);
            hebing(a,left,mid,right);
            System.out.println(Arrays.toString(a));;
    }
}
    private static void hebing(int []a, int left, int mid, int right) {
            int temp []  = new int[right-left+1];
            int jishu = 0;
            int jiaobiao1 = left;
            int jiaobiao2 = mid+1;
            while(jiaobiao1<=mid&&jiaobiao2<=right){
                if(a[jiaobiao1]<a[jiaobiao2]) temp[jishu++]=a[jiaobiao1++];
                if(a[jiaobiao1]>a[jiaobiao2]) temp[jishu++]=a[jiaobiao2++];
            }
            while(jiaobiao1<=mid) temp[jishu++]=a[jiaobiao1++];
            while(jiaobiao2<=right) temp[jishu++]=a[jiaobiao2++];
            for(int i =0;i<right-left+1;i++){
                a[left+i] = temp[i]; 
            }
    }
  
}

9、快速排序

  • 最优的时间复杂度为:O( n*log( n)) ;
  • 最差的时间复杂度为:O( n^2 );
  • 平均的时间复杂度为:O(n*log( n) );
package AC;
import java.util.Arrays;

public class kuaisupaixu {
    public  static void main(String []args){
        int []a = {11,12,762,42,13,43,323,-1,0,24};
        int len = a.length-1;
        kuaisupaixu1(a,0,len);
    }

    private static void kuaisupaixu1(int[] a, int left, int right) {
            int paixushuzi = a[left];
            int left1 = left;
            int right1 = right;
            System.out.println(Arrays.toString(a));
            while(right>left){
                for(int i = right;i>left;i--){
                if(a[i]<paixushuzi){
                    a[left] = a[i];
                    a[i] = paixushuzi;
                    right = i;
                    left = left+1;
                    break;
                }else{
                    right--;
                }
            }
            for(int i = left;i<right;i++){
                if(a[i]>paixushuzi){
                    a[right] = a[i];
                    a[i] = paixushuzi;
                    left = i;
                    right = right-1;
                    break;
                }else{
                    left++;
                }
            }
        }
      //判断是否只有一个元素,递归调用
            if(left-1>left1)
            {
                kuaisupaixu1(a, left1, left-1);
            }
            if(right1>left+1)
            {
                kuaisupaixu1(a, left+1, right1);
            }
        
    }
}

  • 如上,若有错误,还望指正。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值