码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C++ 算法教程


     归并排序
    1. #include
    2. using namespace std;
    3. template <class T>
    4. void Merge(T data[],int start,int mid,int end)
    5. {
    6. int len1 = mid - start + 1, len2 = end - mid;
    7. int i, j, k;
    8. T* left = new int[len1];
    9. T* right = new int[len2];
    10. for (i = 0; i < len1; i++)
    11. left[i] = data[i + start];
    12. for (i = 0; i < len2; i++)
    13. right[i] = data[i + mid + 1];
    14. i = 0, j = 0;
    15. for (k = start; k < end; k++)
    16. {
    17. if (i == len1 || j == len2)
    18. break;
    19. if (left[i] <= right[j])
    20. data[k] = left[i++];
    21. else
    22. data[k] = right[j++];
    23. }
    24. while (i < len1)
    25. data[k++] = left[i++];
    26. while (j < len2)
    27. data[k++] = right[j++];
    28. delete[] left;
    29. delete[] right;
    30. }
    31. template <class T>
    32. void MergeSort(T data[], int start, int end)
    33. {
    34. if (start < end)
    35. {
    36. int mid = (start + end) / 2;
    37. MergeSort(data, start,mid);
    38. MergeSort(data, mid + 1, end);
    39. Merge(data, start, mid, end);
    40. }
    41. }
    42. void show(int*temp,int n)
    43. {
    44. for (int i = 0; i < n; i++)
    45. cout << temp[i] << " ";
    46. }
    47. void main()
    48. {
    49. int temp[8];
    50. for (int i = 0; i < 8; i++)
    51. cin >> temp[i];
    52. MergeSort<int>(temp, 0,7);
    53. show(temp, 8);
    54. }

    冒泡排序_相邻交换

    1. #include
    2. int main()
    3. {
    4. int i,p,temp;
    5. int array[10] = {2,6,1,9,4,7,5,8,3,0};
    6. printf("Display this array:\n");
    7. for(i=0;i<10;i++)
    8. {
    9. printf("%d ",array[i]);
    10. }
    11. for(i = 1;i < 10; ++i)
    12. {
    13. for(p = 0; p < 10; ++p)
    14. {
    15. if(array[i]>array[p])
    16. {
    17. array[i] = array[i]^array[p];
    18. array[p] = array[i]^array[p];
    19. array[i] = array[i]^array[p];
    20. }
    21. }
    22. }
    23. printf("\n");
    24. printf("After sorting,this array is:\n");
    25. for(i=0;i<10;i++)
    26. {
    27. printf("%d ",array[i]);
    28. }
    29. printf("\n");
    30. return 0;
    31. }
    判断循环链表

    1.建立set集合,每次遍历存储元素,当集合大小不变,但循环仍在继续时说明存在循环,并得出该位置
    2.定义双指针遍历:一个指针每次移动一个节点,一个指针每次移动2个节点,当2个节点指针在一个节点指针后面时,此链表存在循环。
    3.链表反向
    4.构造双向链表

    swap

    a:1001
    b:1100
    a=a^b;  a:0101
    b=a^b;  b:1001
    a=a^b;  a:1100

    a = a + b;
    b = a - b;
    a = a - b;

    直插排序
    1. #include
    2. int main()
    3. {
    4. int i,p,temp;
    5. int array[10] = {2,6,1,9,4,7,5,8,3,0};
    6. printf("Display this array:\n");
    7. for(i=0;i<10;i++)
    8. {
    9. printf("%d ",array[i]);
    10. }
    11. //选择第一个数做为起始排序
    12. for(i = 1; i < 10; ++i)
    13. {
    14. temp = array[i]; //待插入排序数
    15. for(p = i-1; p >=0 && array[p]>temp; --p)
    16. {//遍历已排序数列表
    17. {//如果当前数大小在已排序范围中,开始向右位移一个数让出空间
    18. array[p+1] = array[p];
    19. }
    20. }
    21. //找到待排序数的位置,在让出的空间直接插入
    22. array[p+1]=temp;
    23. }
    24. printf("\n");
    25. printf("After sorting,this array is:\n");
    26. for(i=0;i<10;i++)
    27. {
    28. printf("%d ",array[i]);
    29. }
    30. printf("\n");
    31. return 0;
    32. }

    1. #include
    2. int main()
    3. {
    4. int i,j,t;
    5. int array[10]={2,7,1,8,5,9,3,4,0,6};
    6. printf("\nDisplay this array:\n");
    7. for(i=0;i<10;i++)
    8. {
    9. printf("%d ",array[i]);
    10. }
    11. printf("\n");
    12. for(i=1;i<=9;i++)
    13. { //遍历
    14. int t = i-1; //假设当前数为最小数
    15. for(j=i;j<10;j++)
    16. {
    17. if(array[j]
    18. { //遍历找到最小的数,保存最小数索引
    19. t=j;
    20. }
    21. }
    22. if(t!=(i-1))
    23. { //交换最小数与假设最小数
    24. int temp = 0;
    25. temp=array[i-1];
    26. array[i-1]=array[t];
    27. array[t]=temp;
    28. }
    29. }
    30. printf("After sorting,this array is:\n");
    31. for(i=0;i<10;i++)
    32. {
    33. printf("%d ",array[i]);
    34. }
    35. printf("\n");
    36. return 0;
    37. }

    https://github.com/sashafierce/100-days-of-Algorithm-Challenge

    GitHub - hackerkid/Awesome-Data-Structures: C++ implementation of basic data structures and algorithms

    GitHub - hackerkid/LightOJ-Solutions: :sparkles: LightOJ Solutions with hints

    GitHub - mmc-maodun/Data-Structure-And-Algorithm: Data Structure And Algorithm(常用数据结构与算法C/C++实现)

    https://leetcode.com/

    Codewars - Achieve mastery through coding practice and developer mentorship

    Khan Academy | Free Online Courses, Lessons & Practice

    https://github.com/sashafierce/Algo_Ds_Notes

    位操作基础篇之位操作全面总结_c++位操作-CSDN博客

    https://www.cnblogs.com/findumars/p/5180528.html


    创作不易,小小的支持一下吧!

  • 相关阅读:
    【无标题】
    华为云Stack的学习(九)
    商业智能BI的前景如何?看完这篇文章你就明白了
    【Confluence】使用start-confluence.sh命令重启后提示找不到网页HTTP404
    Nacos 服务治理(服务注册中心)
    centos统计子目录和文件的大小并排序
    期货平盘(期货大单压盘)
    Redis与数据库的爱恨纠葛
    1500*C1. Potions (Easy Version)(贪心&优先队列)
    【Scala专栏】方法和函数
  • 原文地址:https://blog.csdn.net/qq_30220519/article/details/139724486
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号