当前位置:   article > 正文

C 语言实现几种常见的排序算法的简单示例代码,包括冒泡排序、选择排序、插入排序、快速排序和归并排序。(附带源码)_选择排序法和冒泡排序法c语言代码

选择排序法和冒泡排序法c语言代码

以下是使用 C 语言实现几种常见的排序算法的简单示例代码,包括冒泡排序、选择排序、插入排序、快速排序和归并排序。

  1. 冒泡排序(Bubble Sort):
    1. #include <stdio.h>
    2. void bubbleSort(int arr[], int n) {
    3. for (int i = 0; i < n-1; i++) {
    4. for (int j = 0; j < n-i-1; j++) {
    5. if (arr[j] > arr[j+1]) {
    6. int temp = arr[j];
    7. arr[j] = arr[j+1];
    8. arr[j+1] = temp;
    9. }
    10. }
    11. }
    12. }
    13. int main() {
    14. int array[] = {64, 34, 25, 12, 22, 11, 90};
    15. int n = sizeof(array) / sizeof(array[0]);
    16. bubbleSort(array, n);
    17. printf("Sorted array: ");
    18. for (int i = 0; i < n; i++) {
    19. printf("%d ", array[i]);
    20. }
    21. return 0;
    22. }
    2、选择排序(Selection Sort):
    1. #include <stdio.h>
    2. void selectionSort(int arr[], int n) {
    3. for (int i = 0; i < n-1; i++) {
    4. int minIndex = i;
    5. for (int j = i+1; j < n; j++) {
    6. if (arr[j] < arr[minIndex]) {
    7. minIndex = j;
    8. }
    9. }
    10. int temp = arr[i];
    11. arr[i] = arr[minIndex];
    12. arr[minIndex] = temp;
    13. }
    14. }
    15. int main() {
    16. int array[] = {64, 34, 25, 12, 22, 11, 90};
    17. int n = sizeof(array) / sizeof(array[0]);
    18. selectionSort(array, n);
    19. printf("Sorted array: ");
    20. for (int i = 0; i < n; i++) {
    21. printf("%d ", array[i]);
    22. }
    23. return 0;
    24. }
    3、插入排序(Insertion Sort):
    1. #include <stdio.h>
    2. void insertionSort(int arr[], int n) {
    3. for (int i = 1; i < n; i++) {
    4. int key = arr[i];
    5. int j = i - 1;
    6. while (j >= 0 && arr[j] > key) {
    7. arr[j+1] = arr[j];
    8. j--;
    9. }
    10. arr[j+1] = key;
    11. }
    12. }
    13. int main() {
    14. int array[] = {64, 34, 25, 12, 22, 11, 90};
    15. int n = sizeof(array) / sizeof(array[0]);
    16. insertionSort(array, n);
    17. printf("Sorted array: ");
    18. for (int i = 0; i < n; i++) {
    19. printf("%d ", array[i]);
    20. }
    21. return 0;
    22. }

    4、快速排序(Quick Sort):

    1. #include <stdio.h>
    2. void quickSort(int arr[], int low, int high) {
    3. if (low < high) {
    4. int pivot = partition(arr, low, high);
    5. quickSort(arr, low, pivot - 1);
    6. quickSort(arr, pivot + 1, high);
    7. }
    8. }
    9. int partition(int arr[], int low, int high) {
    10. int pivot = arr[high];
    11. int i = low - 1;
    12. for (int j = low; j <= high - 1; j++) {
    13. if (arr[j] < pivot) {
    14. i++;
    15. swap(&arr[i], &arr[j]);
    16. }
    17. }
    18. swap(&arr[i + 1], &arr[high]);
    19. return i + 1;
    20. }
    21. void swap(int* a, int* b) {
    22. int temp = *a;
    23. *a = *b;
    24. *b = temp;
    25. }
    26. int main() {
    27. int array[] = {64, 34, 25, 12, 22, 11, 90};
    28. int n = sizeof(array) / sizeof(array[0]);
    29. quickSort(array, 0, n - 1);
    30. printf("Sorted array: ");
    31. for (int i = 0; i < n; i++) {
    32. printf("%d ", array[i]);
    33. }
    34. return 0;
    35. }

    5、归并排序(Merge Sort):

    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. void mergeSort(int arr[], int left, int right) {
    4. if (left < right) {
    5. int middle = left + (right - left) / 2;
    6. mergeSort(arr, left, middle);
    7. mergeSort(arr, middle + 1, right);
    8. merge(arr, left, middle, right);
    9. }
    10. }
    11. void merge(int arr[], int left, int middle, int right) {
    12. int i, j, k;
    13. int n1 = middle - left + 1;
    14. int n2 = right - middle;
    15. int L[n1], R[n2];
    16. for (i = 0; i < n1; i++)
    17. L[i] = arr[left + i];
    18. for (j = 0; j < n2; j++)
    19. R[j] = arr[middle + 1 + j];
    20. i = 0;
    21. j = 0;
    22. k = left;
    23. while (i < n1 && j < n2) {
    24. if (L[i] <= R[j]) {
    25. arr[k] = L[i];
    26. i++;
    27. } else {
    28. arr[k] = R[j];
    29. j++;
    30. }
    31. k++;
    32. }
    33. while (i < n1) {
    34. arr[k] = L[i];
    35. i++;
    36. k++;
    37. }
    38. while (j < n2) {
    39. arr[k] = R[j];
    40. j++;
    41. k++;
    42. }
    43. }
    44. int main() {
    45. int array[] = {64, 34, 25, 12, 22, 11, 90};
    46. int n = sizeof(array) / sizeof(array[0]);
    47. mergeSort(array, 0, n - 1);
    48. printf("Sorted array: ");
    49. for (int i = 0; i < n; i++) {
    50. printf("%d ", array[i]);
    51. }
    52. return 0;
    53. }

    这些是一些常见的排序算法的 C 语言实现。每种算法都有其适用的场景和性能特点,选择适当的排序算法取决于具体的需求和数据希望你也学会了,更多编程请来二当家的素材网:https://www.erdangjiade.com

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/806133
推荐阅读
相关标签
  

闽ICP备14008679号