A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© If_091021 中级黑马   /  2016-3-9 22:05  /  440 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class Paixu {
        //插入排序
        public static void charupaixu(int []a){
               
                int j;
                for(int i=1;i<a.length;i++){
                        int temp=a[i];
               
                        for(j=i;j>0&&temp<a[j-1];j--){
                                a[j]=a[j-1];
                        }
                        a[j]=temp;
                }
        }
        //桶式排序
        public static void tongpaixu(int []a){
                int[] c=new int[20];  //数组大小值要大于排序数组中的最大值
                for(int i=0;i<a.length;i++){
                        c[a[i]]=1;
                }
                int lo=0;
                for(int i=0;i<c.length;i++){
                        if(c[i]==1&&lo<a.length){
                                a[lo]=i;
                                lo++;
                        }
                }
        }
        //输出数组
        public static void print(int a[]){
                for(int i=0;i<a.length;i++){
                        System.out.print(a[i]+"   ");
                }
                System.out.println();
        }
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                int[] a={10,14,7,4,8,9,16};
                int[] b=a;
                charupaixu(a);
                tongpaixu(b);
                print(a);
                print(b);
               
        }

}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马