| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 9445 人关注过本帖, 14 人收藏
标题:常用的六种排序
取消只看楼主 加入收藏
烟雨晨曦
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:150
专家分:599
注 册:2017-3-5
结帖率:100%
收藏(14)
 问题点数:0 回复次数:0 
常用的六种排序
1.好久没写排序了,基本上用的就是map,vector,今天整理了下排序算法,
下面我基本用的很简洁的代码了,希望能帮到大家。
程序代码:
#include<stdio.h>
#include<malloc.h>
void Swap(int a[],int i,int j)
{
    int temp=a[i];
    a[i]=a[j];
    a[j]=temp;
}

//冒泡排序
void BubbleSorting(int a[],int len)
{
   for(int i=0;i<len;i++)
   {
       for(int j=i+1;j<len;j++)
       {
           if(a[i]>a[j])
           {
              Swap(a,i,j);
           }
       }
   }
}

//选择排序
void SelectSorting(int a[],int len)
{
     for(int i=0;i<len;i++)
     {
         int k=i;
         int temp=a[k];
         for(int j=i+1;j<len;j++)
         {
             if(a[j]<temp)
             {
                temp=a[j];
                k=j;
             }
         }
         Swap(a,i,k);
     }
}

//插入排序
void InsertSorting(int a[],int len)
{
    for(int i=1;i<len;i++)
    {
        int k=i;
        int temp=a[k];
        for(int j=i-1;(j>=0)&&(a[j]>temp);j--)
        {
            a[j+1]=a[j];
            k=j;
        }
        a[k]=temp;
    }
}

//希尔排序
void ShellSorting(int a[],int len)
{
     int gap=len;
     do
     {
        gap/=2;
        for(int i=gap;i<len;i++)
        {
           int k=i;
           int temp=a[k];
           for(int j=i-gap;(j>=0)&&(a[j]>temp);j-=gap)
           {
                a[j+gap]=a[j];
                k=j;
           }
           a[k]=temp;
        }
     }while(gap>0);
}

//快速排序
int Pos(int a[],int low,int high)
{
     int i=low;
     int j=high;
     int k=a[low];
     while(i<j)
     {
          while(a[j]>k) j--;
          Swap(a,i,j);
          while(a[i]<k) i++;
          Swap(a,i,j);
     }
     return i;
}

void QSorting(int a[],int low,int high)
{  
       if(low<high)
       {
         int pos=Pos(a,low,high);
         QSorting(a,low,pos);
         QSorting(a,pos+1,high);
       }
}

void QuickSorting(int a[], int len)
{
    QSorting(a, 0, len-1);
}

//归并排序
void MergerSorting(int a[],int des[],int low,int mid,int high)
{
     int i=low;
     int j=mid+1;
     int k=low;
     while((i<=mid)&&(j<=high))
     {
           if(a[i]<a[j]) des[k++]=a[i++];
           else des[k++]=a[j++];
     }
     while(i<=mid) des[k++]=a[i++];
     while(j<=high) des[k++]=a[j++];
}

void MSorting(int a[],int des[],int low,int high,int max)
{
    if(low==high)
    {
         des[low]=a[low];
    }
    else
    {
      int mid=(low+high)/2;
      int* space=(int*)malloc(sizeof(int)*max);
      if(space!=NULL)
      {
          MSorting(a,space,low,mid,max);
          MSorting(a,space,mid+1,high,max);
          MergerSorting(space,des,low,mid,high);
          free(space);
      }
    }
}

void MerSorting(int a[],int len)
{
    MSorting(a,a,0,len-1,len);
}

void ShowSorting(int a[], int len)
{
    for(int i=0;i<len;i++)
    {
        printf("%d ", a[i]);
    }
    printf("\n");
}

int main()
{
    int a[]={1,5,21,20,31,2,15,100};
    int len=sizeof(a)/sizeof(*a);
    //BubbleSorting(a,len);
    //SelectSorting(a,len);
    //InsertSorting(a,len);
    //ShellSorting(a,len);
    //QuickSorting(a,len);
    MerSorting(a,len);
    ShowSorting(a,len);
    return 0;
}
搜索更多相关主题的帖子: color 
2017-04-22 12:05
快速回复:常用的六种排序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029792 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved