Java的两大排序算法:冒泡+选择

直接看代码:

package chapter66.hiyo.com;

public class ScoreSortDemo {
        public static void main(String[] args) {
            int[] enscore = new int[]{3,1,10,43,21,90,6,3,33} ;
            ScoreSortDemo sort = new ScoreSortDemo() ;
            System.out.println("原始数组:") ;
            sort.showArr(enscore);
            System.out.println("排序之后的数组:") ;
            sort.BubbleSort(enscore);
            sort.showArr(enscore);
        }

        //冒泡排序算法的实现
        public void BubbleSort(int[] arr) {
            for(int i=0; i<arr.length; i++) {
                for(int j=0; j<arr.length-1; j++) {
                    if(arr[j+1] < arr[j]) {
                        int temp = arr[j+1]  ;
                        arr[j+1] = arr[j] ;
                        arr[j] = temp ;
                    }
                }
            }
        }

        public void showArr(int[] arr) {
            for(int i : arr ) {
                System.out.print(i + " ") ;
            }
            System.out.println() ;
        }

        //选择排序算法的实现
        public void SelectSort(int[] arr) {
            int minIndex ;
            for(int i=0; i<arr.length ;i++) {
                minIndex = i ;
                //寻找最小值的索引
                for(int j=i+1; j<arr.length ; j++)  {
                    if(arr[minIndex] > arr[j]) {
                        minIndex = j ;
                    }
                }
                //找到最小值之后,和第一个元素进行交换
                int temp = arr[minIndex] ;
                arr[minIndex] = arr[i] ;
                arr[i] = temp ;
            }
        }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值