您的位置:首页 > 编程语言 > C语言/C++

所有的排序c语言实现

2013-12-08 15:06 267 查看
排序主要有两种,一种是n平方复杂度的,一种是nlogn或者n根号n的。

n平方复杂度的主要是一个一个将有序区间缩小,而nlogn或者根号n则是分区间的增大有序序列。

#include <stdio.h>

#include <stdlib.h>

#include <inttypes.h>

#include <math.h>

//在linux中,rand函数的最大值是32位字节,windows是16位字节

void swap(int32_t *a,int32_t *b)

{

    int32_t temp = *a;

    *a = *b;

    *b = temp;

}

void print(int32_t a[],int32_t length)

{

    int32_t i;

    for(i=0;i<length;i++)

    {

        printf("%d\n",a[i]);

    }

}

//重新给数组赋予随机值

void assign(int a[],int length)

{

    int32_t i;

    for(i=0;i<length;i++)

        a[i] = rand();

}

void dubble(int32_t a[],int32_t length)

{

    _Bool flags = 0;

    int32_t i,j;

    for(i=0;i<length-1&&!flags;i++)

    {

        flags = 1;

        for(j=length-1;j>0;j--)

            if(a[j]<a[j-1])

            {

                swap(&a[j],&a[j-1]);

                flags = 0;

            }

    }

}

void select(int32_t a[],int32_t length)

{

    int32_t i,j;

    int32_t min;

    for(i=0;i<length-1;i++)

    {

        min = i;

        for(j=i+1;j<length;j++)

        {

            if(a[j]<a[min])

                min = j;

        }

        if(min!=i)

            swap(&a[i],&a[min]);

    }

}

void insert(int32_t a[],int32_t length)

{

    int32_t i,j,temp;

    for(i=1;i<length;i++)

    {

        if(a[i]<a[i-1])

        {

            temp = a[i];

            for(j=i-1;j>=0&&a[j]>temp;j--)

                a[j+1] = a[j];

            a[j+1] = temp;

        }

    }

}

void shell(int32_t a[],int32_t length)

{

    int32_t increment=length/2;

    int32_t i,j,temp;

    while(increment>=1)

    {

        for(i=increment;i<length;i++)

        {

            if(a[i]<a[i-increment])

            {

                temp = a[i];

                for(j=i-increment;j>=0&&a[j]>temp;j-=increment)

                    a[j+increment] = a[j];

                a[j+increment] = temp;

            }

        }

        increment/=2;

    }

}

//由于堆排序涉及到*2,所以应当用1

void heapadjust(int32_t a[],int32_t low,int32_t high)

{

    int32_t i,j,temp;

    temp = a[low-1];

    for(j=2*low;j<=high;j*=2)

    {

        if(j<high&&a[j-1]<a[j])

            j++;

        if(temp>a[j-1])

            break;

        a[low-1] = a[j-1];

        low = j;

    }

    a[low-1] = temp;

}

void heap(int32_t a[],int32_t length)

{

    int32_t i,j;

    for(i=length/2;i>0;i--)

        heapadjust(a,i,length);

    for(i=length;i>1;i--)

    {

        swap(&a[0],&a[length-1]);

        heapadjust(a,1,i-1);

    }

}

void Merge(int32_t a[],int32_t low,int32_t mid,int32_t high)

{

    int32_t i,j,k;

    int32_t *temp;

    temp = (int32_t *)malloc((high-low+1)*sizeof(int32_t));

    i=low;

    j=mid+1;

    k=0;

    while(i<=mid&&j<=high)

    {

        if(a[i]>a[j])

            temp[k++] = a[j++];

        else

            temp[k++] = a[i++];

    }

    while(i<=mid)

        temp[k++] = a[i++];

    while(j<=high)

        temp[k++] = a[j++];

    for(i=low,j=0;j<k;j++,i++)

        a[i++] = temp[j++];

    free(temp);

}

void Msort(int32_t a[],int32_t low,int32_t high)

{

    int32_t i,j,mid;

    if(high>low)

    {

        mid = (high+low)/2;

        Msort(a,low,mid);

        Msort(a,mid+1,high);

        Merge(a,low,mid,high);

    }

}

void mergesort(int32_t a[],int32_t length)

{

    Msort(a,0,length-1);

}

int32_t Partition(int32_t a[],int32_t low,int32_t high)

{

    int32_t piv = a[low];

    while(low<high)

    {

        while(high>low&&a[high]>piv)

            high--;

        a[low] = a[high];

        while(low<high&&a[low]<piv)

            low++;

        a[high] = a[low];

    }

    a[low] = piv;

    return low;

}

void Qsort(int32_t a[],int32_t low,int32_t high)

{

    int32_t piv;

    if(low<high)

    {

        piv = Partition(a,low,high);

        Qsort(a,low,piv-1);

        Qsort(a,piv+1,high);

    }

}

void quicksort(int32_t a[],int length)

{

    Qsort(a,0,length-1);

}

int main()

{

    //init array

    int32_t a[10]={0,4,6,8,2,3,5,9,7,1};

    assign(a,10);

    //print

    printf("start:\n");

    print(a,10);

    //sort

    dubble(a,10);

    //print

    printf("after sort:\n");

    print(a,10);

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息