zoukankan      html  css  js  c++  java
  • 编程菜鸟的日记-初学尝试编程-整理内部排序(交换排序(冒泡排序和快速排序)、直接选择排序、直接插入排序,希尔排序)

    //首先是交换排序中的冒泡排序,时间复杂度o(n^2),最好情况下是正序则交换次数为0,时间复杂度为o(n)
    //空间复杂度为o(1)
    //是一个稳定的排序算法
    #include <iostream>
    using namespace std;
    typedef int ElemType;
    void BubbleSort(ElemType R[],int n)
    {
     int i,j,temp;
     for(i=0;i<n-1;i++)//注意i的范围只有0 ~ (n-1),n个数只需要比较n-1次即可
     {
      for(j=n-1;j>i;j--)//将第i+1至n-1个数进行逐个比较,从后往前比较,较小的交换到前面的位置
      {
       if(R[j]<R[j-1])
       {
        temp=R[j];
        R[j]=R[j-1];
        R[j-1]=temp;
       }
      }
     }
    }
    //交换排序中的快速排序,被认为是最高效的排序算法
    //时间复杂度最好的是o(n*log2 n),(此时序列基准刚好是中指)
    //时间复杂度最差的是o(n^2),空间复杂度为o(1),不稳定
    void QuickSort(ElemType R[],int begin,int end)
    {
     int i=begin,j=end;//分别是R的开始位置和结束位置
     if(begin<end)
     {
      ElemType temp=R[begin];//将第一个元素作为基准元素
      while(i!=j)
      {
       while(j>i && R[j]>=temp)
        j--;
       R[i]=R[j];
       while(i<j && R[i]<=temp)
        i++;
       R[j]=R[i];
      }
      R[i]=temp;
      QuickSort(R,begin,i-1);//从第i个位置分成左右两段,对左区域进行排序,
      QuickSort(R,i+1,end);//对右区域进行排序
     }
     
    }
    //选择排序:直接选择排序(简单选择排序),它是一个就地排序。
    //时间复杂度o(n^2),空间复杂度为o(1)
    void SelectSort(ElemType R[],int n)
    {
     ElemType temp;
     for(int i=0;i<n-1;i++)//i为无序区,从0开始一直到数组最后一个
     {
      int k=i;
      for(int j=i+1;j<=n-1;j++)
      {
       if(R[j]<R[k])
        k=j;
       //if(k!=i)
       {
        temp=R[k];
        R[k]=R[i];
        R[i]=temp;
       }
      }
     }
    }
    //堆排序:
    //插入排序:直接插入排序和shell排序
    //将关键字的值直接插入到前面已经排好序的序列中,直到全部待排序的全部插入完成为止
    //直接插入排序,稳定的排序方法
    void InsertSort(ElemType R[],int n)
    {
     ElemType temp;
     for(int i=1;i<=n-1;i++)
     {
      temp=R[i];
      int j=i-1;
      while(j>=0 && temp<R[j])
      {
       R[j+1]=R[j];
       j--;
      }
      R[j+1]=temp;//在j+1处插入R[i]
     }
    }
    //shell排序:分组插入,先取增量为d,进行分组,进行组内插入排序,
    //然后增量d逐渐减小,重复上述过程,直到增量为1,所有记录在同一组中进行直接插入排序为止
    void ShellSort(ElemType R[],int n)
    {
     int i,j,gap;
     ElemType temp;
     gap=n/2;
     while(gap>0)
     {
      for(i=gap;i<n;i++)
      {
       temp=R[i];
       j=i-gap;
       while(j>=0 && temp<R[j])
       {
        R[j+gap]=R[j];
        j=j-gap;
       }
       R[j+gap]=temp;
       j=j-gap;
      }
      gap=gap/2;//减小增量
     }
    }

    void main()

     ElemType R[]={1,4,3,2,0};
     int n=sizeof(R)/sizeof(ElemType);
     //BubbleSort(R,n);
     //QuickSort(R,0,n-1);
     //SelectSort(R,n);
        //InsertSort(R,n);
     ShellSort(R,n);
     for(int i=0;i<=n-1;i++)
     {
      cout<<R[i]<<" ";
     }
     cout<<endl;
     system("pause");
    }

  • 相关阅读:
    STM32 HAL库学习笔记
    嵌入式Linux学习笔记
    AVR_Interrupt
    shutdown命令用法
    ULINK2 USB电脑无法识别(连接电脑后,设备管理器显示未知设备)
    MDK中编译程序后Program Size详解
    Keil(MDK-ARM)系列教程(三)_工程目标选项配置(Ⅰ)
    第48章 MDK的编译过程及文件类型全解
    Do not access Object.prototype method ‘hasOwnProperty’ from target object no-prototype-builtins
    让vscode按照eslint进行格式化
  • 原文地址:https://www.cnblogs.com/lynnycy/p/3411173.html
Copyright © 2011-2022 走看看