# java算法-冒泡排序

>相邻位置比较，如果约定从大到小排序，每一轮比较完成，可以排好一个小的，如果从小到大，每一轮比较完成，可以在末尾排好一个大的

package com.ghostwu;
import java.util.Random;

class MyBubbleSort {
private int[] arr;
public MyBubbleSort(){
arr = new int[10];
Random rand = new Random();
for( int i = 0; i < arr.length; i++ ){
arr[i] = rand.nextInt( 101 );
}
}
public void sort(){
for( int i = 0; i < arr.length; i++ ){
for( int j = 0; j < ( arr.length - i - 1 ); j++ ){
if( arr[j] > arr[j+1] ) {
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
display( "第" + ( i + 1 ) + "轮的比较结果: " );
}
}
public void display( String info ){
System.out.println( info );
for( int i = 0; i < arr.length; i++ ){
System.out.print( arr[i] + "\t" );
}
System.out.println();
}
}

public class BubbleSort{
public static void main( String[] args ){

MyBubbleSort bs = new MyBubbleSort();
bs.display( "排序之前:" );
bs.sort();
bs.display( "排序之后:" );
}
}

ghostwu@dev:~/java/data_struct/sort$!javac javac -d . BubbleSort.java ghostwu@dev:~/java/data_struct/sort$ java com.ghostwu.BubbleSort

27    93    9    25    6    98    39    84    56    19

27    9    25    6    93    39    84    56    19    98

9    25    6    27    39    84    56    19    93    98

9    6    25    27    39    56    19    84    93    98

6    9    25    27    39    19    56    84    93    98

6    9    25    27    19    39    56    84    93    98

6    9    25    19    27    39    56    84    93    98

6    9    19    25    27    39    56    84    93    98

6    9    19    25    27    39    56    84    93    98

6    9    19    25    27    39    56    84    93    98

6    9    19    25    27    39    56    84    93    98

6    9    19    25    27    39    56    84    93    98

package com.ghostwu;
import java.util.Random;

class MyBubbleSort {
private int[] arr;
public MyBubbleSort(){
arr = new int[10];
Random rand = new Random();
for( int i = 0; i < arr.length; i++ ){
arr[i] = rand.nextInt( 101 );
}
}
public void sort(){
boolean flag = true;
for( int i = 0; i < arr.length; i++ ){
flag = true;
for( int j = 0; j < ( arr.length - i - 1 ); j++ ){
if( arr[j] > arr[j+1] ) {
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
flag = false;
}
}
if( flag ) break;
display( "第" + ( i + 1 ) + "轮的比较结果: " );
}
}
public void display( String info ){
System.out.println( info );
for( int i = 0; i < arr.length; i++ ){
System.out.print( arr[i] + "\t" );
}
System.out.println();
}
}

public class BubbleSort{
public static void main( String[] args ){

MyBubbleSort bs = new MyBubbleSort();
bs.display( "排序之前:" );
bs.sort();
bs.display( "排序之后:" );
}
}

ghostwu@dev:~/java/data_struct/sort\$ java com.ghostwu.BubbleSort

9    71    79    0    31    11    75    65    68    36

9    71    0    31    11    75    65    68    36    79

9    0    31    11    71    65    68    36    75    79

0    9    11    31    65    68    36    71    75    79

0    9    11    31    65    36    68    71    75    79

0    9    11    31    36    65    68    71    75    79

0    9    11    31    36    65    68    71    75    79    

posted @ 2018-07-07 18:25  ghostwu  阅读(248)  评论(0编辑  收藏  举报