Java插入排序法InsertionSort

public class InsertionSort {
 public int[] insertionSort(int[] array){
  for(int i = 0;i<array.length;i++){
   int temp;
   if(i==0){
    temp = array[i];
    array[0] = temp;
   }else{
    for(int j = 0;j<i;j++){
     if(array[i]<array[j]){
      temp = array[j];
      array[j] = array[i];
      array[i] = temp; 
     }
    }
   }
  }  
  return array;
 }
 public static void main(String args[]){

 int[] array = {57,21,1,89,45,6,8,25,0,68};
  System.out.print(“排序前:”);
 for(int i = 0;i<array.length;i++){
  System.out.print(array[i]+”\t”);
  }
  System.out.print(“\r”);
 InsertionSort sort = new InsertionSort();
 int[] newArray = sort.insertionSort(array);
 System.out.print(“排序后:”);
  for(int i = 0;i<newArray.length;i++){
 System.out.print(newArray[i]+”\t”);
  }

}
 

点赞