- public class TestBubbleSort {
-
- public static void bubbleSortFirst(int[] data){
- for (int i = 1; i < data.length; i++) {
- int temp;
- for (int j = 0; j < data.length-i; j++) {
- if(data[j]>data[j+1]){
- temp = data[j];
- data[j] = data[j+1];
- data[j+1] = temp;
- }
- }
- }
- }
-
- public static void bubbleSortSecond(int[] data){
- for (int i = data.length-1;i>0;i--) {
- int temp;
- boolean isExchanged = false;
- for (int j = 0; j < i; j++) {
- if(data[j]>data[j+1]){
- temp = data[j];
- data[j] = data[j+1];
- data[j+1] = temp;
- isExchanged = true;
- }
- }
- if(!isExchanged){
- return;
- }
- }
- }
-
- public static void main(String[] args) {
- int[] a = {2,34,1,56,28,98};
-
- bubbleSortSecond(a);
- for (int i = 0; i < a.length; i++) {
- System.out.print(a[i]+"\t");
- }
- }
- }
posted @
2011-12-09 16:30
agile_work
阅读(
96)
评论()
收藏
举报