using System;
class DataStructDemo
{
    
public static void insertSort(int[] arr)
    {
            
for (int i = 1; i < arr.length; i++)//从第二个元素开始
            {
               int temp 
= arr[i];
               int  j 
= i - 1;
                
while(arr[j] > temp)//从从当前元素往右边找插入点
                {
                    arr[j 
+ 1= arr[j];//后移
                    j--;
                    
if(j == -1)
                        
break;
                }
                arr[j 
+ 1= temp;//前面已经后移了,会留下一个空位置,现在就插入
            }
    }
    
public static output(int[] arr)
    {
        
for(int i = 0; i < arr.length;i++)
        {
            Console.Write(
"{0}\t",arr[i]);
        }
        Console.WriteLine();
    }
    
public static void Main()
    {
        
int[] arr = new int[]{34,2,45,67,89,90};
        Console.WriteLine(
"insert sort testing");
        output(arr);
        insertSort(arr);
        Console.WriteLine(
"insert sort OK");
        output(arr);
    }
}

相关文章:

  • 2022-01-19
  • 2022-01-06
  • 2022-12-23
  • 2021-07-21
  • 2022-12-23
  • 2021-06-19
  • 2022-03-10
猜你喜欢
  • 2021-06-04
  • 2021-09-14
  • 2021-07-01
  • 2021-12-23
  • 2021-10-08
  • 2022-12-23
  • 2021-07-25
相关资源
相似解决方案