Java中常见的数组元素排序方式【冒泡排序;选择性排序】



A:建一个MaoPaoDemo的类,代码如下:
package com.webservice.demo;

/***
 * 冒泡排序
 * @author Administrator
 *
 */
public class MaoPaoDemo {

 public static void main(String[] args) {
  int a[] = {11,2,5,82,7,0,4,89,72,42,16,34,58,23}; 
  for(int i=0;i<a.length;i++){ 
      for(int j=0;j<a.length-1-i;j++){ 
          if(a[j] > a[j+1]){ 
              int temp = a[j]; 
              a[j] = a[j+1]; 
             a[j+1] = temp; 
         }  
     } 
  } 
  for(int x:a){ 
  System.out.println(x);
  }
 }
}
执行上述代码获得运行结果为:

0 2 4 5 7 11 16 23 34 42 58 72 82 89

B:简历一个选择性排序的SelectDemo类,代码如下:
/****
 * 选择型排序
 * @author Administrator
 *
 */
public class SelectDemo {
 
 public static void main(String[] args) {
  int arr[] = {11,2,5,82,7,0,4,89,72,42,16,34,58,23}; 
  for(int i=0;i<arr.length;i++){
   for(int j=i+1;j<arr.length;j++){
    if(arr[i]>arr[j]){
    int temp=arr[i];
    arr[i]=arr[j];
    arr[j]=temp;
    }
   }
  }
  for(int x:arr){
   System.out.println(x);
  }
 }
 
}

执行上述代码获得运行结果为:
0 2 4 5 7 11 16 23 34 42 58 72 82 89

综上所述两种常见的排序方式获得结果是一致的!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值