Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1449903
  • 博文数量: 254
  • 博客积分: 8696
  • 博客等级: 中将
  • 技术积分: 2961
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-03 16:46
文章分类

全部博文(254)

文章存档

2015年(4)

2014年(18)

2013年(16)

2012年(8)

2011年(25)

2010年(2)

2009年(74)

2008年(107)

分类: Java

2008-07-09 23:34:40

 
public class Sort2
{

   //交换排序之--最简单的冒泡排序法
    public static void BubbleSort(int[] a)
    {
      int i,j,temp;
      int n = a.length;
     
      for(i=0;i      {
          for(j=i+1;j          {
             if (a[i]>a[j])
             {
               temp = a[i];
               a[i] = a[j];
               a[j] = temp;
             }
          }
      }
    }
   

   //插入排序-- 直接插入排序
    public static void InsertSort(int[] a)
    {
      int i,j,temp;
      int n = a.length;
     
      for(i=1;i      {
         temp = a[i];
         j=i-1;
         while((a[j]>temp)&&(j>=0))
         {
           a[j+1] = a[j];
           j--;
         }
         a[j+1] = temp;
      }
    }
    

    //选择排序
    public static void SelectSort(int[] a)
    {
       int i,j,min,temp;
       int n=a.length;
      
       for(i=0;i       {
         min = a[i];
         for(j=i+1;j         {
            if(a[j]            {
              temp = min;
              min =a[j];
              a[j] = temp;
            }
         }
         a[i] =min ;
       }
    }
    public static void main(String[] args)
    {
       int[] arr = {45,76,32,32,5,4,54,7,943,3};
       int n = arr.length;
      
       BubbleSort(arr);
      
       for(int i=0;i         System.out.print(arr[i]+"   ");
        
       System.out.println();
      
       InsertSort(arr);
       for(int i=0;i         System.out.print(arr[i]+"   ");
        
       System.out.println();
      
       SelectSort(arr);
       for(int i=0;i         System.out.print(arr[i]+"   ");
    }
}

(上述出自;http://hi.baidu.com/haoshuang3394/blog/item/3033044c2c2338f8d72afc9e.html 未经验证)

阅读(739) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~