插入排序

直接插入排序

 

在要排序的一組數中,假設前面(n-1)[n>=2] 個數已經是排好順序的,現在要把第n個數插到前面的有序數中,使得這n個數也是排好順序的。

 

代碼實現:

public class InsertSort {

 

 public void insertSort() {

  int array[] = {54,23,52,76,35,25,87,64,43};

  int temp = 0;

  for(int i=1;i<array.length;i++){

   temp = array[i];

   int j = i-1;

   for(;j>=0&&temp<array[j];j--){

    array[j+1] = array[j];

   }

   array[j+1] = temp;

  }

  

  for(int i=0;i<array.length;i++){

   System.out.print(array[i]+"  ");

  }

 }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章