插入排序

直接插入排序

 

在要排序的一组数中,假设前面(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]+"  ");

  }

 }

 

你可能感兴趣的:(插入排序,insertsort)