堆排序(非遞迴)

來源:互聯網
上載者:User

以下代碼經測試,排序5000000(五千萬)int型資料沒有問題!

第一個參數是數組首地址
第二個參數是數組元素個數

void HeapSort(int * const arr, const DWORD number)//堆排序{    int tmp;    DWORD tmpIndex;    DWORD i=1;    DWORD num;    DWORD indexUp;    DWORD indexDown;    if (number < 2)    {        return;    }    indexUp = number-1;    if (0 != (indexUp%2))    {        tmpIndex = (indexUp - 1) / 2;        if (arr[indexUp] > arr[tmpIndex])        {            tmp = arr[indexUp];            arr[indexUp] = arr[tmpIndex];            arr[tmpIndex] = tmp;        }        indexUp--;    }    for (; indexUp>0; indexUp-=2)    {        tmpIndex = (indexUp / 2) - 1;        if (arr[indexUp-1] >= arr[indexUp])        {            if (arr[indexUp-1] > arr[tmpIndex])            {                tmp = arr[indexUp-1];                arr[indexUp-1] = arr[tmpIndex];                arr[tmpIndex] = tmp;                indexDown = indexUp-1;            }            else            {                continue;            }        }        else        {            if (arr[indexUp] > arr[tmpIndex])            {                tmp = arr[indexUp];                arr[indexUp] = arr[tmpIndex];                arr[tmpIndex] = tmp;                indexDown = indexUp;            }            else            {                continue;            }        }        while ((2*indexDown) < number)        {            tmpIndex = 2*indexDown +1;            if (arr[tmpIndex] >= arr[tmpIndex+1] || (tmpIndex+1 == number))            {                if (arr[tmpIndex] > arr[indexDown])                {                    tmp = arr[indexDown];                    arr[indexDown] = arr[tmpIndex];                    arr[tmpIndex] = tmp;                    indexDown = tmpIndex;                }                else                {                    break;                }            }            else            {                if ((arr[tmpIndex+1] > arr[indexDown]) && (tmpIndex < number))                {                    tmp = arr[indexDown];                    arr[indexDown] = arr[tmpIndex+1];                    arr[tmpIndex+1] = tmp;                    indexDown = tmpIndex+1;                }                else                {                    break;                }            }        }    }    tmp = arr[0];    arr[0] = arr[number-1];    arr[number-1] = tmp;    for (num=number-1; num>1; num--)    {        for (indexDown=0; (2*indexDown +1) < num; )        {            tmpIndex = 2*indexDown +1;            if (arr[tmpIndex] >= arr[tmpIndex+1])            {                if (arr[tmpIndex] > arr[indexDown])                {                    tmp = arr[indexDown];                    arr[indexDown] = arr[tmpIndex];                    arr[tmpIndex] = tmp;                    indexDown = tmpIndex;                }                else                {                    break;                }            }            else            {                if ((arr[tmpIndex+1] > arr[indexDown]) && (tmpIndex+1 < num))                {                    tmp = arr[indexDown];                    arr[indexDown] = arr[tmpIndex+1];                    arr[tmpIndex+1] = tmp;                    indexDown = tmpIndex+1;                }                else                {                    break;                }            }        }        tmp = arr[0];        arr[0] = arr[num-1];        arr[num-1] = tmp;    }}

測試代碼:

#include <stdio.h>#include <conio.h>#include <windows.h>void HeapSort(int * const arr, const DWORD number);//堆排序void ExamineArr(const int * const arr, DWORD number);//檢查排序結果int main(int argc, char *argv[]){    DWORD StartTime, EndTime;    DWORD i;    DWORD num = 50000000;    int *arr = NULL;    arr = (int *)malloc(num * sizeof (int));    if (NULL == arr)    {        free(arr);        printf("記憶體配置失敗,程式退出!\n");        getch();        return -1;    }    StartTime = GetTickCount();    for (i=0; i<num; i++)    {        *(arr+i) = rand();    }    EndTime = GetTickCount();    printf("產生%u個隨機數耗時:%ums\n", num, EndTime - StartTime);        StartTime = GetTickCount();    HeapSort(arr, num);    EndTime = GetTickCount();    printf("堆排序耗時:%ums\n", EndTime - StartTime);    ExamineArr(arr, num);//檢查排序結果    free(arr);    getch();    return 0;}void HeapSort(int * const arr, const DWORD number)//堆排序{    int tmp;    DWORD tmpIndex;    DWORD i=1;    DWORD num;    DWORD indexUp;    DWORD indexDown;    if (number < 2)    {        return;    }    indexUp = number-1;    if (0 != (indexUp%2))    {        tmpIndex = (indexUp - 1) / 2;        if (arr[indexUp] > arr[tmpIndex])        {            tmp = arr[indexUp];            arr[indexUp] = arr[tmpIndex];            arr[tmpIndex] = tmp;        }        indexUp--;    }    for (; indexUp>0; indexUp-=2)    {        tmpIndex = (indexUp / 2) - 1;        if (arr[indexUp-1] >= arr[indexUp])        {            if (arr[indexUp-1] > arr[tmpIndex])            {                tmp = arr[indexUp-1];                arr[indexUp-1] = arr[tmpIndex];                arr[tmpIndex] = tmp;                indexDown = indexUp-1;            }            else            {                continue;            }        }        else        {            if (arr[indexUp] > arr[tmpIndex])            {                tmp = arr[indexUp];                arr[indexUp] = arr[tmpIndex];                arr[tmpIndex] = tmp;                indexDown = indexUp;            }            else            {                continue;            }        }        while ((2*indexDown) < number)        {            tmpIndex = 2*indexDown +1;            if (arr[tmpIndex] >= arr[tmpIndex+1] || (tmpIndex+1 == number))            {                if (arr[tmpIndex] > arr[indexDown])                {                    tmp = arr[indexDown];                    arr[indexDown] = arr[tmpIndex];                    arr[tmpIndex] = tmp;                    indexDown = tmpIndex;                }                else                {                    break;                }            }            else            {                if ((arr[tmpIndex+1] > arr[indexDown]) && (tmpIndex < number))                {                    tmp = arr[indexDown];                    arr[indexDown] = arr[tmpIndex+1];                    arr[tmpIndex+1] = tmp;                    indexDown = tmpIndex+1;                }                else                {                    break;                }            }        }    }    tmp = arr[0];    arr[0] = arr[number-1];    arr[number-1] = tmp;    for (num=number-1; num>1; num--)    {        for (indexDown=0; (2*indexDown +1) < num; )        {            tmpIndex = 2*indexDown +1;            if (arr[tmpIndex] >= arr[tmpIndex+1])            {                if (arr[tmpIndex] > arr[indexDown])                {                    tmp = arr[indexDown];                    arr[indexDown] = arr[tmpIndex];                    arr[tmpIndex] = tmp;                    indexDown = tmpIndex;                }                else                {                    break;                }            }            else            {                if ((arr[tmpIndex+1] > arr[indexDown]) && (tmpIndex+1 < num))                {                    tmp = arr[indexDown];                    arr[indexDown] = arr[tmpIndex+1];                    arr[tmpIndex+1] = tmp;                    indexDown = tmpIndex+1;                }                else                {                    break;                }            }        }        tmp = arr[0];        arr[0] = arr[num-1];        arr[num-1] = tmp;    }}void ExamineArr(const int * const arr, DWORD number){    DWORD i=0, j=1;    if (number <2)    {        return;    }    for (; j<number; i++,j++)    {        if (arr[i] > arr[j])        {            printf("第%u個數大於第%u個數!\n", i, j);            return;        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.