2025暑假笔记2

作者:第二章 - 图1

程序设计基础知识

算法的特性

第二章 - 图2

算法的复杂度

-时间复杂度 -空间复杂度

c++程序设计

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int a(int x){
  4. if (x==1)return 1;
  5. return a(x-1)+x;
  6. }
  7. int main ()
  8. {
  9. int n;
  10. cin>>n;
  11. cout<<n;
  12. return 0;
  13. }<bookstack-summary></bookstack-summary>

算法的复杂度

时间复杂度 空间复杂度 第二章 - 图3

c++语言基础

数据类型

第二章 - 图4

函数

第二章 - 图5

示例:

例如编写一求1+2+..+n的值,其中n<=20

  1. #include <iostream>
  2. using namespace std;
  3. int add(int n){
  4. if(n==1) {
  5. return 1;
  6. }
  7. return add(n-1) + n;
  8. 1 + 2 + 3
  9. }
  10. int main(){
  11. cout << add(4);
  12. return 0;
  13. }

基础排序

常见算法复杂度与稳定性

第二章 - 图6

冒泡排序

小的元素会经由交换慢慢“浮”到顶端,就像泡泡一样,故名“冒泡排序”。

它的工作原理是,重复地走访过要排序的元素,依次比较两个相邻的两个元素,如果前面的数比后面的数大就把他们交换过来。

走访元素的工作重复地进行,直到没有相邻元素需要交换

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int SIZE = 10;
  4. int arr[SIZE] = {2,7,8,4,36,78,1,91,42,13};
  5. int main(){
  6. for(int i = 0 ; i < SIZE - 1; i++){
  7. for(int j = 0 ; j < SIZE-1 -i; j++){
  8. if(arr[j] > arr[j+1])
  9. swap(arr[j],arr[j+1]);
  10. }
  11. }
  12. for(int x : arr) cout << x << " ";
  13. return 0;
  14. }