三种排序方式

 三种排序方式:冒泡排序,选择排序,插入排序。

这三种排序皆用idea软件,Java语言所编写,希望能够对其有所帮助

  1. 冒泡排序

public class Mpao {
    public static void mpao(int[] a) {
        int tem=0;
        for (int i = 0; i < a.length-1 ; ++i) {
            for (int j=0;jlength-i-1;++j){
                if (a[j+1][j]){
                    tem=a[j];
                    a[j]=a[j+1];
                    a[j+1]=tem;

                }
            }
        }
    }

    public static void main(String[] args) {
        int[]arr={9,5,8,6,4,1,7};
        mpao(arr);
        System.out.println(Arrays.toString(arr));
    }
}

2.选择排序的代码
//选择排序
public class Xzhepx {
    public static void main(String[] args) {
        int[] arry={65,54,21,35,18,94,85};
        System.out.println("原数组:");
        for (int i :arry) {
            System.out.print(i+" ");
        }
        System.out.println();
        Xze(arry);
        System.out.println("排序后:");
        for (int i:arry) {
            System.out.print(i+" ");
        }
    }
public static void Xze(int[] arr){
    for (int i = 0; i length-1 ; i++) {
        int min=i;
        for (int j = i+1; j length ; j++) {
            if (arr[j][min]){
                min=j;
            }
        }
        if (min!=i){
            swap(arr,i,min);
        }
    }
}
public static void swap(int[] arr,int a,int b){
        int tem=arr[a];
        arr[a]=arr[b];
        arr[b]=tem;
}
}

3.插入排序的代码


//插入排序

import java.util.Arrays;

public class Crupx {
    public static void main(String[] args) {
        int[] numb={8,5,9,6,3,7};
        System.out.println("排序前:"+ Arrays.toString(numb));
        int i,j;
        for ( i = 1; i length ; i++) {
            int tem=numb[i];
            for (j = i; j >0 && tem[j-1] ; j--) {
                numb[j]=numb[j-1];
            }numb[j]=tem;
            System.out.println("第"+i+"轮排序结果:"+Arrays.toString(numb));
        }
        System.out.println("排序后的结果:"+Arrays.toString(numb));
    }
}

你可能感兴趣的:(毕业季,java)