Improved direct insertion sorting

Source: Internet
Author: User
The basic idea of directly inserting a sort (StraightInsertionSorting) is to regard n elements to be sorted as an ordered table and an unordered table. at the beginning, an ordered table contains only one element, an unordered table contains n-1 elements. The first element is retrieved from the unordered table every time during sorting... syntaxHighlighter. the basic idea of directly inserting a sort (Straight Insertion Sorting) is to regard n elements to be sorted as an ordered table and an unordered table, at the beginning, an ordered table contains only one element. an unordered table contains n-1 elements. during the sorting process, the first element is extracted from the unordered table each time and inserted to the appropriate position in the ordered table, make it a new Ordered Table. repeat n-1 times to complete the sorting process.
Insert a [I] to a [0], a [1],..., the specific implementation process in a [i-1] is: first assign a [I] to the variable t, then t with a [i-1], a [i-2],... for comparison, move the element greater than t to the right until a j (0 <= j <= i-1) is found ), if a [j] <= t or j is (-1), assign t to a [j + 1].
Improvement Method
A method that allows you to search for comparison operations and record moving operations in turn.
Specific practices:
Record the keywords of the record R [I] from right to left and record R [j] (j = i-1, I-2 ,..., 1) keyword comparison:
① If the keyword of R [j] is greater than the keyword of R [I], the R [j] is moved back to a position;
② If the keyword of R [j] is less than or equal to the keyword of R [I], the search process ends, and j + 1 is the insert position of R [I.
Records with larger keywords than those of R [I] have been moved backward, so the position of j + 1 has been vacated, you only need to insert R [I] directly to this position to complete the sort of insertion directly.
That is, compare and shift from the back of the new sequence;
Php code:
[Php]
Echo'
';
$ Arr = array (90,5, 0 );
Print_r (insertSort ($ arr ));
 
Function insertSort ($ arr ){
$ Res = array (); // the space to be inserted
$ Res [0] = $ arr [0]; // first put the first character in
For ($ I = 1; $ I $ C = count ($ res); // calculates the number of cycles.
For ($ j = $ c; $ j> = 0; $ j --){
If ($ res [$ j-1] <= $ arr [$ I]) {// $ J-1 is the value to be compared, $ j is the bit that is empty and to be inserted
$ Res [$ j] = $ arr [$ I];
Break; // The value has been inserted to end the for loop of this value.
} Else {
$ Res [$ j] = $ res [$ j-1]; // shift backward
}
}
}
Return $ res;
}
?>
Author: dats0407

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.