Chinaunix首页 | 论坛 | 博客
  • 博客访问: 230448
  • 博文数量: 55
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 530
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-22 17:59
文章分类

全部博文(55)

文章存档

2015年(2)

2011年(1)

2010年(1)

2009年(18)

2008年(16)

2007年(17)

我的朋友

分类: C/C++

2007-11-10 21:02:40

1、冒泡排序

void BubbleSort(int r[],int n) 
{//冒泡排序(有小到大) 
 int i,j,k; 
 int exchange; 
 for(i=0;i<=n;i++) 
 { 
  exchange=0; 
  for(j=n-1;j>=i;j--) 
  if(r[j+1]  { 
   k=r[j+1]; 
   r[j+1]=r[j]; 
   r[j]=k; 
   exchange=1; 
  } 
  if(!exchange) 
  break; 
 } 
}

2、快速排序

void swap(int &a,int &b) //交换两个数的大小a,b
{
 int temp;
 temp = a;
 a = b;
 b = temp;

}
int Partition(int a[],int low,int high)//返回枢纽记录应该放的位置
{
 int temp;
 int pivotkey = a[low];
 while(low < high)
 {
  while((low < high) && (a[high] >= pivotkey))
   --high;
  swap(a[low],a[high]);
  while((low < high) &&(a[low] <= pivotkey))
   ++low;
  swap(a[low],a[high]);
 }
 return low;
 }
void QSort(int a[],int low,int high)
{
 int pivotloc;
 if(low < high)
 {
  pivotloc = Partition(a,low,high);
  QSort(a,low,pivotloc-1);
  QSort(a,pivotloc+1,high);
 }
}

3、选择排序

void SelectSort(int arr[],int len)

{

    int i,j;

    int minPos,temp;

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

   {

       minPos = i;

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

        {

            if(arr[j] < arr[i])

                 minPos = j;

        }

       if(minPos != i)

       {

           temp =arr[minPos];

           arr[minPos] = arr[i];

           arr[i] = temp;

       }   

   }

}

4、插入排序
void InsertSort(int arr[],int len)
{
    int i,j;
    int temp;
    for(i = 1; i < len; i++)
   {
       temp = a[i];
       j = i-1;
       while(arr[j] > temp)
       {
           arr[j+1] = arr[j];
           j--;
       } 
       arr[j+1] = temp; 
   }
}
5、希尔排序
void ShellSort(int arr[],int len)
{
    int d ;
    d = len;
    while(d >1)
    {
       d = (d + 1)/2;
       for(int i = 0; i < len - d; i++)
       {
            if(arr[i+d] > a[i])
            {
                temp = a[i+d];
                a[i+d] = a[i];
                a[i] = temp;
            }
       }
    }
}
阅读(903) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~