2012/3/30----冒泡排序

来源:互联网 发布:出口网络推广 编辑:程序博客网 时间:2024/05/11 19:10

冒泡排序的核心思想:把数组中的相邻两个数进行比较,然后把较大的数向后移,一直到最后的一个数是整个数组中最大的数。再把前面的过程循环,就可以完成排序。

Java代码  收藏代码
  1. package com.akon405.www;  
  2.   
  3. public class BubbleSort {  
  4.     public BubbleSort(int[] A){  
  5.         int i,j;  
  6.         for(i=0;i<A.length;i++){  
  7.             for(j=0;j<A.length-i-1;j++){  
  8.                 if(A[j+1]<A[j]){  
  9.                     int temp;  
  10.                     temp=A[j];  
  11.                     A[j]=A[j+1];  
  12.                     A[j+1]=temp;  
  13.                 }     
  14.             }  
  15.         }  
  16.         for(i=0;i<A.length;i++)  
  17.             System.out.print(A[i]+",");  
  18.     }  
  19.     /** 
  20.      * @param args 
  21.      */  
  22.     public static void main(String[] args) {  
  23.         // TODO Auto-generated method stub  
  24.         int[] A={2,12,32,43,13,45,1,8,23,47,89,90};  
  25.         new BubbleSort(A);  
  26.     }  
  27.   
  28. }  
 
0 0
原创粉丝点击