手记

算法(四)、时间复杂度与排序算法

算法(四)、时间复杂度与排序算法

一、时间复杂度

 1、 常数阶O(1)    

    package test;    public class CCC {        public static void main(String[] args) {            int sum=100;            int n=100;            sum = sum+n;            System.out.println(sum);        }    }

 2、线性阶O(n)

package test;    public class CCC {        public static void main(String[] args) {            int sum=0;            int n=100;            for(int i=1;i<=n;i++){                sum = sum+i;            }            System.out.println(sum);        }    }

 3、 对数阶O(Log2 n)

package test;public class CCC {    public static void main(String[] args) {        int n=100;        for(int i=1;i<=n;i++){            i=i*2;            System.out.println(i);        }    }}

4、 平方阶O(n2)
例1:

package test;public class CCC {    public static void main(String[] args) {        int sum=0;        int n=100;        for(int i=1;i<=n;i++){            for(int j=1;j<=n;j++){                sum = sum+i;            }        }        System.out.println(sum);    }}

例2:

package test;public class CCC {    public static void main(String[] args) {        int n=100;        for(int i=1;i<=n;i++){            for(int j=i;j<=n;j++){                System.out.println(j);            }        }    }}

当i=1时执行n次,i=2时执行n-1次,......当i=n-1时执行了1次,总共执行的次数为:

因此为时间复杂度为:O(n2)

5、 时间复杂对耗费时间由小到大排序:

二、常用算法

    总结了以下几种常用算法:找出最大值、冒泡排序、选择排序、快速排序、插入排序、希尔排序

2、遍历数组,找出数组中的最大值

public class Abc{    int[] b=new int[10];    void B(){        for(int i=0;i<b.length;i++){            b[i]=(int)(Math.random()*100);            System.out.println("数组的第"+i+"个值为:"+b[i]);        }    }    int max=b[0];    void A(){        for(int i=0;i<b.length;i++){            int c=b[i];            if(b[i]>=max){                max=b[i];            }        }        System.out.println("数组的最大值为:"+max);    }}

3、冒泡排序

import java.util.Arrays;public class Def {    public static void DDD(){        int[] d={15,6,3,89,54,34};        System.out.println("原始数组为:"+Arrays.toString(d));        for(int i=0;i<=d.length;i++){            for(int j=0;j<d.length-1-i;j++){                if(d[j]>d[j+1]){                    int t=d[j];                    d[j]=d[j+1];                    d[j+1]=t;                }            }        }        System.out.println("冒泡后数组为:"+Arrays.toString(d));    }}

4、选择排序

package test;public class CCC {     public static void main(String[] args) {                    int[] abc={1,76,54,100,65,34,2,19,40};                    System.out.print("快速排序前数组为:");                    for(int num:abc){                        System.out.print(num+" ");                    }                    for(int i=1;i<abc.length-1;i++){                        int k=i;                        for(int j=i+1;j<abc.length;j++){                            if( abc[j]<abc[k]){                                k=j;                            }                        }                        if(i !=k){                            int tmp=abc[i];                            abc[i]=abc[k];                            abc[k]=tmp;                        }                    }                    System.out.println();                    System.out.print("快速排序前数组为:");                    for(int num:abc){                        System.out.print(num+" ");                    }            }}

5、快速排序

package cn.qiuuuu.test;public class sortQuick {    public static void sort(int array[],int low,int high){        int i,j;        int index;        if(low>=high){            return;        }        i=low;        j=high;        index=array[i];        while(i<j){            while(i<j&&array[j]>=index){                j--;            }            if(i<j){                array[i++]=array[j];            }            while(i<j&&array[i]<index){                i++;            }            if(i<j){                array[j--]=array[i];            }            array[i]=index;            sort(array,low,i-1);            sort(array,i+1,high);        }    }    public static void quickSort(int array[]){        sort(array,0,array.length-1);    }    public static void main(String[] args) {        int i=0;        int a[]={5,4,9,8,7,6,0,1,3,2};        int len=a.length;        quickSort(a);        for(i=0;i<len;i++){            System.out.println(a[i]+" ");        }    }}

6、插入排序

int arr[] = { 34, 45, 65, 54, 23, 1, 34, 3, 45, 4 };for (int i = 0; i < arr.length; i++) {        int j;        int tmp = arr[i];        for (j = i - 1; j >= 0; j--) {                if (arr[j] > tmp) {                        arr[j + 1] = arr[j];                } else {                        break;                }        }        arr[j + 1] = tmp;}

7、希尔排序

int[] a={49,38,65,97,76,13,27,49,78,34,12,64,1,33,85,29};int d = a.length/2; //设置增量while(true){        for(int i=0;i<d;i++){                for(int j=i;j+d<a.length;j+=d){                int temp;                if(a[j]>a[j+d]){                        temp=a[j];                        a[j]=a[j+d];                        a[j+d]=temp;                        }                }        }        if(d==1){break;}        d--;     }

0人推荐
随时随地看视频
慕课网APP