【动手写排序】冒泡排序

来源:互联网 发布:淘宝下单返利 编辑:程序博客网 时间:2024/05/21 14:49

代码都是自己根据算法的原理写出来的,理解起来应该不难

import org.junit.Test;


public class BubbleSort {
public void sort(int[] arr){
for(int i=arr.length-1;i>0;i--){
for(int j=0;j<i;j++){
if(arr[j]>arr[j+1])
swap(arr,j,j+1);
}
}
}
public void swap(int[] arr,int i,int j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
@Test
public void test(){
int[] arr={9,8,7,6,4,5,3,1,2};
sort(arr);
for(int i:arr){
System.out.print(i+" ");
}
}
}

0 0
原创粉丝点击