Sort (C language implementation)

Source: Internet
Author: User

Reading data structure and algorithm analysis

Insert Sort

Core: Used from position 0 to position p are all sorted

So starting from position 1, if the current position is not correct, and the previous element is repeatedly exchanged for reordering

Realize
void InsertionSort(ElementType A[], int N){    int i,P;    ElementType Tmp ;    for(P = 1;P < N;P++)    {        Tmp = A[P] ;        for(i = P;i > 0 && A[i-1] > Tmp;i--)            A[i] = A[i-1] ;        A[i] = Tmp ;    }}


Hill sort

After sequencing using the HK increment sequence, there is a

A[i] \leq A[i+hk]   

Important properties: A HK sorted file will maintain the HK sort in the subsequent sort

Realize
    • HT = [N/2]
    • HK = [HK+1/2]
void ShellSort(ElmentType A[], int N){    ElementType Tmp ;    int i,j,Increment ;    for(Increment = N/2;Increment > 0;Increment /= 2)        for(i = Increment;i < N;i++)        {            Tmp = A[i] ;            for(j = i;j <= Increment;j -= Increment)                if(Tmp < A[j-Increment])                    A[j] = A[j-Increment] ;                else                     break ;            A[j] = Tmp ;        }}
Heap Sort

Based on the original two-fork heap

Build two fork heap, perform deletemax operation, save to array (saving space can be stored in the current array in reverse)

Realize
#define LeftChild(i) (2 * i + 1) ;void Percdown(ElementType A[],int i,int N){    int Child ,i;    ElementType Tmp ;    for(Tmp = A[i]; LeftChild(i) < N-1;i = Child)    {        Child = LeftChild(i) ;        if(Tmp < A[Child])            A[i] = A[Chile] ;        else            break ;    }    A[i] = Tmp ;}void HeapSore(ElementType A[],int N){    int i ;    for(i = N/2 ;i >= 0;i--)        Percdown(A,i,N) ; //建立二叉堆    for(i = N-1; i > 0; i--)    {        Swap(&A[0],&A[i]) ;        Percdown(A,0,i) ;    }    }
Merge sort

The basic idea is to merge two sorted tables

Realize

Recursive merging

void Msort(ElementType A[],ElementeType TmpArray[],int Left,int Right){    int Center ;    if(Left < Right)    {        Center = (Left + Right) /2 ;        Msort(A,TmpArray,Left,Right) ;        Msort(A,TmpArray,Center + 1,Right) ;        Merge(A,TmpArray,Left,Center + 1,Right) ;    }}

Driver Program

void Mergesort(ElementType A[],int N){    ElementType *TmpArray ;        TmpArray = malloc(N * sizeof(ElementType)) ;    if(TmpArray != NULL)    {        Msort(A,TmpArray,0,N-1) ;        free(TmpArray) ;    }    else        FatalError("内存不足") ;}

Merging functions

void Merge(ElementType A[],ElementType TmpArray[],int Lpos,int Rpos,int RightEnd){    int i,LeftEnd,NumElements,TmpPos ;        LeftEnd = Rpos - 1;    TmpPos = Lpos ;    NumElement = RightEnd - Lpos + 1 ;        while(Lpos <= LeftEnd && Rpos <= RightEnd)        if(A[Lpos] <= A[Rpos])            TmpArray[TmpPos++] = A[Lpos++] ;        else            TmpArray[TmpPos++] = A[Rpos++] ;                while(Lpos <= LeftEnd)        TmpArray[TmpPos++] = A[Lpos++] ;    while(Rpos <= RightEnd)        TmpArray[TmpPos++] = A[Rpos++]        for(i = 0;i < NumElement ;i++,RightEnd--)        A[RightEnd] = TmpArray[RightEnd] ;}
Quick Sort

The same as the merge sort is divided into recursive algorithm, in the decimal group efficiency is not inserted sort well

    1. If the number of elements in S is 0 or 1, the return
    2. Whichever element in S is V, called the hub element
    3. Divides the remaining elements of S into two disjoint sets
    4. Return to Quicksort (S1), continue to select V, continue quicksort (S2);
Implement SELECT Pivot Element

Three-digit median-value segmentation method

ElementType Median3(ElementType A[],int Left,int Right){    int Center = (Left + Right) / 2 ;        if(A[Left] > A[Center])        Swap(&A[Left],&A[Center]) ;    if(A[Left] > A[Right])        Swap(&A[Left],&A[Right]) ;    if(A[Center] > A[Right])        Swap(&A[Center],&A[Right]) ;            Swap(&A[Center],&A[Right - 1]) ;    return A[Right - 1] ;    }
Main function
#define Cutoff(3) ;void Qsort(ElementType A[],int Left,int Right){    int i,j ;    ElementType Pivot ;        if(Left + Cutoff <= Right)    {        Pivot = Midian3(A,Left,Right)        i = Left ; j = Right ;        for(;;)        {            While(A[++i] < Privot){}            While(A[--j] > Privot){}            if(i < j)                Swap(&A[i],&A[j]) ;            else                break ;        }        Swap(&A[i],&A[Right-1]) ;                Qsort(A,Left,i-1) ;        Qsort(A,i+1,Right) ;            }    else       InsertionSort(A + Left,Right - Left + 1) ; }

Drive function

void QuickSort(ElementType A[],int N){    Qsort(A,0,N-1) ;}
Summarize
    • For general internal sorting, the selection method is usually insert sort, hill sort and quick sort, according to the input to select
    • Highly optimized fast sequencing can also be as fast as the hill sort for very few inputs

      For fast sorting, it is critical to select pivot elements

    • Heap sort vigil sort to full
    • Insert sort is typically used for small or near-ordered inputs
    • Merge sort is not as good as fast sorting in main memory sort, but the central idea of external sorting when merging

Sort (C language implementation)

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.