The data structure of the story of the insertion sort

Source: Internet
Author: User

Long time no write something, recently many enterprises are recruiting interns, around a lot of people are also starting to go everywhere to find internships, engage in our panic, the pressure is great. Especially recently the Internet seems to be very fire appearance, as the traditional communication industry of tobacco and wine monks, also gave up their old line, began to turn to the soft team.

Unfortunately, we are learning the "signal and system", "communication principle" of the underlying theory of the communication course, although looks tall, in fact, there is no more than the modulation and demodulation code decoding. Although also began to learn C language from a freshman, but compared with the computer, regardless of all aspects of the difference is very far. Say more is a tear!! or pick up the data structure to lay a good foundation for it!

Sorting is a very important chapter in the data structure, and the classic sorting algorithm is mainly:

    • Insert Sort: Direct insert sort, binary insert sort, hill sort
    • Swap sort: bubble sort, quick sort
    • Select sort: Simple select sort, heap sort
    • Merge sort
    • Base sort

Hurry into the topic, why call insertion sort? Insert sort divides the sequence of orders into ordered and unordered areas, and the initial ordered area is the first record of the sequence to be sorted, and then each record in the unordered area is inserted into the ordered area in turn until the unordered area is empty.

1. Direct Insert Sort

voidDirectinsertsort (intArr[],intN) {    intOuter,inner;  for(outer=2; outer<=n; outer++) {arr[0] =Arr[outer];  for(inner=outer-1; arr[inner]>arr[0]; inner--) Arr[inner+1] =Arr[inner]; Arr[inner+1] = arr[0]; }}
Arr[0] Just as a staging unit for the record to be inserted, below.

2. Binary Insert Sort

voidBininsertsort (intArr[],intN) {    intOuter,inner; intBegin,end,mid;  for(outer=2; outer<=n;outer++) {arr[0] =Arr[outer]; Begin=1; End= outer-1; //mid = (begin+end)/2;         while(Begin <=end) {Mid= (begin+end)/2; if(arr[mid]>arr[0]) End= mid-1; Elsebegin= mid+1; }         for(inner=outer-1; inner>end;inner--) Arr[inner+1] =Arr[inner]; Arr[end+1] = arr[0]; }}

3. Hill sort

Hill sort: The entire backlog of records is divided into several sub-sequences, the direct insertion in each subsequence is sorted, the increment d is reduced, and the previous operation is repeated until d=1.

voidShellinsertsort (intArr[],intN) {    intD; intOuter,inner;  for(d=n/2;d >=1;d/=2){         for(outer=d+1; outer<=n;outer++) {arr[0] =Arr[outer];  for(Inner=outer;inner>d && arr[0]<arr[inner-d];inner-=d) Arr[inner]= arr[inner-d]; Arr[inner]= arr[0]; }    }}

Improvement: Each sub-sequence can be sorted by binary insertion.

4. Summary

Binary insert sort only when inserting records, because it is inserted in the ordered area, so when looking for the insertion position, you can use binary find technology.

Direct insertion Sort: Time complexity ο (N2), is a stable sort

Binary Insert Sort: Time complexity ο (nlog2n), stable sort

Hill sort: Time complexity depends on function with increment, unstable sort

The data structure of the story of the insertion sort

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.