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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ╰'宁静致远` 中级黑马   /  2013-1-13 17:14  /  1740 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class  maopao
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.    
  6. int [] q = {99,8,7,48,41,85,41};

  7.   

  8.                
  9. paixu (q);
  10. dayin(q);
  11. }

  12. public static void paixu (int [] q)
  13.         {
  14.         for (int a = 0;a <= q.length-1;a++ )//总数有q.length个角标,本身不用比较,所以q.length-1

  15.         {
  16.                 for (int b= 0; b<q.length-1-a;b++ )//除了本身还剃出了比较过的数
  17.                 {
  18.                         
  19.                         if (q[b] > q[b+1])
  20.                         {
  21.                             int tamp = 0;
  22.                           
  23.                             tamp  = q[b];
  24.                                 q[b] = q[b+1];
  25.                                 q[b+1]= tamp;
  26.                           
  27.                         }
  28.                 }
  29.                   
  30.         }

  31.         }

  32. public static void dayin (int []q)
  33.         {


  34.          for (int a=0;a<=q.length-1 ;a++ )//因为角标存在0,所以是q.length-1.
  35.          {
  36.                  if (a != q.length-1)
  37.                  
  38.                  System.out.print(q[a]+" "+"<"+" ");
  39.                  else
  40.          System.out.print(q[a]);
  41.          }

  42.         }


  43. }
复制代码

未命名.jpg (4.57 KB, 下载次数: 23)

未命名.jpg

2 个回复

倒序浏览
public class SortTest {
        public static void main(String[] args) {
                int[] x = new int[] { 1, 9, 7, 3, 5};
                insert(x);
                shell(x);
        }

        // 插入排序
        public static void insert(int[] x) {
                for (int i = 1; i < x.length; i++) {
                        // i从一开始,因为第一个数已经是排好序的啦
                        for (int j = i; j > 0; j--) {
                                if (x[j] < x[j - 1]) {
                                        int temp = x[j];
                                        x[j] = x[j - 1];
                                        x[j - 1] = temp;
                                }
                        }
                }
                for (int i : x) {
                        System.out.print(i + " ");
                }
        }

        // 希尔排序
        public static void shell(int[] x) {
                // 分组
                for (int increment = x.length / 2; increment > 0; increment /= 2) {
                        // 每个组内排序
                        for (int i = increment; i < x.length; i++) {
                                int temp = x[i];
                                int j = 0;
                                for (j = i; j >= increment; j -= increment) {
                                        if (temp < x[j - increment]) {
                                                x[j] = x[j - increment];
                                        } else {
                                                break;
                                        }
                                }
                                x[j] = temp;
                        }
                }
                for (int i : x) {
                        System.out.print(i + " ");
                }
        }
}
没事可以看下,
回复 使用道具 举报
四种排序方法都齐了,冒泡.选择,插入,希尔下面是补充兄台的选择和自己习惯用的希尔
选择for嵌套循环
     public static void select(int[] a)
        {
        int tmp =0 ;
        for (int i = 0; i <a.length-1; i++ ) //比较次数
        {
                for (int j = i+1;j < a.length;j++ )  //元素的个数
                {
                        if (a[i]>a[j])
                        {
                        tmp=a[i];
                        a[i]=a[j];
                        a[j]=tmp;
                        }
                }
        }
  }

希尔
public void sort1(double[] a, int incr[]) {  
        int span;// 增量   
        int alen = a.length;// 数组个数   
        for (int i = 0; i < incr.length; i++) {//增量个数次   
            span = incr[i];  
            for (int j = 0; j < span; j++) {  
                  
                for (int k = 0; k < alen - span; k += span) {//组内直接插入排序                        
                    int p = k;  
                    double temp = a[k+span];//current   
                    while( p >=0 && a[p] > temp){  
                        a[p+span] = a[p];  
                        p -=span;  
                    }            
                    a[p + span] = temp;  
                }  
            }  
        }  
    }  
希望可以有帮助...
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马