黑马程序员技术交流社区

标题: 第一个java程序,冒泡排序 [打印本页]

作者: ╰'宁静致远`    时间: 2013-1-13 17:14
标题: 第一个java程序,冒泡排序
  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

作者: 罗利华    时间: 2013-1-13 17:26
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 + " ");
                }
        }
}
没事可以看下,
作者: 汪平乐    时间: 2013-1-14 02:14
四种排序方法都齐了,冒泡.选择,插入,希尔下面是补充兄台的选择和自己习惯用的希尔
选择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;  
                }  
            }  
        }  
    }  
希望可以有帮助...




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2