sort
1 package ACM; 2 public class Sort{ 3 public static int[] selectSort(int[] arr){ 4 int i,j,minPst,minPstIdx,jdx,temp,idx; 5 jdx=0; 6 minPstIdx=0; 7 minPst=0; 8 for(i=1;i<=arr.length;i++){ 9 minPst=i; 10 idx=i-1; 11 for(j=i;j<=arr.length;j++){ 12 minPstIdx=minPst-1; 13 jdx=j-1; 14 if(arr[minPstIdx] >arr[jdx]){ 15 minPst=j; 16 minPstIdx=jdx; 17 } 18 } 19 System.out.println("in "+i+" loop,the miniest position is "+minPst+", value is "+ arr[minPstIdx]); 20 temp=arr[minPstIdx]; 21 arr[minPstIdx]=arr[idx]; 22 arr[idx]=temp; 23 printArr(arr); 24 } 25 return arr; 26 } 27 public static int[] bubbleSort(int[] arr){ 28 int i,j,jdx,temp; 29 for(i=1;i<=arr.length-1;i++){ 30 System.out.print("in "+i+" loop,array is:"); 31 printArr(arr); 32 for(j=1;j<=arr.length-i;j++){ 33 jdx=j-1; 34 if(arr[jdx]>arr[jdx+1]){ 35 temp=arr[jdx+1]; 36 arr[jdx+1]=arr[jdx]; 37 arr[jdx]=temp; 38 } 39 } 40 } 41 return arr; 42 } 43 static void printArr(int[] arr){ 44 int i,idx; 45 for(i=1;i<=arr.length;i++){ 46 idx=i-1; 47 System.out.print(arr[idx]+" "); 48 } 49 System.out.print("\n"); 50 } 51 public static void main(String[] args){ 52 int[] arr={2,8,7,5,3,4,10,1,9,6}; 53 int i,idx; 54 System.out.println("before sort:"); 55 printArr(arr); 56 //arr=selectSort(arr); 57 arr=bubbleSort(arr); 58 System.out.println("after sort:"); 59 printArr(arr); 60 } 61 }