排序大全

1.直接插入排序


void insert_sort(int a[], int n)
{
    int i,j,temp;
    for ( i=1; i<n; i++)
    {
        temp = a[i];
        for (j=i-1; j>=0&&temp<a[j];j--)
        {
            a[j+1] = a[j];
        }
        a[j+1] = temp;
    }
}

2希尔(shell)排序

void shell_sort(int a[],int len)
{

    int h,i,j,temp;

    for (h=len/2; h>0; h=h/2)
    {
        for (i=h; i<len; i++)
        {
            temp = a[i];
            for (j=i-h; (j>=0 && temp<a[j]); j-=h)
            {
                a[j+h] = a[j];
            }
            a[j+h] = temp;
        }
    }
}

3.冒泡排序


void bubble_sort(int a[],int len)
{
    int i = 0;
    int j = 0;
    int temp = 0;
    int exchange = 0;

    for(i=0; i<len-1; i++)
    {
        exchange = 0;
        for(j=len-1; j>=1;j--)
        {
            if(a[j+1] < a[j])
            {
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
                exchange = 1;
            }
        }
        if(exchange !=1)
            return;
    }
}

4.快速排序


int quick_sort_mid(int a[],int low,int high)
{
    int i,j,pivot;
    if(low < high)
    {
        pivot = a[low];
        i = low;
        j = high;
        while(i<j)
        {
            while (i<j && a[j]>=pivot)
                j--;
            if(i<j)
                a[i++] = a[j];

            while (i<j && a[i]<=pivot)
                i++;
            if(i<j)
                a[j--] = a[i];
        }
        a[i] = pivot;
    }
    return i;
}

//递归法
void quick_sort_recur(int a[],int low,int high)
{
    if(low<high)
    {
        int p = quick_sort_mid(a[],low,high);
        quick_sort_recur(a[],low,p-1);
        quick_sort_recur(a[],p+1,high);
    }

}

//非递归法

void quick_sort_unrecur(int a[],int low,int high)
{
    stack <int> s;

    if(low<high)
    {
        int mid = quick_sort_mid(a[],low,high);
        if(low<mid-1)
        {
            s.push(low);
            s.push(mid-1);
        }
        if(high>mid+1)
        {
            s.push(mid+1);
            s.push(high);
        }
        while(!s.empty())
        {
            int q = s.top();
            s.pop();
            p = s.top();
            s.pop();
            mid = quick_sort_mid(a[],p,q);
            if(low<mid-1)
            {
                s.push(low);
                s.push(mid-1);
            }
            if(high>mid+1)
            {
                s.push(mid+1);
                s.push(high);
            }
        }
    }
}

5.选择排序


void select_sort(int a[],int len)
{
    int i,j,mmin,mmin_pos;

    for(i=0; i<len; i++)
    {
        mmin = a[i];
        mmin_pos = i;
        for(j=i; j<len; j++)
        {
            if(a[j] < mmin)
            {
                mmin = a[j];
                mmin_pos = j;
            }
        }
        a[mmin_pos] = a[i];
        a[i] = mmin;
    }
}


6.堆排序


int heapSize = 0;
#define LEFT(index) (((index<<1)+1))
#define RIGHT(index) (((index<<1)+2))
void swap(int* a,int* b){int temp = *a;*a = *b;*b = temp;}

void maxHeapify (int array[],int index)
{
    int largest = 0;
    int left = LEFT(index);
    int right = RIGHT(index);

    if((left <= heapSize) && (array[left] > array[index]))
        largest = left;
    else
        largest = index;

    if((right <= heapSize) && (array[right] > array[largest]))
        largest = right;

    if(largest != index)
    {
        swap(&array[index],&array[largest]);
        maxHeapify(array,largest);
    }
}

void buildMaxHeap(int array[],int length)
{
    int i;
    heapSize = length;
    for (i= length/2; i >= 0; i--)
        maxHeapify(array, i);
}

void heap_sort (int array[], int length)
{
    int i;

    buildMaxHeap(array,(length - 1));

    for(i = length-1;i >= 1; i--)
    {
        swap(&array[0],&array[i]);
        heapSize--;
        maxHeapify(array,0);
    }
}

7.归并排序


void Merge(int a[],int tmp[],int lPos,int rPos,int rEnd)
{
    int i,lEnd,len,tmpPos;
    lEnd = rPos - 1;
    tmpPos = lPos;
    len = rEnd - lPos + 1;

    while( lPos <= lEnd && rPos <= rEnd)
    {
        if(a[lPos] <= a[rPos])
            tmp[tmpPos++] = a[lPos++];
        else
            tmp[tmpPos++] = a[rPos++];
    }
    while( lPos <= lEnd)
        tmp[tmpPos++] = a[lPos++];
    while( rPos <= rEnd)
        tmp[tmpPos++] = a[rPos++];

    for( i = 0; i<len; i++,rEnd--)
        a[rEnd] = tmp[rEnd];
}

void msort(int a[],int tmp[],int low,int high)
{
    if(low>=high)
        return;
    int middle = (low + high)/2;
    msort(a,tmp,low,middle);
    msort(a,tmp,middle+1,high);
    Merge(a,tmp,low,middle+1,high);
}

void merge_sort(int a[],int len)
{
    int* tmp = NULL;
    tmp = new int[len];
    if(tmp != NULL)
    {
        msort(a,tmp,0,len-1);
        delete []tmp;
    }
}

8.基数排序(桶排序)


int find_max(int a[],int len)
{
    int max = a[0];
    for(int i=1; i<len; i++)
    {
        if( max < a[i])
            max = a[i];
    }
    return max;
}

int digit_number(int number)
{
    int digit = 0;
    do
    {
        number = number/10;
        digit++;
    }while(number != 0);
    return digit;
}

int kth_digit(int number,int kth)
{
    number = number/pow(10,kth-1);
    return number%10;
}

void radix_sort(int a[],int len)
{
    int* temp[10];
    int cont[10] = {0};
    int max = find_max(a,len);
    int maxDigit = digit_number(max);
    int i,j,k;
    for(i=0;i<10;i++)
    {
        temp[i] = new int[len];
        memset(temp[i],0,sizeof(int)*len);
    }
    for(i=1; i<=maxDigit; i++)
    {
        memset(cont,0 , sizeof(int)*10);
        for(j=0; j<len; j++)
        {
            int xx = kth_digit(a[j],i);
            temp[xx][cont[xx]] = a[j];
            cont[xx]++;
        }
        int index = 0;
        for(j=0; j<10; j++)
        {
            for(k=0; k<cont[j]; k++)
            {
                a[index++] = temp[j][k];
            }
        }
    }
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值