Eight sort algorithms that must be known [java Implementation] (2) Select sorting, insert sorting, and Hill algorithm [Detailed description ].
1. Select sorting
1. Basic Ideas: In the number of a group to be sorted, the smallest one is selected and the number at the first position is exchanged. Then, the smallest one is found in the remaining number and the number at the second position is exchanged, this
Java Hill Sort
Hill sort is a type of insertion sort, or you can narrow the increment method with an image name. The basic idea is to divide an array into several arrays, a bit like partition, but the division here is controlled by a constant d.
This 0
Position to the fir
First, the principle of insertion sortingInsert a record into an ordered table that is already sorted, resulting in a new, ordered table with an additional 1 record count. Starting with the first element, consider the first element as an ordered subsequence, then insert the second element from the start of the second element, then get an ordered table of two elements, then insert the third element, get an ordered table of three elements ..., and so on
the right subtree. denoted left-root-right.(3) subsequent traversal (LRD), first traversing the left subtree, and then traversing the right subtree, the last access to the root node. Denoted left-right-root.
Second, code writing 1, the tree node class definition 0
Package Com.lin;
/**
* Function Summary: * * Public
class TreeNode {public
Integer data;
/* The parent node of the node
/public TreeNode parent;
/* The
"Notes" bubble, select, insert three of the sorting algorithm time complexity is O (N2) level, the order is mainly to perform comparison and exchange (copy) operations, relatively speaking: choose a sort of more bubble sort to reduce the number of exchanges;The left side of the insertion sort is kept orderly."Reference
Algorithm analysis and improvement of bubble sort
The basic idea of exchange sorting is: 22 Compare the keywords of the records to be sorted, and find that the order of the two records is reversed to exchange until there is no reverse-order record.The main sorting methods for applying the basic idea of exchange sorting are bubble sort and quick sort.
Copy
value, move the previous value back for(intj = i; J > 0 Temp ) {Numbers[j]= Numbers[j-1]; } Numbers[j]=temp; } } Public Static voidMain (string[] args) {intA[] = {46,79,56,38,40,84}; Insertsort (a); System.out.println (Arrays.tostring (a)); }}Algorithm Performance Analysis:Time complexity: When the array is ordered, there is no move operation, at this time the complexity is O (N), when the array is reversed, the number of comparisons to the maximum-for subscrip
Sort java string arrays by size and java string Arrays
If two strings are compared directly, The packet: the operator> is undefined for The argument type (s) java. lang. String, java. lang. String is incorrect.
To compare the size of a string, you can use the string length o
Two fork Sorting tree introduction
We know that the binary tree, each node has a maximum of 2 Shang trees, known as the Saozi right subtree.The binary sort tree, apparently also a binary tree, has a more salient feature that renders sorting on the data field .In the two-fork sort tree , the data field of the left subtree of each tree is less than its root node value, and the data field of the right subtree
Select sort-Simple Select sortThe basic idea of sorting is: Each trip selects the smallest record of the keyword from the record to be sorted, placing it in the final order of the ordered sub-series until all records have been sorted. Simple selection sort is also called direct selection sort.Basic algorithm:The given sequence to be sorted A[0....N], the first time from a[0]~a[n-1] to select the minimum val
Insert SortIt works by constructing an ordered sequence, for unsorted data, to scan from backward forward in the sorted sequence, to find the appropriate position and insert it.The insertion sort is implemented on the implementation, usually in the in-place sort (in-place order, that is, the order of the extra Space of O (1) is only used),Thus, in the process of
During the Chinese New Year, I read the introduction of the algorithm in this book, I feel a good benefit. Here also based on the algorithm in the introduction of the algorithm used in Java implementation.In the first chapter we start with the sort, and the principle of the insertion sort is simple, just like when we p
The time complexity of hill sort is O (n*log2n) The spatial complexity of O (1) is an unstable sort algorithm
Thought: Hill sort is also a sort of insertion method, which is actually a grouping insertion method. First, take a int
[min_index]=temp;
}
}
}
Efficiency O (n²), suitable for sorting small lists.
Third, insert sort
Copy Code code as follows:
void Insertsortarray ()
{
The for (int i=1;i{
int temp=arr[i];//temp marked as unordered first element
int j=i-1;
while (j>=0 arr[j]>temp)/* Compares temp with the sorted element from small to large
balance is broken, just one rotation is enough to solve the problem. And the need to rotate is the insertion node is located, the left and right sub-tree height difference is greater than 1 of the kid Tree.Since the AVL tree is a balanced lookup tree, it is easier to remember from the normal two-fork lookup tree.Binary tree insertion is performed recursively, which can express the idea of the algorithm ver
Reconstruction: improve the design of existing code (commentary version); Comment sort
Comments sort
In the past decade, it has been difficult to bypass martinfowler's classic book to discuss how to improve the quality of code. This book has already influenced several generations of programmers, and may continuously i
Quick sort a listen to the name may feel fast, but his algorithm time complexity is the worst case is the same as the insertion order. The reason for the quick sort is that his average efficiency is faster than the heap sort, and the fast sort is based on the idea of splitti
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.