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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 半世心修 中级黑马   /  2015-5-30 09:09  /  264 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

java的四种基础排序分别是:冒泡法,插入法,选择法,SHELL排序法
冒泡:
  1. public void bubbleSort(int[] array)
  2. {
  3.   int temp;
  4.   for(int i=0;i<array.length-1;i++)
  5.   {
  6.    for(int j=i+1;j<array.length;j++)
  7.    {
  8.     if(array[i]>array[j])
  9.     {
  10.      temp = array[i];
  11.      array[i] = array[j];
  12.      array[j] = temp;
  13.     }
  14.    }
  15.   }
  16. }
复制代码

选择排序:
  1. public void chooseSort(int[] array)
  2. {
  3.   int temp;
  4.   for(int i=0;i<array.length;i++)
  5.   {
  6.    int lowIndex = i;
  7.    for(int j=array.length-1;j>i;j--)
  8.    {
  9.     if(array[j]<array[lowIndex])
  10.     {
  11.      lowIndex = j;
  12.     }
  13.    }
  14.    temp = array[i];
  15.    array[i] = array[lowIndex];
  16.    array[lowIndex] = temp;
  17.   }
  18. }
复制代码

插入排序:
  1. public void insertSort(int[] array)
  2. {
  3.   int temp;
  4.   for(int i=1;i<array.length;i++)
  5.   {
  6.    for(int j=i;(j>0)&&array[j]<array[j-1];j--)
  7.    {
  8.     temp = array[j];
  9.     array[j]=array[j-1];
  10.     array[j-1]=temp;
  11.    }
  12.   }
  13. }
复制代码

shell:
  1. public void shellSort(int[] array)
  2. {
  3.   for(int i=array.length/2;i>2;i/=2)
  4.   {
  5.    for(int j=0;j<i;j++)
  6.    {
  7.     insertSortForShell(array,j,i);
  8.    }
  9.    insertSortForShell(array,0,1);
  10.   }
  11. }
  12. private void insertSortForShell(int[] array,int start,int inc)
  13. {
  14.   int temp;
  15.   for(int i=start+inc;i<array.length;i+=inc)
  16.   {
  17.    for(int j=i;(j>=inc)&&(array[j])<array[j-inc];j-=inc)
  18.    {
  19.     temp=array[j];
  20.     array[j]=array[j-inc];
  21.     array[j-inc]=temp;
  22.    }
  23.   }
  24. }
复制代码

不要去看排序的解释就看算法,分析出他们是怎么实现的,这是最好的

评分

参与人数 1技术分 +1 收起 理由
lwj123 + 1

查看全部评分

1 个回复

正序浏览
学习了,楼主好厉害,我只知道冒泡跟选择
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马