打印
[软件资料]

C语言常见算法

[复制链接]
32|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
forgot|  楼主 | 2025-3-31 20:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
# C语言常见算法

C语言中常用的算法可以分为以下几大类:

## 1. 排序算法

### 冒泡排序 (Bubble Sort)
```c
void bubbleSort(int arr[], int n) {
    for (int i = 0; i < n-1; i++) {
        for (int j = 0; j < n-i-1; j++) {
            if (arr[j] > arr[j+1]) {
                // 交换
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}
```

### 选择排序 (Selection Sort)
```c
void selectionSort(int arr[], int n) {
    for (int i = 0; i < n-1; i++) {
        int min_idx = i;
        for (int j = i+1; j < n; j++) {
            if (arr[j] < arr[min_idx]) {
                min_idx = j;
            }
        }
        // 交换
        int temp = arr[min_idx];
        arr[min_idx] = arr[i];
        arr[i] = temp;
    }
}
```

### 插入排序 (Insertion Sort)
```c
void insertionSort(int arr[], int n) {
    for (int i = 1; i < n; i++) {
        int key = arr[i];
        int j = i - 1;
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = key;
    }
}
```

### 快速排序 (Quick Sort)
```c
void quickSort(int arr[], int low, int high) {
    if (low < high) {
        int pi = partition(arr, low, high);
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}

int partition(int arr[], int low, int high) {
    int pivot = arr[high];
    int i = (low - 1);
    for (int j = low; j <= high - 1; j++) {
        if (arr[j] < pivot) {
            i++;
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return (i + 1);
}

void swap(int* a, int* b) {
    int t = *a;
    *a = *b;
    *b = t;
}
```

## 2. 查找算法

### 线性查找 (Linear Search)
```c
int linearSearch(int arr[], int n, int x) {
    for (int i = 0; i < n; i++) {
        if (arr[i] == x) {
            return i;
        }
    }
    return -1;
}
```

### 二分查找 (Binary Search)
```c
int binarySearch(int arr[], int l, int r, int x) {
    while (l <= r) {
        int m = l + (r - l) / 2;
        if (arr[m] == x) {
            return m;
        }
        if (arr[m] < x) {
            l = m + 1;
        } else {
            r = m - 1;
        }
    }
    return -1;
}
```

## 3. 递归算法

### 阶乘计算
```c
int factorial(int n) {
    if (n == 0 || n == 1) {
        return 1;
    }
    return n * factorial(n - 1);
}
```

### 斐波那契数列
```c
int fibonacci(int n) {
    if (n <= 1) {
        return n;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
}
```

## 4. 数学算法

### 最大公约数 (GCD)
```c
int gcd(int a, int b) {
    if (b == 0) {
        return a;
    }
    return gcd(b, a % b);
}
```

### 最小公倍数 (LCM)
```c
int lcm(int a, int b) {
    return (a * b) / gcd(a, b);
}
```

### 素数判断
```c
int isPrime(int n) {
    if (n <= 1) return 0;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            return 0;
        }
    }
    return 1;
}
```

## 5. 字符串算法

### 字符串反转
```c
void reverseString(char* str) {
    int n = strlen(str);
    for (int i = 0; i < n / 2; i++) {
        char temp = str[i];
        str[i] = str[n - i - 1];
        str[n - i - 1] = temp;
    }
}
```

### 字符串比较
```c
int stringCompare(char *str1, char *str2) {
    while (*str1 && *str2 && *str1 == *str2) {
        str1++;
        str2++;
    }
    return *str1 - *str2;
}
```

## 6. 数据结构相关算法

### 链表反转
```c
struct Node* reverseList(struct Node* head) {
    struct Node* prev = NULL;
    struct Node* current = head;
    struct Node* next = NULL;
   
    while (current != NULL) {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }
    return prev;
}
```

### 二叉树遍历 (前序)
```c
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
};

void preorderTraversal(struct TreeNode* root) {
    if (root == NULL) return;
    printf("%d ", root->val);
    preorderTraversal(root->left);
    preorderTraversal(root->right);
}
```

## 7. 其他常用算法

### 位操作算法
```c
// 计算二进制中1的个数
int countSetBits(int n) {
    int count = 0;
    while (n) {
        count += n & 1;
        n >>= 1;
    }
    return count;
}

// 判断是否是2的幂
int isPowerOfTwo(int n) {
    return n && (!(n & (n - 1)));
}
```

### 动态规划 - 斐波那契数列 (优化版)
```c
int fib(int n) {
    int a = 0, b = 1, c, i;
    if (n == 0) return a;
    for (i = 2; i <= n; i++) {
        c = a + b;
        a = b;
        b = c;
    }
    return b;
}
```

这些算法是C语言编程中常见的基础算法,掌握它们对于提高编程能力和解决实际问题非常有帮助。

使用特权

评论回复
评论
qintian0303 2025-4-1 17:12 回复TA
可以使用论坛的代码功能试一试 
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1896

主题

13870

帖子

57

粉丝