Excellent algorithm series-sorting algorithm (2)

Source: Internet
Author: User

Insert sort
It is to insert a number into an ordered data sequence, but it is required that the data sequence still be ordered after insertion. The basic operation of insert sorting is to insert a data into ordered data that has been sorted in order to obtain a new ordered data with a number plus one. The algorithm applies to sorting a small amount of data. The process is to divide the array to be sorted into two parts: the first part contains all the elements of this array, except for the last element, the second part only contains this element. After sorting the first part, insert the last element to the position in the first part of the order.

Insert sorting includes direct insertion sorting, binary insertion sorting (also called semi-insertion sorting), linked list insertion sorting, and Hill sorting (also called downgrading incremental sorting ).

 

 

To facilitate the description, the sequence table type is defined as follows:

# Define Max size 1000
Typedef int KeyType;
Typedef struct {
KeyType key;
InfoType otherinfo;
} RecType;

Typedef struct {
RecType r [MAXSIZE + 1];
Int length;
} SqList;
Sort by insert directly:
Set R [1... n] to n records to be sorted, R [1... I-1] has been sorted by keywords from small to large.

Void Dinsert (SqList & q)
{
Int I, j, k;
RecType t;
For (I = 1; I <q. length; I ++)
{
For (t = q. r [I], j = I-1; j> = 0 & t. key <q. r [j]. key; j --)
Q. r [j + 1] = q. r [j];
Q. r [j + 1] = t;
}
}
Hill sorting:
Shell Sort is a Sort of insert. Is an improvement for directly inserting sorting algorithms. This method, also known as downgrading incremental sorting, was named after DL. Shell was proposed in 1959.

Basic Thinking of hill sorting:

Take an integer d1 smaller than n as the first increment, and divide all records of the file into d1 groups. All records whose distance is multiples of d1 are placed in the same group. Insert and sort data in each group first. Then, repeat the group and sort data in the second incremental d2 <d1 until the incremental dt = 1 (dt <dt-l <... <D2 <d1), that is, all records are placed in the same group for direct insertion sorting.

This method is essentially a grouping insertion method.

Shell sorting process of the given instance:

  

 

For the first time, the length of half of the linear table is the step size, and then halved each time until the step size is 1. The algorithm is as follows:

Void Shsort (SqList & q)
{
Int j, K, h;
RecType y;
For (h = q. length/2; h> 0; h/= 2)
For (j = 1; j <= h; j ++)
{
Y = q. r [j];
For (k = j-h; k> = 0 & y. key <q. r [k]. key; k-= h)
Q. r [k + h] = q. r [k];
Q. r [k + h] = y;
}
}
Well, let's talk about it first.

Author's "flute column"
 

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.