现在的位置: 首页 > 综合 > 正文

算法—–冒泡排序

2013年12月07日 ⁄ 综合 ⁄ 共 626字 ⁄ 字号 评论关闭
  1. package com.eshore.sweetop.sort;
  2. import java.util.Arrays;
  3. public class Bubble {
  4.     public static void sort(int[] a,boolean reverse){
  5.         for (int i = 0; i < a.length-1; i++) {
  6.             for (int j = i+1; j < a.length; j++) {
  7.                 if(reverse^(a[j]<a[j-1])){
  8.                     a[j]^=a[j-1];
  9.                     a[j-1]^=a[j];
  10.                     a[j]^=a[j-1];
  11.                 }
  12.             }
  13.         }
  14.     }
  15.     
  16.     public static void sort(int[] a){
  17.         sort(a,false);
  18.     }
  19.     
  20.     public static void main(String[] args) {
  21.         int[] a={2,5,3,7,6,8};
  22.         sort(a);
  23.         System.out.println(Arrays.toString(a));
  24.     }
  25. }

抱歉!评论已关闭.