• 排序算法1:冒泡排序、快速排序、插入排序


    排序算法:交换类排序,插入类排序、选择类排序、归并类排序

    交换类排序:冒泡排序、快速排序

    一、冒泡排序

    1. #include
    2. #include
    3. #include
    4. typedef int ElemType;
    5. typedef struct{
    6. ElemType *elem; //整形指针,申请的堆空间的起始地址存入elem
    7. int TableLen; //存储动态数组里边元素的个数
    8. }SSTable;
    9. void ST_Init(SSTable &ST,int len){
    10. ST.TableLen=len;
    11. ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen);
    12. int i;
    13. srand(time(NULL)); //随机数生成
    14. for(i=0;i
    15. ST.elem[i]=rand()%100;
    16. }
    17. }
    18. void ST_print(SSTable ST){
    19. int i;
    20. for(i=0;i
    21. printf("%3d",ST.elem[i]);
    22. }
    23. printf("\n");
    24. }
    25. void swap(ElemType &a,ElemType &b){
    26. ElemType tmp;
    27. tmp=a;
    28. a=b;
    29. b=tmp;
    30. }
    31. //两层循环
    32. //优先写内层循环,再写外层循环
    33. void BubbleSort(ElemType A[],int n){
    34. int i,j;
    35. bool flag;
    36. for(i=0;i-1;i++){ //控制的是有序数的数目
    37. flag=false;
    38. for(j=n-1;j>i;j--){ //内层控制比较和交换
    39. if(A[j-1]>A[j]){
    40. swap(A[j-1],A[j]);
    41. flag=true;
    42. }
    43. }
    44. if(flag==false){ //如果一趟没有发生任何交换,说明数组有序,提前结束排序
    45. return;
    46. }
    47. }
    48. }
    49. int main(){
    50. SSTable ST;
    51. ST_Init(ST,10);
    52. ST_print(ST);
    53. BubbleSort(ST.elem,10);
    54. ST_print(ST);
    55. return 0;
    56. }

     时间复杂度:内层是j>i,外层是从0到n-1,运行的总次数是1+2+3+4+...+n-1,即O(n^{2})

    空间复杂度:O(1),没有使用额外空间,不会因为n的变化而变化

    如果数组本身有序,最好的时间复杂度是O(n)

    二、快速排序

    核心:分治思想

    1. #include
    2. #include
    3. #include
    4. typedef int ElemType;
    5. typedef struct{
    6. ElemType *elem; //整形指针,申请的堆空间的起始地址存入elem
    7. int TableLen; //存储动态数组里边元素的个数
    8. }SSTable;
    9. void ST_Init(SSTable &ST,int len){
    10. ST.TableLen=len;
    11. ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen);
    12. int i;
    13. srand(time(NULL)); //随机数生成
    14. for(i=0;i
    15. ST.elem[i]=rand()%100;
    16. }
    17. }
    18. void ST_print(SSTable ST){
    19. int i;
    20. for(i=0;i
    21. printf("%3d",ST.elem[i]);
    22. }
    23. printf("\n");
    24. }
    25. void swap(ElemType &a,ElemType &b){
    26. ElemType tmp;
    27. tmp=a;
    28. a=b;
    29. b=tmp;
    30. }
    31. int Partition(ElemType A[],int low,int high){
    32. ElemType pivot=A[low]; //拿最左边的作为分割值,并存储下来
    33. while(low
    34. while(low=pivot){ //从后往前遍历,找到一个比分割值小的
    35. --high;
    36. }
    37. A[low]=A[high];
    38. while(low
    39. ++low;
    40. }
    41. A[high]=A[low];
    42. }
    43. A[low]=pivot;
    44. return low; //返回分割值所在的下标
    45. }
    46. //递归实现
    47. void QuickSort(ElemType A[],int low,int high){
    48. if(low
    49. int pivotpos=Partition(A,low,high);
    50. QuickSort(A,low,pivotpos-1);
    51. QuickSort(A,pivotpos+1,high);
    52. }
    53. }
    54. int main(){
    55. SSTable ST;
    56. ST_Init(ST,10);
    57. ST_print(ST);
    58. QuickSort(ST.elem,0,9);
    59. ST_print(ST);
    60. return 0;
    61. }

    空间复杂度与n有关 

    三、插入排序

    插入排序:直接插入排序、折半插入排序、希尔排序

    直接插入排序

    1. #include
    2. #include
    3. #include
    4. typedef int ElemType;
    5. typedef struct{
    6. ElemType *elem; //整形指针,申请的堆空间的起始地址存入elem
    7. int TableLen; //存储动态数组里边元素的个数
    8. }SSTable;
    9. void ST_Init(SSTable &ST,int len){
    10. ST.TableLen=len;
    11. ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen);
    12. int i;
    13. srand(time(NULL)); //随机数生成
    14. for(i=0;i
    15. ST.elem[i]=rand()%100;
    16. }
    17. }
    18. void ST_print(SSTable ST){
    19. int i;
    20. for(i=0;i
    21. printf("%3d",ST.elem[i]);
    22. }
    23. printf("\n");
    24. }
    25. void InsertSort(ElemType *arr,int n){
    26. int i,j,insertVal;
    27. for(i=1;i//控制要插入的数
    28. insertVal=arr[i]; //先保存要插入的值
    29. //内层控制比较,往后覆盖
    30. for(j=i-1;j>=0&&arr[j]>insertVal;j--){
    31. arr[j+1]=arr[j];
    32. }
    33. arr[j+1]=insertVal;
    34. }
    35. }
    36. int main(){
    37. SSTable ST;
    38. ST_Init(ST,10);
    39. ST_print(ST);
    40. InsertSort(ST.elem,10);
    41. ST_print(ST);
    42. return 0;
    43. }

  • 相关阅读:
    RBAC模型 && 各表设计与梳理
    java-php-net-python-代驾网站计算机毕业设计程序
    国内常用源开发环境换源(flutter换源,python换源,Linux换源,npm换源)
    chatgpt赋能python:Python文件大小:如何优化和管理您的文件大小
    JavaScript中的Error错误对象与自定义错误类型
    多视角碰撞,探索 Serverless 企业落地更多可能性丨阿里云用户组厦门站
    MYSQL 窗体汇总函数
    PhPstudy小皮面板和navicat数据库图像化软件的使用教程
    C++布隆过滤器和哈西切分
    python tornado(4)路由传参
  • 原文地址:https://blog.csdn.net/m0_60651303/article/details/136208332