java插入排序之直接插入排序

时间复杂度为O(n^2)

代码:


package com.tuyao.paixu;


import java.util.Scanner;


public class Zhijiecharu
{
   public static void main(String args[])
   {
  System.out.println("请输入排序数字的个数:");
  Scanner scint=new Scanner(System.in);
  int geshu=scint.nextInt();
  
  Scanner sc=new Scanner(System.in);
 
  int [] a = new int[geshu];
  int temp;
 
  for(int i=0;i<geshu;i++)
  {
System.out.println("请输入数字:");
    a[i]=sc.nextInt();
  }
  
  
  
  for(int i=1;i<geshu;i++)
  {  
  for(int j=i;j>0;j--)
  {
  if(a[j]<a[j-1])
  {
  temp=a[j];
  a[j]=a[j-1];
  a[j-1]=temp;
  }
  
  
  }
  
 
  
  }
   
  for(int i=0;i<geshu;i++)
  {
  System.out.print(a[i]+" ");
  }
    
   }

}


插入排序法算法说明:
i=1 [46] 58 15 45 90 18 10 62
i=2 [46 58] 15 45 90 18 10 62
┌——┘
i=3 [15 46 58] 45 90 18 10 62
┌——┘
i=4 [15 45 46 58] 90 18 10 62
i=5 [15 45 46 58 90] 18 10 62
┌—————┘
i=6 [15 18 45 46 58 90] 10 62
┌————————┘
i=7 [10 15 18 45 46 5890] 62
┌—┘
i=8 [10 15 18 45 46 5862 90]

本人非常喜欢做开发,自己做的有关于互联网学习的网站-学亦知网站:www.xueyizhi.com,对应的论坛:bbs.xueyizhi.com,技术QQ群:175603095,非常欢迎大家过来一起学习!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值