Description of Data Structure insertion sorting and Hill sorting instances

Source: Internet
Author: User

Description of Data Structure insertion sorting and Hill sorting instances
I. Insert sorting

<1> introduction:The description of Insertion Sort is a simple and intuitive sorting algorithm. Its working principle is to build an ordered sequence. For unordered data, scan the sorted sequence from the back to the front, locate the corresponding position, and insert it. Insert sorting usually uses in-place sorting (that is, sorting of the extra space of O (1). Therefore, during the scanning from the back to the forward, the sorted elements need to be moved backward repeatedly to provide the insert space for the new elements. <2> ideas: Perform the following operations from the first element: for the current element, you can think that the previous part of the current element has been ordered; take the next element of the current position as the value to be sorted, scans the sorted element sequence from the back to the front. If the element (sorted) is greater than the new element, move the element to the next position; insert the new element into the position where the sorted element is smaller than or equal to the new element; Use the subscript from 0 to the number of array elements-1 as the end to perform the above operations. The purpose of each loop is to place the current tmp value in the position of the previous sequence, make the current part of the sequence orderly. <3> Implementation:
void InsertSort(int* arr, int sz){    for (int i = 0; i < sz - 1; i++)    {        int end = i;        int tmp = arr[end + 1];        while (end >= 0)        {            if (arr[end] > tmp)            {                arr[end + 1] = arr[end];                end--;            }            else                break;        }        arr[end + 1] = tmp;    }}
<4> graphical process:

Ii. Hill sorting <1> introduction:Hill sorting, also known as the descending incremental sorting algorithm, is a fast and stable improved version of insert sorting. Hill sorting proposes an improvement method based on the following two attributes of insert sorting: 1. Insert sorting is efficient when operations are performed on data in almost sorted order, that is, linear sorting can be achieved; 2. Insert sorting is generally inefficient, because insert sorting can only move one bit of data at a time. <2> ideas and sorting results: ①:First, the entire sequence of elements to be arranged is divided into several sub-sequences (composed of elements separated by an increment) for direct insertion and sorting; ②:Then, the incremental data is reduced in sequence and then sorted. When the elements in the entire sequence are basically ordered (the increment is small enough), all the elements are directly inserted and sorted.

The general idea is the same as that of insert sorting. The difference is that a gap value, that is, the gap value, can be specified in the hill sorting. The gap sequence can be sorted first before the gap value is reduced, compare them to the last one.

<3> Implementation:
void ShellSort(int* arr, int sz){    int gap = 3;    while (gap > 1)    {        gap = gap / 3 + 1;        for (int i = 0; i < sz - gap; i++)        {            int end = i;            int tmp = arr[end + gap];            while (end >= 0)            {                if (tmp < arr[end])                {                    arr[end + gap] = arr[end];                    end -= gap;                }                else                    break;            }            arr[end + gap] = tmp;        }    }
<4> graphical process:

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.