java基础------》数据结构---》交换排序(冒泡排序,快速排序),选择排序,系统排序,插入排序

来源:互联网 发布:影视鉴赏网络课答案16 编辑:程序博客网 时间:2024/05/22 03:45

冒泡排序

冒泡排序是根据相邻的两个数进行对比

第一次排序是进行大规模的排序,

第二次排序忽略了最末尾的那个数

例如:5,4,3,8,1(相邻两个进行对比)

首先5和4比  大则换位    4,5,3,8,1

        5和3比  大则换位    4,3,5,8,1

        5和8比   小则不变   4,3,5,8,1

        8和1比   大则换位   4,3,5,1,8

第一次运算结果:4,3,5,1,8

再  4和3比  大则换位     3,4,5,1,8

     4和5比  小则不变    

     。。。。。。。。。。。

最后得出结果

这就是冒泡算法


import java.util.Arrays;

public class Sort {
        
    public static int[]  buble_sort(int [] ary) {
        for(int i=0;i<ary.length-1;i++ ){
            
            for(int j=0;j<ary.length-1-i;j++){
                if(ary[j]>ary[j+1]){
                    int temp=ary[j];
                    ary[j]=ary[j+1];
                    ary[j+1]=temp;
                }
                
            }
            
        }
        return ary;
    }
    public static void main(String[] args) {
        int []ary={100,20,5,1000,60,40,5,1,2,6};
        ary = Sort.buble_sort(ary);
        System.out.println(Arrays.toString(ary));
    }
}



快速算法(冒泡算法优化)


快速算法是最有效率的一种排序方法,它采用两边同时进行对比,不断交换位置,另外采用的递归方法使该算法更为有效率


public class sort {
    
    
    public static  void quicksort(int [] table) {
            quicksort(table,0,table.length-1);

    }
    
    private static void quicksort(int[] table ,int low,int hight){
            if(low<hight){
                int i=low,j=hight;
                int vot=table[i];
                while(i!=j){
                    while(i<j&&vot<=table[j])
                        j--;
                    if(i<j){
                        table[i]=table[j];
                        i++;
                    }
                    while(i<j && table[i]<vot)
                        i++;
                    if(i<j){
                        table[j]=table[i];
                        j--;
                    }
                }
                table[i]=vot;
                System.out.println(low+".."+hight+", vot= "+vot+"  ");
                quicksort(table,low,j-1);
                quicksort(table,i+1,hight);
                
            }
    }

    public static void main(String[] args) {
        int[]a={5,3,1,2000,8,9,7,1};
        
        quicksort(a);
        for(int i=0;i<a.length;i++){
            System.out.print(a[i]+",");
        }
    }
}


选择排序

例如:5,4,3,8,1(进行对比)

1.首先5和4比  大则换位  4,5,3,8,1

         4和3比  大则换位  3,4,5,8,1

          3和8比  小则不变  3,4,5,8,1

        3和1比    大则换位   1,4,5,8,3   最小的被放于最左边,于是忽略最左边再进行对比


2.再 4和5比   小则不变  1,4,5,8,3

    4和8比    小则不变  1,4,5,8,3

    4和3比   大则换位  1,3,5,8,4


3.      5和8比  小则不变   1,3,5,8,4

          5和4比  大则换位   1,3,4,8,5


4.     8和5  比     大则换位    1,3,4,5,8

这就是选择排序的算法过程,它最主要是通过一次循环中固定不变的位置对其他各个位置进行对比,然后再得出最小值摔到最左边

public class Sort {
        
    public static int[]  buble_sort(int [] ary) {
            for(int i=0;i<ary.length-1;i++){
                for(int j=i+1;j<ary.length;j++){
                    if(ary[i]>ary[j]){
                        int temp=ary[i];
                        ary[i]=ary[j];
                        ary[j]=temp;
                    }
                }
            }
        return ary;
    }
    public static void main(String[] args) {
        int []ary={100,20,5,1000,60,40,5,1,2,6};
        ary = Sort.buble_sort(ary);
        System.out.println(Arrays.toString(ary));
    }
}


系统排序

这个是jdk自带的方法

public static void main(String[] args) {
        int []ary={100,20,5,1000,60,40,5,1,2,6};
        Arrays.sort(ary);
        System.out.println(Arrays.toString(ary));
    }




1 0
原创粉丝点击