Direct insertion and direct quotation

Source: Internet
Author: User

Direct insertion and direct quotation
Direct insertion sorting direct insertion sorting is a simple insert sorting method. Its basic idea is: insert the records to be sorted into an ordered sequence one by one based on their key code values until all records are inserted. A new ordered sequence is obtained. [1] For example, a group of records that are known to be sorted are: 60, 71, 24, 3, 66. Assume that during the sorting process, the first three records have been re-arranged in ascending order of key code values to form an ordered sequence: 49,60, 71 Insert the 4th records (I .e. 11) in the records to be sorted into the above ordered sequence, get a new sequence containing four records. First, you should find the insert position for 11 and then insert it. It can be said that 11 is placed in the first unit of the array r [0]. This unit is called the monitoring whistle, and then searches from right to left from 71, 11 is less than 71, shift 71 to the right one location, 11 to 60, 60 to the right, 11 to 49, and 49 to the right, then compare the value of 11 with r [0]. If 11 is greater than or equal to r [0], the insert position is r [1]. Assume that 11 is greater than the first value r [1]. Its insertion position should be between r [1] and r [2]. Since 60 has shifted to the right, the position left is exactly 11. the subsequent records are inserted into the ordered sequence one by one according to the same method. If the number of records is n, the n-1 sorting can be continued. Algorithm idea of directly inserting sorting: (1) setting monitoring record r [0], assigning the value of the record to r [0]; (2) setting the start position j; (3) search in the array, and move the j-th record after the search until r [0]. key ≥ r [j]. (4) Insert r [0] to the position of r [j + 1. Insert the sorting algorithm directly: public void zjinsert (Redtype r [], int n) {int I, j; Redtype temp; for (I = 1; I <n; I ++) {temp = r [I]; j = I-1; while (j>-1 & temp. key <r [j]. key) {r [j + 1] = r [j]; j --;} r [j + 1] = temp ;}}

Related Article

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.