JavaScript Algorithms, Python Algorithms, Go Algorithms, java Algorithms, Insertion Sorting of C# Algorithms

Keywords: Python Java Javascript

Common internal sorting algorithms include insertion sorting, Hill sorting, selection sorting, bubble sorting, merge sorting, fast sorting, heap sorting, cardinal sorting, etc. Summarize with a picture:

Insertion sort

Insertion Sort is a simple and intuitive sorting algorithm. Its working principle is to construct an ordered sequence, scan backward and forward in the ordered sequence, find the corresponding position and insert it into the unordered data. In the implementation of insertion sorting, in-place sorting is usually used (that is, sorting using only the extra space of O(1). Therefore, in the process of scanning backwards and forwards, it is necessary to move the sorted elements backwards step by step repeatedly to provide insertion space for the latest elements.
Like bubble sort, insertion sort also has an optimization algorithm called split-and-half insertion.

  1. Algorithm steps

  2. Consider the first element of the first sequence to be sorted as an ordered sequence, and the second element to the last element as an unordered sequence.

  3. Scanning the unordered sequence from beginning to end, inserting each element scanned into the appropriate position of the ordered sequence. (If the element to be inserted is equal to an element in an ordered sequence, the element to be inserted is inserted after the equivalent element.)

2 Motion Map Demonstration

  1. JavaScript Code Implementation


function insertionSort(arr) {
    var len = arr.length;
    var preIndex, current;
    for (var 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;
}
  1. Python Code Implementation

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
  1. Go Code Implementation

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
}

6 Java implementation

 public static void insertion_sort( int[] arr ){
    for( int i=0; i<arr.length-1; i++ ) 
    {   
        for( int j=i+1; j>0; j-- ) 
        {
            if( arr[j-1] <= arr[j] )
                break;
            int temp = arr[j];
            arr[j] = arr[j-1];
            arr[j-1] = temp;
        }
    }
}

Another version of 7 Java

  public static void insertion_sort(int[] arr){
              for (int i = 1; i < arr.length; i++ ) {
                      int temp = arr[i];
                      int j = i - 1;  
   
                      for (; j >= 0 && arr[j] > temp; j-- ){
                              arr[j + 1] = arr[j];
                      }
                      arr[j + 1] = temp;
              }
      }

8 C# implementation

public static void InsertSort(double[] data){
        int i, j;
        var count = data.Length;
        for (i = 1 ; i < count ; i++) {
        var t = data[i];
            for(j = i - 1; j >= 0 && data[j] > t; j--)
            data[j + 1] = data[j];
            data[j + 1] = t;
        }
}

Hope to exchange technology together, interested in adding qq invitation group: 525331804 whole stack technology development qqq group: 581993430

Posted by Wolverine68 on Fri, 15 Feb 2019 01:30:19 -0800