Java Learning Data-java Common algorithm-Direct insertion algorithm

Source: Internet
Author: User

SortAscending source Program
public class SortAscending {
Public sortascending () {
SYSTEM.OUT.PRINTLN ("Direct insertion sorting");
}

public static int[] SortAscending (int[] with) {//Direct Insert method increment sort
int length = With.length; The length of the array to be queued
int[] temp = new Int[length]; Create a new array to hold the sorting results
Temp[0] = with[0]; Initializes the element of the temp array to with[0]
for (int i = 1; i < length; i++) {//traversal with array
for (int j = i-1; J >= 0; j--) {
if (With[i] >= temp[j]) {//If the element in the sequence to be sorted is greater than or equal to an element in an ordered sequence, insert
Temp[j + 1] = With[i];
Break
} else {
Temp[j + 1] = Temp[j];
If the traversed element is less than the array that already exists in temp, the space that will be temp[j] in the next traversal of the inner loop will be filled by with[i], or continue to reserve space
if (j = = 0)
TEMP[J] = With[i]; With[[i] is the smallest of the ordered sequence, so it is at the beginning
}
}
}
return temp;
}

public static void Main (string[] args) {
Int[] Test1 = {49 38 65 97 76 13 27 49}; Test array
Int[] Temp1; Intermediate variables
Temp1 = sortascending (test1);

for (int i = 0; i < temp1.length; i++) {
System.out.print (Temp1[i] + "");
}
}
}
3.5.1.2 Program Run Results:
13 27 38 49 49 65 76 97
3.5.2 Source program Disclosure
The implementation of the direct insertion method can be in many ways and is not unique. The algorithm reduces the time complexity of the algorithm by increasing the complexity of the space by creating a new array to store the comparison results.
Traversal is an essential process for each sorting algorithm, sequentially inserting the data element to be inserted into the appropriate position of the sorted element collection by the size of its keyword value. The number of elements in a subset starts from only one element, and then grows, and is sorted when the subset size is eventually the same as the collection size.
Data structure of the direct insertion algorithm:
? Array to sort with: The array contains with.length elements
? An array that holds a subset of the sorted array: Temp
Principle of direct insertion algorithm:
The sorting principle of the direct insertion sorting method is to arrange a set of unordered numbers in a row, the first number on the left is the number that has been sorted, and the other numbers are unsorted numbers. The unordered numbers are then inserted from left to right into the sorted numbers.
? The direct insert sort specific algorithm is described below:
? Starting with the first element, the element can be thought to have been sorted;
? Takes the next element and scans the sequence of elements that have been sorted from the back forward;
? If the element (sorted) is greater than the new element, move the element to the next position;
? Repeat step 3 until you find the sorted element is less than or equal to the position of the new element
? Inserts a new element into the position;
? Repeat step 2;
? Direct Insert Sort Example
49 38 65 97 76 13 27 49
49
38 49
39 49 65 97
38 49 65
38 49 65) 76 97
13 38 49 65 76 97
13 27 38 49 65 76 97
13 27 38 49 49 65 76 97

Java Learning Data-java Common algorithm-Direct insertion algorithm

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.