java 冒泡排序

来源:互联网 发布:大型网络3d手游2016年 编辑:程序博客网 时间:2024/06/10 03:18

package com;


/**
 * @author leon
 *
 */
public class BubbleSort {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BubbleSort bubbleSort = new BubbleSort();
int arr[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
bubbleSort.bubbleSort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
System.out.print(",");
}
}

public void bubbleSort(int arr[]){
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 temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
}


}
原创粉丝点击