Data structure practice--improvement of merging sorting algorithm

Source: Internet
Author: User

This article is for the [Data Structure Basic series (9): sort] project.

"Improvement of the project-merge sorting algorithm"
By using high-efficiency algorithms such as merge sort and fast sorting, when the data elements are small (such as n≤64), it is often used directly to the algorithm of higher complexity such as direct insertion sorting algorithm. In doing so, there are some benefits, such as reducing the allocation, recovering the frequency of the temporary storage area, and reducing the recursive hierarchy by a quick sort.
Try to re-implement the merge sorting algorithm according to the above ideas.

[Reference Solution]

#include <stdio.h>#include <malloc.h>#include <stdlib.h>#include <time.h>#define MINLENGTH //min. segment Length typedef intKeyType;//define keyword typestypedef Charinfotype[Ten];typedef struct          //Record type{KeyType key;//keyword ItemsInfoType data;//Other data item, type InfoType} RecType;//Sort record type definitionvoidGetData (RecType *&r,intN) {Srand (0)); R= (rectype*)malloc(sizeof(RecType) *n); for(intI=0; i<n; i++) r[i].key= rand ();printf("%d records were generated \ n", n);}//To R[low. High] Sort by ascending order by direct insertionvoidInsertsort (RecType r[],intLowintHigh) {intI,j; RecType tmp; for(I=low; i<=high; i++)        {Tmp=r[i]; j=i-1;//Right-to-left in the ordered area R[low. I-1] Where to find R[i]         while(J>=low && Tmp.key<r[j].key) {r[j+1]=R[J];//Move a record with a keyword greater than R[i].keyj--; } r[j+1]=tmp;//Insert r[i at j+1]}}//Merge two ordered tablesvoidMerge (RecType r[],intLowintMidintHigh) {RecType *r1;intI,j,k; I=low,j=mid+1, k=0;//k is the subscript of R1, I, J are the subscript of 1th and 2 respectively .r1= (RecType *)malloc((high-low+1)*sizeof(RecType));//Dynamic allocation of space     while(I<=mid && J<=high)//Cycle after the 1th and 2nd paragraphs are not scanned        if(R[i].key<=r[j].key)//Put the records in the 1th paragraph into the R1{R1[k]=r[i];            i++;        k++; }Else                            //Put the records in the 2nd paragraph into the R1{R1[K]=R[J];            j + +;        k++; } while(I<=mid)//Copy the remainder of the 1th paragraph to R1{R1[k]=r[i];        i++;    k++; } while(J<=high)//Copy the remainder of the 2nd paragraph to R1{R1[K]=R[J];        j + +;    k++; } for(k=0, I=low; i<=high; k++,i++)//Copy the R1 back into RR[I]=R1[K];}//One-trip mergervoidMergepass (RecType r[],intLengthintN//merge the entire sequence of numbers{intI for(i=0; i+2*length-1<n; i=i+2*length)//Merge length two adjacent sub-tableMerge (r,i,i+length-1, i+2*length-1);if(i+length-1<n)//Two remaining sub-tables, which are less than lengthMerge (r,i,i+length-1, N-1);//Merge these two sub-tables}//Bottom-up two-way merge algorithm, but too short segment, with direct insert completevoidMergeSort (RecType r[],intN) {intlength, I; for(i=0; i<n;i+=minlength)//First by the shortest segment, with the insertion sort to make it segmented and orderlyInsertsort (R, I, (i+minlength-1<n)? (i+minlength-1): N); for(Length=minlength; length<n; length=2*length)//To merge{Mergepass (r,length,n); }}intMain () {inti,n=10000;    RecType *r;    GetData (R, N); MergeSort (R,n);printf("After sorting (top 300): \ n"); I=0; while(i< -)    {printf("%12d", R[i].key); i++;if(i%5==0)printf("\ n"); }printf("\ n");printf("After sorting (after 300): \ n"); I=0; while(i< -)    {printf("%12d", r[n- -+i].key); i++;if(i%5==0)printf("\ n"); }printf("\ n"); Free(R);return 0;}

Data structure practice--improvement of merging sorting algorithm

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.