`

排序算法(选择,冒泡,插入)

    博客分类:
  • J2SE
 
阅读更多
//1、选择排序
for(int i = 0;i < ary.length -1;i ++){
   for(int j = 0;j < ary.length;j++){
      if(ary[i] > ary[j]){
         int temp = ary[i];
         ary[i] = ary[j];
         art[j] = temp;
      }
   }
}

//2、冒泡排序
for(int i =0;i < ary.legnth-1;i++){
   for(int j = 0;j < ary.length -1 -i;j ++){
      if(ary[j] > ary[j+1]){
         int temp = ary[j];
         ary[j] = ary[j+1];
         ary[j+1] = temp;
      }
   }
}

//3、插入排序
int i ,j ,k;
for(i =1;i < ary.length;i ++){
   k = ary[i];//一定要注意不能把这句赋值语句丢掉,即把下标为i 的值取出来
   for(j =0;j >= 0 && ary[j] > k;j++){
      ary[j+1] = ary[j]; 
   }
   ary[j+1] = k;
   System.out.println();
}
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics