How to implement insertion sorting in web
This article mainly shows you "how to achieve insertion sorting in web", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve insertion sorting in web" this article.
Insert sort, also known as direct insert sort. It is an effective algorithm for sorting a small number of elements. Insert sort is one of the simplest sort method, insert sort and bubble sort, there is also an optimization algorithm, called split half insert, the following will explain insert sort in detail.
Algorithm step
The first to be sorted sequence the first element as an ordered sequence, the second element to the last element as an unsorted sequence.
The unsorted sequence is scanned from beginning to end, and each element scanned is inserted into the appropriate position of the ordered sequence. (if the element to be inserted is equal to an element in the ordered sequence, the element to be inserted is inserted after the equivalent element. )
Dynamic Picture to demonstrate insertion sort introduction insert sort introduction Code to implement JavaScript
Example
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
Example
Def insertionSort (arr): for i in range (len (arr)): preIndex = iMur1 current = arr [I] while preIndex > = 0 and arr [preIndex] > current: arr [preIndex+1] = arr [preIndex] preIndex-=1 arr [preIndex+1] = current return arrGo
Example
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
Example
Public class InsertSort implements IArraySort {@ Override public int [] sort (int [] sourceArray) throws Exception {/ / copy a pair of arr without changing the parameter content int [] arr = Arrays.copyOf (sourceArray, sourceArray.length); / / choose the appropriate location to insert from the element with subscript 1, because there is only one element with subscript 0, and the default is ordered for (int I = 1). I while (j > 0 & & tmp if (j! = I) {arr [j] = tmp;}} return arr;}} PHP
Example
Function insertionSort ($arr) {$len = count ($arr); for ($I = 1; $I $len; $iTunes +) {$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
Example
Void insertion_sort (int arr [], int len) {int iLINGON0) & & (arr [j] > key)) {arr [arr] = arr [j]; int len;} arr [Jaffa1] = key;}} C++
Example
Void insertion_sort (int arr [], int len) {for (int item1) & & (keyC#)
Example
Public static void InsertSort (int [] array) {for (int I = 1 array I for (int j = I-1 * * j > = 0 * * array -) {if (Array [j] > temp) {array [j + 1] = array [j]; array [j] = temp;} else break;} Swift
Example
For i in 1..let temp = arr [I] for j in (0..reversed () {if arr [j] > temp {arr.swapAt (j, jacks 1)} these are all the contents of the article "how to implement insertion sorting in web". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!