This is a creation in
Article, where the information may have evolved or changed.
This algorithm is still my postgraduate examination of the time to read. There are basically two kinds of insertion sort, head interpolation and tail interpolation method. The difference is whether the inserted position is the head or the tail.
Simply say the idea of inserting a sort
The insertion sort in the introduction to algorithms is handy. The core idea is quasi, the realization is prevail.#include Algorithm does not return to the insertion sort (C version)
LeetCode -- Insertion Sort ListDescription:Sort a linked list using insertion sort.Ideas:Insert a sort list class, add the records to the list one by one, and use the list to create a new linked list.Implementation Code:
/** * Definition for singly-linked list. * public class ListNode { * public int val; * publ
Package Algorithm;public class Straightinsertsort {/** * plus Sentinel Direct insertion sort * @param arr */public static void Insertsort (int[] ar R) {int i,j;for (i=2;i The direct insertion sort of the surveillance whistle
] is moved back one bit, at this point, the post-shifted bit covers the key, and then i--, and loop repeat, until the end of the loop, the end of the loop , from small to large arrangement, but at this time because of the previous i--reasons, after jumping out of the loop, the first step to save the key value to a[i+1]; must be i+1. This ensures that the values to be sorted are changed from left to right after this sort.The analysis of the text is still not straightforward enough to be understoo
not change the seatif (Array[i] int sign = 0; Set the signal, default first, if larger than the previous element, then insert in the element seatfor (int j = i; j > 0; j--) {//Search from back to forwardif (Array[i] > Array[j-1]) {sign = j;Break}}Place the value of the signal at the TEMP variable, insert the value of array[i] after the signal elementtemp = array[sign];Array[sign] = Array[i];Count + = 1;Swaps the values so that the elements in front of the array I and behind sign are shifted one
Simple insert sort, always time out, leave this record for a moment.Class solution: # @param head, a listnode # @return a listnode def insertionsortlist (self, head): if head = = No NE or head.next = = None:return Headpsuhead = ListNode ( -1) while head:tail = Psuheadheadnext = Head.nextwhile Tail.next and Tail.next.val
Last executed input:
{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,2
Test instructions: Give a list of links to implement the insertion sort.Idea: O (1) space, O (n*n) complexity. String up the row with the other end of the list, and the linked list is deleted and returned to the list.1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7 * };8 */9 classSolution {Ten Public: Onelistnode* Insertionsortlist (listnode*head) { A
DescriptionSort a linked list using insertion sort.ExampleGiven 1->3->2->0->null , return 0->1->2->3->null .Problem solving: The list is sorted by inserting. A very simple topic, but there are still a lot of problems.Summarize the problems encountered:(1) Without a head node, in order to facilitate the construction of a, return to the first node of the next on the line.(2) There is no need to always in the original linked list of tangled, can apply fo
Problem description:
Sort a linked list using insertion sort.
Solution:
1 public class Solution { 2 public ListNode insertionSortList(ListNode head) { 3 int length = 0; 4 ListNode p = head; 5 while (p != null) { 6 length++; 7 p = p.next; 8 } 9 10 if (length
Error-prone points1 not in situ sort, surprised!!2 to reposition pre!! Public classSolution { PublicListNode insertionsortlist (ListNode head) {//It 's easy to figure out how to insert it. if(head==NULL|| Head.next = =NULL)returnHead; ListNode h=NewListNode (-1); //h.next = head;//not in-situ sortingListNode cur =Head; while(cur!=NULL){ //to redefine the point of the Pre!!! ListNode pre =h; ListNode T=Cur.next; while(pre.next!=NULL
The method of directly inserting the sort (straight insertion sort) is:
Each time the first element is extracted from the unordered table, it is inserted to the proper position of the ordered table, so that the ordered table is still ordered.
The procedure is as follows:
Initial sequence:I = 1 [46] 58 15 45 90 18 10 62BytesI = 2 [46 58] 15 45 90 18 10 62Sna
Interview 24: Encode direct Insert sort the original answer:#include Thought analysis: The whole idea no matter what mistake. Error analysis such as the following: for (i = 2; I array[0]//selected as Sentry causes the first number in the sequence to always change and cannot be sorted correctly >>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>> Fix the answer: #incl
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.