A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© ZYZQ 中级黑马   /  2015-6-1 22:44  /  291 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package com.cw.ubuntu.test;

  2. public class TestVoidSort {

  3.         /**
  4.          * @param args
  5.          */
  6.         public static void main(String[] args) {
  7.                 // TODO Auto-generated method stub
  8.                 int[] testTemp = {45,234,67,12,35,68,99,24,158,57,77};
  9.                 int[] testTemp2 = {24,56,34,78,12,99,36,15,68,59,76};
  10.                 System.out.println("Test 1 ------------------------------");
  11.                 sort(testTemp);
  12.                 System.out.println("Test 2 ------------------------------");
  13.                 sort(testTemp2);
  14.         }
  15.         //The code of sort
  16.         public static void sort(int[] temp){
  17.                 for (int i=1; i<temp.length; i++){
  18.                         for (int j=0; j<temp.length; j++){
  19.                                 if(temp[i] < temp[j]){
  20.                                         int x = temp[i];
  21.                                         temp[i] = temp[j];
  22.                                         temp[j] = x;
  23.                                 }
  24.                         }
  25.                         //The export for Inner circulation
  26.                         print(temp);
  27.                         System.out.println("The "+ i+" test :");
  28.                 }
  29.         }
  30.         //The code for print
  31.         public static void print(int[] temp){
  32.                 for (int i = 0; i < temp.length; i++){
  33.                         System.out.print(temp[i] + "\t");
  34.                 }
  35.         }

  36. }
  37. /*
  38. *
  39. Test 1 ------------------------------
  40. 45        234        67        12        35        68        99        24        158        57        77        The 1 test :
  41. 45        67        234        12        35        68        99        24        158        57        77        The 2 test :
  42. 12        45        67        234        35        68        99        24        158        57        77        The 3 test :
  43. 12        35        45        67        234        68        99        24        158        57        77        The 4 test :
  44. 12        35        45        67        68        234        99        24        158        57        77        The 5 test :
  45. 12        35        45        67        68        99        234        24        158        57        77        The 6 test :
  46. 12        24        35        45        67        68        99        234        158        57        77        The 7 test :
  47. 12        24        35        45        67        68        99        158        234        57        77        The 8 test :
  48. 12        24        35        45        57        67        68        99        158        234        77        The 9 test :
  49. 12        24        35        45        57        67        68        77        99        158        234        The 10 test :
  50. Test 2 ------------------------------
  51. 24        99        34        56        12        78        36        15        68        59        76        The 1 test :
  52. 24        34        99        56        12        78        36        15        68        59        76        The 2 test :
  53. 24        34        56        99        12        78        36        15        68        59        76        The 3 test :
  54. 12        24        34        56        99        78        36        15        68        59        76        The 4 test :
  55. 12        24        34        56        78        99        36        15        68        59        76        The 5 test :
  56. 12        24        34        36        56        78        99        15        68        59        76        The 6 test :
  57. 12        15        24        34        36        56        78        99        68        59        76        The 7 test :
  58. 12        15        24        34        36        56        68        78        99        59        76        The 8 test :
  59. 12        15        24        34        36        56        59        68        78        99        76        The 9 test :
  60. 12        15        24        34        36        56        59        68        76        78        99        The 10 test :
  61. *
  62. * */
  63. */
复制代码
综上,可见,冒泡排序关键是
1.一次遍历后,将最大数放置于第二位,以后依次后延最大数的位置
2.保证最大数之前的数据已排序OK

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马