算法篇---冒泡排序算法

冒泡排序算法

原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换,

这样一趟过去后,最大或最小的数字被交换到了最后一位,

然后再从头开始进行两两比较交换,直到倒数第二位时结束,其余类似看例子

例子为从小到大排序,

代买仅供参考:

 1 package com.zc.manythread;
 2 /**
 3  * 冒泡排序
 4  * @author Administrator
 5  *
 6  */
 7  
 8 public class BSrot extends Sort{
 9  
10     public void sort() {
11         try {
12             sort(date);
13         } catch (Exception e) {
14             // TODO: handle exception
15             e.printStackTrace();
16         }
17     }
18     public int[] sort(int[] a)throws Exception{
19         for (int i = a.length; --i>=0;) {
20             boolean swapped=false;
21             for (int j = 0; j < i; j++) {
22                 if (a[j]>a[j+1]) {
23                     int T=a[j];
24                     a[j]=a[j+1];
25                     a[j+1]=T;
26                     swapped=true;
27                 }
28             }
29             if (!swapped) {
30                 return a;
31             }
32         }
33         return a;
34     }
35     public BSrot(int[] date) {
36         super(date);
37         // TODO Auto-generated constructor stub
38     }
39  
40 }
原文地址:https://www.cnblogs.com/oumyye/p/4191422.html