鎺掑簭绠楁硶

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鎺掑簭绠楁硶相关的知识,希望对你有一定的参考价值。

鏍囩锛?/p>

//BubbleSort
void BubbleSort(int* pData, int Count){
    int iTemp;
    for(int i = 0; i < Count; i++){
        for(int j = Count-1; j >= i; j--){
            if(pData[j] < pData[j-1]){
                iTemp = pData[j];
                pData[j] = pData[j-1];
                pData[j-1] = iTemp;
            }
        }
    }
}

//SelectSort
void SelectSort(int* pData, int Count){
    int iTemp;
    int iPos;
    for(int i = 0; i < Count-1; i++){
        iTemp = pData[i];
        iPos = i;
        for(int j = i+1; j < Count; j++){
            if(pData[j] < iTemp){
                iTemp = pData[j];
                iPos = j;
            }
        }
        pData[iPos] = pData[i];
        pData[i] = iTemp;
    }
}

//InsertSort
void InsertSort(int* pData, int Count){
    int iTemp;
    int iPos;
    for(int i = 1; i < Count; i++){
        iTemp = pData[i];
        iPos = i-1;
        while(iPos >= 0 && iTemp < pData[iPos]){
            pData[iPos+1] = pData[iPos];
            iPos--;
        }
        pData[iPos+1] = iTemp;
    }
}

//MergeSort
void merge(int array[], int p, int q, int r){
    int i, k;
    int begin1, end1, begin2, end2;
    int *temp = (int*)malloc((r-p+1)*sizeof(int));
    begin1 = p; end1 = q;
    begin2 = q+1; end2 = r;
    k = 0;
    while(begin1 <= end1 && begin2 <= end2){
        if(array[begin1] < array[begin2]){
            temp[k] = array[begin1]; begin1++;
        }
        else{
            temp[k] = array[begin2]; begin2++;
        }
        k++;
    }
    while(begin1 <= end1)
        temp[k++] = array[begin1++];
    while(begin2 <= end2)
        temp[k++] = array[begin2++];
    for(i = 0; i < r-p+1; i++)
        array[p+i] = temp[i];
    free(temp);
}

void merge_sort(int array[], unsigned int first, unsigned int last){
    int mid = 0;
    if(first < last){
        mid = (first + last)/2;
        merge_sort(array, first, mid);
        merge_sort(array, mid+1, last);
        merge(array, first, mid, last);
    }
}

//qsort
inline void swap(int v[], int k, int j){
    int temp;
    temp = v[k];
    v[k] = v[j];
    v[j] = temp;
}

void qsort(int v[], int left, int right){
    int j, last;
    if(left >= right) return;
    swap(v, left, (left+right)/2);
    last = left;
    for(j = left+1; j <= right; j++){
        if(v[j] < v[left]) swap(v, ++last, j);
    }
    swap(v, left, last);
    qsort(v, left, last-1);
    qsort(v, last+1, right);
}

//ShellSort
int ShellPass(int *array, int d){
    int temp;
    int k=0;
    for(int i = d+1; i < 13; i++){
        if(array[i] < array[i-d]){
            temp = array[i];
            int j = i-d;
            do{
                array[j+d] = array[j];
                j = j-d;
                k++;
            }
            while(j > 0 && temp < array[j]);
            array[j+d] = temp;
        }
        k++;
    }
    return k;
}

void ShellSort(int *array){
    int count = 0;
    int ShellCount = 0;
    int d = 12;//涓€鑸閲忚缃负鏁扮粍鍏冪礌涓暟锛屼笉鏂櫎浠?浠ュ彇灏?/span>
    do{
        d = d/2;
        ShellCount = ShellPass(array, d);
        count += ShellCount;
    }
    while(d>1);
    cout << "甯屽皵鎺掑簭鍏抽敭瀛楃Щ鍔ㄦ鏁帮細" << count << endl;
}

//HeapSort
void HeapAdjust(int a[], int i, int n){
    if(n == 1 || i > (n-2)/2) return;
    int lc = 2*i+1, rc = 2*i+2;
    if(rc <= n-1){
        int pos;
        if(a[lc] >= a[rc])
            pos = lc;
        else
            pos = rc;
        if(a[i] < a[pos]){
            int temp = a[i];
            a[i] = a[pos];
            a[pos] = temp;
            HeapAdjust(a, pos, n);
        }
    }
    else{
        if(a[i] < a[lc]){
            int temp = a[lc];
            a[lc] = a[i];
            a[i] = temp;
            HeapAdjust(a, lc, n);
        }
    }
}

void HeapCreate(int a[], int n){
    int first = (n-1)/2;
    for(; first>=0; first--)
        HeapAdjust(a, first, n);
}

void HeapSort(int a[], int n){
    HeapCreate(a, n);
    int temp;
    for(int i = 0; i < n; i++){
        temp = a[n-1-i];
        a[n-1-i] = a[0];
        a[0] = temp;
        HeapAdjust(a, 0, n-1-i);
    }
}

鏃堕棿澶嶆潅搴︼細

鍐掓场銆侀€夋嫨銆佹彃鍏ユ帓搴忥細O(n2)

蹇€熴€佸綊骞躲€佸爢鎺掑簭:O(nlog2n)

甯屽皵鎺掑簭锛歄(n1+ξ) 0<ξ<1

以上是关于鎺掑簭绠楁硶的主要内容,如果未能解决你的问题,请参考以下文章

缁忓吀鎺掑簭绠楁硶鈥斺€旈€夋嫨鎺掑簭

鎺掑簭绠楁硶绯诲垪1-鍐掓场鎺掑簭

鎺掑簭绠楁硶绯诲垪3-鎻掑叆鎺掑簭

缁忓吀鎺掑簭绠楁硶鈥斺€斿爢鎺掑簭

娴呮瀽涓冪缁忓吀鎺掑簭绠楁硶

鎻掑叆鎺掑簭浠g爜鍒嗘瀽