排序C语言代码

// 归并排序
//
void MergeSort(int sourceArr[],int tempArr[],int startIndex,int endIndex)
{
        void Merge(int sourceArr[],int tempArr[],int startIndex,int midIndex, int endIndex);
        int midIndex;
        if(startIndex < endIndex)
        {
                midIndex = (startIndex + endIndex)/2;
                MergeSort(sourceArr,tempArr,startIndex,midIndex);
                MergeSort(sourceArr,tempArr,midIndex+1,endIndex);
                Merge(sourceArr,tempArr,startIndex,midIndex,endIndex);
        }
}


void Merge(int sourceArr[],int tempArr[],int startIndex,int midIndex, int endIndex)
{
        int i = startIndex,j=midIndex+1,k = startIndex;
        while(i != midIndex+1 && j != endIndex+1 )
        {
                if(sourceArr[i] > sourceArr[j])
                        tempArr[k++] = sourceArr[j++];
                else
                        tempArr[k++] = sourceArr[i++];
        }
        while(i != midIndex+1)
                tempArr[k++] = sourceArr[i++];
        while(j != endIndex+1)
                tempArr[k++] = sourceArr[j++];
        for(i=startIndex;i<endIndex;i++)
                sourceArr[i] = tempArr[i];

}


//快速排序
void quickSort(int a[],int left,int right)
{
        if(left >= right)
        {
                return;
        }
        int i = left;
        int j = right;
        int key = a[left];
        while(i<j)
        {
                while(i<j && key<=a[j])
                {
                        j--;
                }
                a[i] = a[j];
                while(i<j && key>=a[i])
                {
                        i++;
                }
                a[j] = a[i];
        }
        a[i] = key;
        quickSort(a,left,i-1);
        quickSort(a,i+1,right);
}


//直接排序
void inserSort(int p[],int n)
{
        int temp=0,j,i;
        for(i=1;i<n;i++)
        {
                j = i -1;
                temp=p[i];
                for(;j>=0 && temp<p[j];j--)
                {
                        p[j+1] = p[j];
                }
                p[j+1] = temp;
        }
}


//希尔排序(最小增量)
void shellSort(int a[],int n)
{
        int temp=0;
        double d1 = n;
        while(1)
        {
                d1 = ceil(d1/2);
                int d = (int) d1;
                int x = 0;
                for(;x<d;x++)
                {
                        int i = x + d;
                        for(;i<n;i+=d)
                        {
                                int j =i - d;
                                temp = a[i];
                                for(;j>=0 && temp < a[j];j-=d)
                                {
                                        a[j+d] = a[j];
                                }
                                a[j+d] = temp;
                        }
                }
                if(d==1)
                {
                break;
                }
        }
}


//简单选择排序
void selectSort(int a[],int n)
{
        int position=0,i,temp;
        for(i = 0;i<n;i++)
        {
                int j=i+1;
                position = i;
                temp = a[i];
                for(;j < n;j++)
                {
                        if(a[j]<temp)
                        {
                                temp = a[j];
                                position = j;
                        }
                }
                a[position] = a[i];
                a[i] = temp;


        }
}


//冒泡排序
void bubbleSort(int a[],int n)
{
        int temp = 0;
        int i;
        for(i=0;i<n;i++)
        {
                int j;
                for(j=0;j<n-1-j;j++)
                {
                        if(a[j]>a[j+1])
                        {
                                temp = a[j];
                                a[j] = a[j+1];
                                a[j+1] = temp;
                        }
                }
        }


}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值