常用的选择排序.Shell排序.快速排序.冒泡排序.插入排序的算法

 下面显示的是常用排序算法中的冒泡排序,选择排序,shell排序和插入排序以及快速排序这5种常见的排序算法的程序代码:
下面的是string类的复制控制函数和各类重载函数的写法:(以后会陆续添加相关的函数重载)
#include <iostream>
using namespace std;
void bubble_sort(int a[],int num)
{
   int i,j;
   for(i=0;i<num-1;i++)       //进行num-1次循环
   {
     for(j=0;j<num-i-1;j++)
     {
      if(a[j]>a[j+1])
      {
   int temp;
     temp = a[j];
      a[j] = a[j+1];
       a[j+1] = temp;
      }
     }
   }
}
void select_sort(int a[],int num)
{
   int i,j;
   for(i=0;i<num;i++)
   {
      for(j=i+1;j<num;j++)
      {
         if(a[i]>a[j])
         {
             int temp;
             temp = a[i];
             a[i] = a[j];
          a[j] = temp;
          }
       }
   }
} 
void quick_sort(int a[],int s,int e)
{
     int i,j;
     int x=a[s];
     i=s;
     j=e;
     if(s<e)
     {
      while(i<j)
   {
        while(i<j && a[j]>= x)
       {
           j--;
        }
       if(i<j && a[j]< x)
       {
            int temp ;
             temp = a[j];
            a[j] = a[i];
            a[i] = temp;
            i++;
       }
  while(i<j && a[i]<=x)
      {
           i++;
      }
       if(i<j && a[i]>x)
      {
           int temp;
temp = a[i];
        a[i] = a[j];
        a[j] = temp;
       j--;
     }
}
     quick_sort(a,s,j-1);
    quick_sort(a,i+1,e);
   }
} 
void shell_sort(int a[],int num)
{
  int deep = num;
    while(deep >1)
     {
    deep=(deep+1)/2;
    for(int i=0;i<num-deep;i++)
       {
if(a[i+deep] < a[i])
         {
             int temp = a[i+deep];
                a[i+deep] = a[i];
         a[i] = temp;
     }
      }
        for(int i=0;i<8;i++)
             {
          cout<<a[i]<<" ";
             }
       cout<<endl;
     }
}
 
void insert_sort(int a[],int num)
{
     int temp;
    for(int i=1;i<num;i++)
   {
      temp = a[i];
      int compare = i;
       while(compare >0)
               {
       if(a[compare]<a[compare-1])
               {
                  int p=a[compare];
                  a[compare] = a[compare-1];
            a[compare-1] = p;
              }
         else
  {
           temp =a[compare];
             }
     compare--;
  }
                
     for(int i=0;i<num;i++)
     {
  cout<<a[i]<<" ";
  }
     cout<<endl;
   }
}
 
void print(int a[] , int num)
{
   for(int i=0;i<num;i++)
  {
      cout<<a[i]<<" ";
    }
    cout<<endl;
}
 
void main()
{
     int a[8]={49 , 38 , 21 , 13 , 52 ,7 ,55 ,49};
     //bubble_sort(a,8);
     cout<<"冒泡排序的结果"<<endl;
    //print(a,8);
   //select_sort(a,8);
     cout<<"选择排序的结果"<<endl;
     //print(a,8);
    //quick_sort(a,0,7);
     cout<<"快速排序的结果"<<endl;
     //print(a,8);
    //shell_sort(a,8);
    cout<<"Shell排序的结果"<<endl;
    //insert_sort(a,8);
     print(a,8);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值