This article describes in detail how to implement insertion sorting of the classic sorting algorithm using JavaScript, which has some reference value, if you are interested, you can refer to the code implementation of insertion sorting. Although it is not as simple and crude as Bubble sorting and selection sorting, its principle should be the easiest to understand, because anyone playing poker should be able to understand it in seconds. Like sorting cards with one hand, at the beginning, our left hand was empty and the cards on the table were down. Then, each time we take a card from the table and insert it to the correct position in the left hand. In order to find the correct position of a card, we compare it with each card in the hand from right to left, and the cards on the left are always sorted, it turns out these cards are cards at the top of the cards on the desk.
1) algorithm principle
The description of Insertion-Sort is a simple and intuitive sorting algorithm. Its working principle is to build an ordered sequence. For unordered data, scan the sorted sequence from the back to the front, locate the corresponding position, and insert it. Insert sorting usually uses in-place sorting (that is, sorting of the extra space of O (1). Therefore, during the scanning from the back to the forward, the sorted elements need to be moved backward repeatedly to provide the insert space for the new elements.
2) algorithm description and implementation
In general, insert sorting is implemented on the array using in-place. The specific algorithm is described as follows:
<1> starting from the first element, this element can be considered to have been sorted;
<2> extracts the next element and scans the sorted element sequence from the back to the front;
<3> If the element (sorted) is greater than the new element, move the element to the next position;
<4> repeat Step 3 until you find the position where the sorted elements are smaller than or equal to the new elements;
<5> Insert the new element to this position;
<6> repeat Step 2 ~ 5.
3) JavaScript code implementation
function insertSort(arr) { for (var i = 1; i < arr.length; i++) { var temp = arr[i]; var j = i - 1; while (j >= 0 && arr[j] > temp) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = temp; } return arr; } var arr = [1, 45, 37, 5, 48, 15, 37, 26, 29, 2, 46, 4, 17, 50, 52]; console.log(insertSort(arr));
Improved insert sorting: Binary Search is used to locate the insert position.
Steps:
<1> starting from the first element, this element can be considered to have been sorted;
<2> retrieve the next element and locate the first number greater than the sorted element sequence in binary mode;
<3> Insert the new element to this position;
function binaryInsertionSort(arr) { for (var i = 1; i < arr.length; i++) { var key = arr[i],left = 0,right = i - 1; while (left <= right) { var middle = parseInt((left + right) / 2); if (key < arr[middle]) { right = middle - 1; } else { left = middle + 1; } } for (var j = i - 1; j >= left; j--) { arr[j + 1] = arr[j]; } arr[left] = key; } return arr; } var arr = [1, 45, 37, 5, 48, 15, 37, 26, 29, 2, 46, 4, 17, 50, 52]; console.log(binaryInsertionSort(arr));
4) algorithm analysis
Optimal Condition: Input arrays are arranged in ascending order. T (n) = O (n)
Worst case: the input array is sorted in descending order. T (n) = O (n2)
Average: T (n) = O (n2)
The above is all the content of this article. I hope it will help you learn and support PHP.
For more articles about how to use JavaScript to insert a sorting algorithm to a classic sorting algorithm, please follow the PHP Chinese website!