[Question]
Sort a linked list using insertion sort.
Question]
Sort the linked list by inserting the sorting method
[Idea]
Directly engage in
[Code]
/*** Definition for singly-linked list. * struct listnode {* int val; * listnode * Next; * listnode (int x): Val (x), ne
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
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 clas
Count sort 10th Section count sort ExercisesFor an int array, write a counting sort algorithm that sorts the array elements.Given an int array a and the size of the array n, return the sorted array.Test examples:[1,2,3,5,2,3],6[1,2,2,3,3,5]Java (javac 1.7)Code Auto-Complete1
] 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
This article brings to you the content is about the Java sorting algorithm: Bubble sorting algorithm Implementation (code), there is a certain reference value, the need for friends can refer to, I hope you have some help.
Bubble Sort method is one of the most basic sorting methods, and the mechanism of bubble sorting is a simple
elements of another sequence directly to the end of the merge sequenceInstance
Raw data:
3 5 2 6 2The premise of merging is to separate the array, split in Split, and then divided into no more points to merge.
First-round dividers, index 2 ((0+4)/2=2) for the middle
[3 5 2] [6 2]
Second-round separation, separating [3 5 2]
[3 5] [2] [6 2]
Third-round separation, separate for [3 5]
[3] [5] [2] [6 2]
merging [3] [5]
[3 5] [2] [6 2]
Mergin
In the program Ape World, the language dispute is an eternal topic, here, I want to say: Let the argument come more violent.
Here, I want to achieve a quick sort in the same way. First, let's briefly review what a quick sort is.
Quick sort:
The essence of the fast platoon is "divide and conquer". Note: This is two aspects, and "min" is about taking advantage of t
An algorithm ideaEach step inserts a record to be sorted by the size of its key value into the appropriate position in the previously sorted file until all is inserted.For an array of n elements R, the first time from r[0]~r[n-1] to select the minimum value, and r[0] Exchange, the second time from r[1]~r[n-1] to select the minimum value, and r[1] exchange, ...., the second time from the r[i-1]~r[n-1] to select the minimum value, and r[i-1] Exchange, ..., n-1 times from r[n-2]~r[n-1] to the minim
At first I thought a lot more complicated, then I thought it over again and found that I thought it was too complicated.This is the first version of the re-revision, and the efficiency can continue to improve:if (Head==null | | head->next==null) return head;listnode* Nh=head;Head = head->next;Nh->next = NULL;while (Head!=null) {if (Head->val ListNode *a = head->next;Head->next = NH;NH = head;head = A;}else {if (Nh->next = = NULL) {Nh->next = head;Head = head->next;Nh->next->next = NULL;Continue}
This is a creation in
Article, where the information may have evolved or changed.
In the World of procedural apes, the struggle for language is an eternal topic, and here I want to say: Let the argument come more violently.
Here, I'm going to do a quick sort in the same way. First, let's briefly review what is a quick sort.
Quick sort:
The essence of the fast-lin
It is worth saying that the delete operation, delete operation we are divided into three kinds of situations:1. The node to be deleted has two children:Locate the node that corresponds to the maximum value in the left dial hand tree, or the smallest value in the right subtree, and assign the value of node to the nodes del that you want to delete, and then delete nodeIn fact, the real deletion is the substitution of Node,del only once.In order to facilitate understanding and operation, we put two
One, what is the direct insertion sort:
The first one compares the top two numbers, the second number is then inserted into an ordered table by size; The second one scans the third data with the first two digits, inserting the third number into an ordered table by size, and then, in turn, completes the entire sorting process after the (n-1) scan.
In simple terms, the array to be sorted is divided into order
1. Basic Ideas
By sorting the data that will be sorted into two separate parts, a part of all the data is smaller than the other part of all the data, and then the two parts of the data for the rapid sorting, the entire sequencing process can be recursive, so as to achieve the entire data into an ordered sequence.
2, the algorithm steps to get sorted array Select the appropriate number as the Sort datum number (in general, select the first number of a
As a sort algorithm, insertion sorting is stable. Time Complexity: O (N * n), space complexity: O (1 ). The best time complexity is O (n), and the worst and average is O (n * n ).
Algorithm idea: given an unordered array, we start with its second number and assign it to the mark variable to compare the size of mark with the previous number, if the number is smaller than the previous one, move the previous o
introduction of direct insertion sort
Each time the first element is removed from the unordered table, it is inserted into the proper position of the ordered table, so that the ordered table is still orderly.
The first one compares the top two numbers, the second number is then inserted into an ordered table by size; The second one scans the third data with the first two digits, inserting the third number i
Packagebackup class; Public classHill Inline Sort { Public Static voidMain (string[] args) {int[] A =New int[] {9, 8, 7, 6, 5, 4, 3, 2, 1, 10 }; Print (a); Shellsort (a); Print (a); } Public Static voidShellsort (int[] a) {intD = 3;//Incremental intGap = 1;//spacing//calculates the maximum gap value while(Gap d) {gap= Gap * D + 1; } //inter-group loops while(Gap > 0) { //a group of loops wi
Base sort 11th Section cardinal sort ExercisesFor an int array, write a cardinal sort algorithm that sorts the array elements.Given an int array a and the size of the array n, return the sorted array. The guaranteed element is less than or equal to 2000.Test examples:[1,2,3,5,2,3],6[1,2,2,3,3,5]Java (javac 1.7)
Bubble Sort Method : A record of a small number of keywords is like a bubble stepping up, the key word larger record is like a stone sinking, each trip has one of the largest stones to the bottom.
Algorithmic Nature: (The maximum is the key point, must be put to the last, so the cycle) each time from the first backward scrolling comparison, so that the maximum value of the bottom, the minimum rise one time, the last one forward (that is, the last jus
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.