选择、冒泡排序

数组选择排序

//选择排序:数组中的每个元素都进行比较
public static void SelectSort(int[] a){
        for(int i=0;i<a.length;i++){
            for(int j=i+1;j<a.length;j++){
                if(a[i] > a[j]){
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
    }

数组冒泡排序

//冒泡排序:每次都是相邻的连个元素相比较
public static void BubbleSort(int[] a){
    for(int i=0;i<a.length-1;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;
            }
        }
    }
}
发布了4 篇原创文章 · 获赞 0 · 访问量 24

猜你喜欢

转载自blog.csdn.net/phpsilence/article/details/105376270
今日推荐