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

发布时间:2024年01月22日

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

  1. 冒泡排序(Bubble Sort):
    #include <stdio.h>
    
    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;
                }
            }
        }
    }
    
    int main() {
        int array[] = {64, 34, 25, 12, 22, 11, 90};
        int n = sizeof(array) / sizeof(array[0]);
    
        bubbleSort(array, n);
    
        printf("Sorted array: ");
        for (int i = 0; i < n; i++) {
            printf("%d ", array[i]);
        }
    
        return 0;
    }
    2、选择排序(Selection Sort):
    #include <stdio.h>
    
    void selectionSort(int arr[], int n) {
        for (int i = 0; i < n-1; i++) {
            int minIndex = i;
            for (int j = i+1; j < n; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            int temp = arr[i];
            arr[i] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }
    
    int main() {
        int array[] = {64, 34, 25, 12, 22, 11, 90};
        int n = sizeof(array) / sizeof(array[0]);
    
        selectionSort(array, n);
    
        printf("Sorted array: ");
        for (int i = 0; i < n; i++) {
            printf("%d ", array[i]);
        }
    
        return 0;
    }
    3、插入排序(Insertion Sort):
    #include <stdio.h>
    
    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;
        }
    }
    
    int main() {
        int array[] = {64, 34, 25, 12, 22, 11, 90};
        int n = sizeof(array) / sizeof(array[0]);
    
        insertionSort(array, n);
    
        printf("Sorted array: ");
        for (int i = 0; i < n; i++) {
            printf("%d ", array[i]);
        }
    
        return 0;
    }

    4、快速排序(Quick Sort):

    #include <stdio.h>
    
    void quickSort(int arr[], int low, int high) {
        if (low < high) {
            int pivot = partition(arr, low, high);
    
            quickSort(arr, low, pivot - 1);
            quickSort(arr, pivot + 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 temp = *a;
        *a = *b;
        *b = temp;
    }
    
    int main() {
        int array[] = {64, 34, 25, 12, 22, 11, 90};
        int n = sizeof(array) / sizeof(array[0]);
    
        quickSort(array, 0, n - 1);
    
        printf("Sorted array: ");
        for (int i = 0; i < n; i++) {
            printf("%d ", array[i]);
        }
    
        return 0;
    }

    5、归并排序(Merge Sort):

    #include <stdio.h>
    #include <stdlib.h>
    
    void mergeSort(int arr[], int left, int right) {
        if (left < right) {
            int middle = left + (right - left) / 2;
    
            mergeSort(arr, left, middle);
            mergeSort(arr, middle + 1, right);
    
            merge(arr, left, middle, right);
        }
    }
    
    void merge(int arr[], int left, int middle, int right) {
        int i, j, k;
        int n1 = middle - left + 1;
        int n2 = right - middle;
    
        int L[n1], R[n2];
    
        for (i = 0; i < n1; i++)
            L[i] = arr[left + i];
        for (j = 0; j < n2; j++)
            R[j] = arr[middle + 1 + j];
    
        i = 0;
        j = 0;
        k = left;
        while (i < n1 && j < n2) {
            if (L[i] <= R[j]) {
                arr[k] = L[i];
                i++;
            } else {
                arr[k] = R[j];
                j++;
            }
            k++;
        }
    
        while (i < n1) {
            arr[k] = L[i];
            i++;
            k++;
        }
    
        while (j < n2) {
            arr[k] = R[j];
            j++;
            k++;
        }
    }
    
    int main() {
        int array[] = {64, 34, 25, 12, 22, 11, 90};
        int n = sizeof(array) / sizeof(array[0]);
    
        mergeSort(array, 0, n - 1);
    
        printf("Sorted array: ");
        for (int i = 0; i < n; i++) {
            printf("%d ", array[i]);
        }
    
        return 0;
    }

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

文章来源:https://blog.csdn.net/u013032788/article/details/135743788
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。