排序

冒泡排序

void Swap(int*a, int* b)
{
    int tmp = *a;
    *a = *b;
    *b = tmp;
}

void BubbleSort(int array[], int size)
{
    if(size <= 1)
    {
        return;
    }
    int bound = 0;
    for(; bound < size; bound++)
    {
        int cur = size - 1;
        for(; cur >= 0; cur--)
        {
            if(array[cur] < array[cur - 1])
            {
                Swap(&array[cur], &array[cur - 1]);
            }
        }
    }//end for(; bound < size; bound++)
}

選擇排序

void SelectSort(int array[], int size)
{
    if(size <= 1)
    {
        return;
    }
    int bound = 0;
    for(; bound < size; bound++)
    {
        int cur = bound + 1;
        for(; cur < size; cur++)
        {
            if(array[bound] > array[cur])
            {
                Swap(&array[bound] , &array[cur]);
            }
        }
    }//end for(; bound < size; bound++)
}

插入排序

void InsertSort(int array[], int size)
{
    if(size <= 1)
    {
        return;//zhi you yi ge yuan su huo zhe fei fa shu ru
    }
    //ding yi yi ge bian jie zhi bound
    //qi zhong [0, bound)biao shi you xu qu jian
    //[bound, cur)dai biao wu xu qu jian
    int bound = 1;
    for(; bound < size; bound++)
    {
        //ding yi yi ge bound_value bao cun array[bound] de zhi, bian yu ban yun
        int bound_value = array[bound];
        //ding yi yi ge cur bang zhu wo men ban yun
        int cur = bound;
        //cur xiang qian sao miao
        for(; cur > 0; cur--)
        {
            //jiang shu zhu cur-1 dui ying de yuan su he cur bound_value bi jiao, ru guo da yu, 
            //jiu jin xin ban yun
            if(array[cur - 1] > bound_value)
            {
                array[cur] = array[cur - 1];
            }
            //shuo ming yi jing shi he shi wei zhi, jiu bu xu yao ban yun
            else
            {
                break;
            }
        }
        array[cur] = bound_value;
    }//end for(; bound < size; bound++)
}

堆排序

void AdjustDown(int array[], int size, int index)
{
    int parent = index;
    int child = 2 * parent + 1;
    while(child < size)
    {
        if(child + 1 < size && array[child + 1] > array[child])
        {
            child = child + 1;
        }
        if(array[child] > array[parent])
        {
            Swap(&array[child], &array[parent]);
        }
        else if(array[child] < array[parent])
        {
            break;
        }
        parent = child;
        child = 2 * parent + 1;
    }
}

void HeapCreat(int array[], int size)
{
    int i = (size - 1 - 1) / 2;
    for(; i >= 0; i--)
    {
        AdjustDown(array, size, i);
    }
}

void HeapPop(int array[], int index)
{
   Swap(&array[0], &array[index]) ;
   AdjustDown(array, index, 0);
}

void HeapSort(int array[], int size)
{
    if(size <= 1)
    {
        return;
    }
    HeapCreat(array, size);
//    int end = size - 1;
//    for(; end >= 0; --end)
//    {
//        Swap(&array[0], &array[end]);
//        AdjustDown(array, end, 0);
//    }
    int i = 1;
    for (; i < size; i++)
    {
        HeapPop(array, size - i);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章