首页 > 编程语言 > 详细

插入排序

时间:2016-09-19 01:06:59      阅读:198      评论:0      收藏:0      [点我收藏+]
 1 /**
 2  * 功能:直接插入排序
 3  * 原理:将一个记录插入到已经排好的有序表中,从而得到一个新的,记录数增1的有序表
 4  * 直接插入排序时间复杂度:(1)最好的情况下,为o(n) (2)最坏的情况下为o(n^2)
 5  * */
 6 public class InsertSort {
 7 
 8     public int[] insertSort(int[] array){
 9         
10         for(int i=1; i<array.length; i++){
11             int temp = array[i];
12             int index = i;
13             
14             for(int j=i-1; j>=0; j--){
15                 if(array[j] > temp){
16                     array[j+1] = array[j];
17                     index = j;
18                 }
19             }
20             
21             array[index] = temp;
22         }
23         
24         return array;
25     }
26 }

 

插入排序

原文:http://www.cnblogs.com/jiangyi-uestc/p/5883539.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!