public class TestSort{
public static void main(String[] args){
int[] arr={1,4,9,3,7,10,23,5};
printArray(arr);
System.out.println();
selectSort(arr);
printArray(arr);
System.out.println();
BubleSort(arr);
printArray(arr);
}
//sort by bubling[冒泡排序]
private static void BubleSort(int[] arr) {
// TODO Auto-generated method stub
for(int x=0;x<arr.length-1;x++){
for(int y=0;y<arr.length-x-1;y++){
if(arr[y]>arr[y+1]){
int temp=arr[y];
arr[y]=arr[y+1];
arr[y+1]=temp;
}
}
}
}
// sort by selecting[选择排序]
public static void selectSort(int[] arr){
for (int x=0;x<arr.length-1 ;x++ )
{
for (int y=x+1;y<arr.length ;y++ )
{
if (arr[x]>arr[y])
{
int temp=arr[x];
arr[x]=arr[y];
arr[y]=temp;
}
}
}
}
private static void printArray(int[] arr) {
// TODO Auto-generated method stub
System.out.print("[");
for (int x=0;x<arr.length ;x++ )
{
if (x!=arr.length-1)
{
System.out.print(arr[x]+",");
}else
System.out.print(arr[x]+"]");
}
}
}