1.软文推荐

2.软文推荐

3.软文推荐

插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的、个数加一的有序数据,算法适用于少量数据的排序,时间复杂度为O(n^2)。是稳定的排序方法。

算法步骤

将第一待排序序列第一个元素看做一个有序序列,把第二个元素到最后一个元素当成是未排序序列。

从头到尾依次扫描未排序序列,将扫描到的每个元素插入有序序列的适当位置。(如果待插入的元素与有序序列中的某个元素相等,则将待插入元素插入到相等元素的后面。)

动图演示
代码实现

JavaScript

实例

function insertionSort(arr) {
   var len = arr.length;
   var preIndex, current;
   for (var i = 1; i while(preIndex >= 0 && arr[preIndex] > current) {
           arr[preIndex+1] = arr[preIndex];
           preIndex--;
       }
       arr[preIndex+1] = current;
   }
   return arr;
}

Python

实例

def insertionSort(arr):
   for i in range(len(arr)):
       preIndex = i-1
       current = arr[i]
       while preIndex >= 0 and arr[preIndex] > current:
           arr[preIndex+1] = arr[preIndex]
           preIndex-=1
       arr[preIndex+1] = current
   return arr

Go

实例

func insertionSort(arr []int) []int {
       for i := range arr {
               preIndex := i - 1
               current := arr[i]
               for preIndex >= 0 && arr[preIndex] > current {
                       arr[preIndex+1] = arr[preIndex]
                       preIndex -= 1
               }
               arr[preIndex+1] = current
       }
       return arr
}

Java

实例

public class InsertSort implements IArraySort {

   @Override
   public int[] sort(int[] sourceArray) throws Exception {
       // 对 arr 进行拷贝,不改变参数内容
       int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);

       // 从下标为1的元素开始选择合适的位置插入,因为下标为0的只有一个元素,默认是有序的
       for (int i = 1; i while (j > 0 && tmp if (j != i) {
               arr[j] = tmp;
           }

       }
       return arr;
   }
}

PHP

实例

function insertionSort($arr)
{
   $len = count($arr);
   for ($i = 1; $i $len; $i++) {
       $preIndex = $i - 1;
       $current = $arr[$i];
       while($preIndex >= 0 && $arr[$preIndex] > $current) {
           $arr[$preIndex+1] = $arr[$preIndex];
           $preIndex--;
       }
       $arr[$preIndex+1] = $current;
   }
   return $arr;
}

C

实例

void insertion_sort(int arr[], int len){
       int i,j,key;
       for (i=1;i=0) && (arr[j]>key)) {
                       arr[j+1] = arr[j];
                       j--;
               }
               arr[j+1] = key;
       }
}

C++

实例

void insertion_sort(int arr[],int len){
       for(int i=1;iwhile((j>=0) && (key

C#

实例

public static void InsertSort(int[] array)
{
   for(int i = 1;i for(int j = i - 1;j >= 0;j--)
       {
           if(array[j] > temp)
           {
               array[j + 1] = array[j];
               array[j] = temp;
           }
           else
               break;
       }
   }
}

Swift

实例

for i in 1..let temp = arr[i]
   for j in (0..reversed() {
       if arr[j] > temp {
           arr.swapAt(j, j+1)
       }
   }
}

本文来源:www.lxlinux.net/9096.html,若引用不当,请联系修改。

相关文章 8

2

Linux下使用sed命令切割文本 4分钟前

sed是一个流(stream)编辑器,每次读取文本中的一行,放到内存的缓冲区(pattern space),然后使用sed命令处理pattern space中的内容,处理完成...

3

易优cms怎么样(易优cms伪静态) 4分钟前

目录:1、易优cms虎皮椒插件支付后不回调2、易优cms缺点3、易优cms 支持多少数据4、eyoucms好用吗5、易优程序能用在内网吗易优cms虎皮椒插件...

4

Linux下常用的录屏工具 7分钟前

录屏可以帮助用户解决一些问题,比如bug重现,或者是视频教学,比单纯的文字描述直观很多,本篇文章为大家分享一下Linux下常用的录屏工...

5

数据中心备用电源系统助解决停机问题 8分钟前

由于现在企业必须提供更高水平的正常运行时间,企业都开始关注数据中心备用电源。备用电源也越来越先进,管理员可以使用多种系统类...

7

大数据框架Hadoop和Spark的异同 11分钟前

在大数据框架中Hadoop和Spark可以说是很火的了,这俩个框架都是对数据进行存储的,到底有什么异同呢,他们各自有啥特点,本片文章将讲一...

8

淘宝网标题怎么优化(淘宝网标题怎么优化的) 12分钟前

目录:1、淘宝店铺的宝贝标题优化是什么意思。具体该怎么优化2、淘宝如何优化标题3、淘宝网店标题优化技巧4、淘宝标题怎么优化5、淘宝...