- package com.cw.ubuntu.test;
- public class TestVoidSort {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- int[] testTemp = {45,234,67,12,35,68,99,24,158,57,77};
- int[] testTemp2 = {24,56,34,78,12,99,36,15,68,59,76};
- System.out.println("Test 1 ------------------------------");
- sort(testTemp);
- System.out.println("Test 2 ------------------------------");
- sort(testTemp2);
- }
- //The code of sort
- public static void sort(int[] temp){
- for (int i=1; i<temp.length; i++){
- for (int j=0; j<temp.length; j++){
- if(temp[i] < temp[j]){
- int x = temp[i];
- temp[i] = temp[j];
- temp[j] = x;
- }
- }
- //The export for Inner circulation
- print(temp);
- System.out.println("The "+ i+" test :");
- }
- }
- //The code for print
- public static void print(int[] temp){
- for (int i = 0; i < temp.length; i++){
- System.out.print(temp[i] + "\t");
- }
- }
- }
- /*
- *
- Test 1 ------------------------------
- 45 234 67 12 35 68 99 24 158 57 77 The 1 test :
- 45 67 234 12 35 68 99 24 158 57 77 The 2 test :
- 12 45 67 234 35 68 99 24 158 57 77 The 3 test :
- 12 35 45 67 234 68 99 24 158 57 77 The 4 test :
- 12 35 45 67 68 234 99 24 158 57 77 The 5 test :
- 12 35 45 67 68 99 234 24 158 57 77 The 6 test :
- 12 24 35 45 67 68 99 234 158 57 77 The 7 test :
- 12 24 35 45 67 68 99 158 234 57 77 The 8 test :
- 12 24 35 45 57 67 68 99 158 234 77 The 9 test :
- 12 24 35 45 57 67 68 77 99 158 234 The 10 test :
- Test 2 ------------------------------
- 24 99 34 56 12 78 36 15 68 59 76 The 1 test :
- 24 34 99 56 12 78 36 15 68 59 76 The 2 test :
- 24 34 56 99 12 78 36 15 68 59 76 The 3 test :
- 12 24 34 56 99 78 36 15 68 59 76 The 4 test :
- 12 24 34 56 78 99 36 15 68 59 76 The 5 test :
- 12 24 34 36 56 78 99 15 68 59 76 The 6 test :
- 12 15 24 34 36 56 78 99 68 59 76 The 7 test :
- 12 15 24 34 36 56 68 78 99 59 76 The 8 test :
- 12 15 24 34 36 56 59 68 78 99 76 The 9 test :
- 12 15 24 34 36 56 59 68 76 78 99 The 10 test :
- *
- * */
- */
复制代码 综上,可见,冒泡排序关键是
1.一次遍历后,将最大数放置于第二位,以后依次后延最大数的位置
2.保证最大数之前的数据已排序OK
|
|