What is the method for sorting a one-dimensional array …

In C language, various sorting algorithms can be used to sort one-dimensional arrays. Common sorting algorithms include bubble sort, selection sort, insertion sort, quicksort, and merge sort.

Here is a simple example code for bubble sorting:

#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]) {
                // 交换arr[j]和arr[j+1]的值
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}

int main() {
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(arr)/sizeof(arr[0]);
    bubbleSort(arr, n);
    printf("排序后的数组:");
    for (int i=0; i < n; i++)
        printf("%d ", arr[i]);
    return 0;
}

The output result is: 11 12 22 25 34 64 90.

The code above demonstrates the implementation of the bubble sort algorithm. The implementation of other sorting algorithms is similar, with only differences in the specific comparison and swapping operations. One can choose the appropriate sorting algorithm based on actual needs.

bannerAds