编辑代码

#include <stdio.h>
/**
*简单数据交换函数
*/
void swapInt(int indexOne,int indexTwo,int* ary)
{
    int tempVal = ary[indexOne];
    ary[indexOne] = ary[indexTwo];
    ary[indexTwo] = tempVal;
}

/**
*插入排序
*/
void insterSort(int length,int* ary)
{
    for (int i = 1; i < length; i++)
    {
         for (int j = i; j > 0 && ary[j] < ary[j-1]; j--)
        {
            swapInt(j,j-1,ary);
        }
    }
}

/*
*希尔排序
*/
void shellSort(int length,int* ary)
{
    for (int step = length / 2; step > 0; step /=2)
    {
        for (int i = 0;i < step - 1;i++)
        {
            for (j = i; j >= 0 && ary[j] > ary[j + step]; j -= step)
            {
                swapInt(j,j + step,ary);
            }
        }
    }
}

/**
*冒泡排序
*/
void bublleSort(int length,int* ary)
{
    for (int i = 0;i<length-1;i++)
    {
        for (int j = 0;j<length - i -1;j++)
        {
            if (ary[j] > ary[j+1])
            {
                swapInt(j,j+1,ary);
            }
        }
    }
}

/**
*快速排序
*/
void quickSort(int beginIndex,int endIndex,int* ary)
{
    if (beginIndex > endIndex)
        return;
    int frist = beginIndex;
    int last = endIndex;
    int markVal = ary[beginIndex];
    while (beginIndex < endIndex)
    {
        while(beginIndex < endIndex && ary[endIndex] > markInt)
        {
            --endIndex;
        }
        ary[beginIndex] = ary[endIndex];
               
        while(beginIndex < endIndex && markVal <= ary[beginIndex])
        {
            ++beginIndex;
        }
        ary[endIndex] = ary[beginIndex];
    }
    ary[beginIndex] = markVal;
    quickSort(frist,beginIndex-1,ary);
    quickSort(beginIndex + 1,last,ary);
}

/**
*选择排序
*/
void selectSort(int length,int* ary)
{
    for (int i = 0;i<length-1;i++)
    {
        int maxIndex = 0;
        for (int j = 1;j<length - i -1;j++)
        {
            if (ary[j] > ary[maxIndex])
            {
                maxIndex = j;
            }
        }
                swapInt(maxIndex,length - i -1,ary);
    }
}

/**
*堆排序
*/
void heapAdjust(int rootIndex ,int length,int* ary)
{
    int tempRootIndex = rootIndex;
    int lChildIndex = tempRootIndex * 2 + 1;
    int rChildIndex = lChildIndex + 1;
    int largestIndex = 0;
    while(lChildIndex < length)
    {
        rChildIndex = lChildIndex + 1;
        if (rChildIndex < length && ary[rChildIndex] > ary[lChildIndex])
            largestIndex = rChildIndex;
        else
            largestIndex = lChildIndex;    
        if (ary[tempRootIndex] > ary[largestIndex])
            break;
        swapInt(largestIndex,tempRootIndex,ary)     
        tempRootIndex = largestIndex;
        lChildIndex = tempRootIndex * 2 + 1;
    }
}

void heapSort(int length,int* ary)
{
    //(i-1)/2求i的根
    for (int i = (length-2) / 2;i>=0;i--)
        heapAdjust(i,length,ary);
       
    for (int j = 0;j<length;j++)
    {
        swapInt(0,length - j - 1,ary);
        heapAdjust(0,length - j - 1,ary);
    }
}

/**
*计数排序
*/
void countSort(int length,int* ary,int maxNum)
{
    int* sortAry = new int[length];
    int* countAry = new int[maxNum];
    for (int i = 0;i<length;i++)
    {
        ++countAry[ary];
    }
    for (int i= 1;i<maxNum;i++)
    {
        countAry += countAry[i-1];
    }
    for(int i=length-1;i>=0;--i)
    {
        sortAry[--countAry[ary]] = ary;
    }
    for (int i = 0;i<length;i++)
    {
        ary = sortAry;
    }
    delete countAry;
    delete sortAry;
}

void maxBitFunc(int length,int* ary)
{
    int maxBit = 1;
    for (int i = 0;i <length;i++)
    {
        int tempBit = ary/10 + 1;
        if(maxBit < tempBit)
        {
            maxBit = tempBit;
        }
    }
    return maxBit;
}

/**
*基数排序
*/
void radixSort(int length,int* ary)
{
    int maxBit = maxBitFunc(length,ary);
    int* sortAry = new int[length];
    int* countAry = new int[10];
    int radix = 1
    for (int i = 0;i<maxBit;i++)
    {
        for(int j = 0;j<10;j++)
        {
            countAry[j] = 0;
        }       
        for (int j = 0;j < length;j++)
        {
            int k = (ary[j] / radix) % 10;
            countAry[k]++;
        }       
        for (int j = 1;j<10;j++)
        {
            countAry[j]+= countAry[j-1];
        }      
        for (int j = length - 1;j>=0;j++)
        {
            int k = (ary[j] / radix) % 10;
            sortAry[countAry[k]--] = ary[j];
        }
        for (int j = 0;j < length;j++)
        {
            ary[j] = sortAry[j];
        }
    }
    delete countAry;
    delete sortAry;
}

/**
*归并排序
*/
void merge(int* ary,int* tempAry,int beginIndex,int midIndex,int endIndex)
{
        int frist1Index = beginIndex, frist2Index = midIndex + 1,tempIndex;
        while(frist1Index != midIndex && frist2Index != endIndex)
        {
                if (ary[frist1Index] > ary[frist2Index])
                {
                        tempAry[tempIndex++] = ary[frist2Index++];
                }
                else
                {
                        tempAry[tempIndex++] = ary[frist1Index++];
                }
        }
       
        while(frist1Index != midIndex)
        {
                tempAry[tempIndex++] = ary[frist1Index++];
        }
       
        while(frist2Index != endIndex)
        {
                tempAry[tempIndex++] = ary[frist2Index++];
        }
       
        for (int i = beginIndex;i<= endIndex;i++)
        {
                ary = tempAry;
        }
}

void mergeSort(int beginIndex,int endIndex,int* ary,int* tempAry)
{
        if (beginIndex < endIndex)
        {
                int midIndex = (beginIndex + endIndex) / 2;
                mergeSort(beginIndex,midIndex,ary,tempAry);
                mergeSort(midIndex+1,endIndex,ary,tempAry);
                merge(ary,tempAry,beginIndex,midIndex,endIndex);
        }
}
int main () {
    //JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 
    printf("Hello world!     - c.jsrun.net.");
    return 0;
}