Here is an example of a C macro for performing quicksort on a three-dimensional array:

#define SWAP(a, b) { double temp = a; a = b; b = temp; }

void quickSort(double arr[][3][3], 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(double arr[][3][3], int low, int high) {
    double pivot = arr[high][0][0];
    int i = low - 1;

    for (int j = low; j <= high - 1; j++) {
        if (arr[j][0][0] < pivot) {
            i++;
            for (int k = 0; k < 3; k++) {
                for (int l = 0; l < 3; l++) {
                    SWAP(arr[i][k][l], arr[j][k][l]);
                }
            }
        }
    }

    for (int k = 0; k < 3; k++) {
        for (int l = 0; l < 3; l++) {
            SWAP(arr[i + 1][k][l], arr[high][k][l]);
        }
    }

    return i + 1;
}

int main() {
    double arr[5][3][3] = {
        {{4, 2, 1}, {3, 0, -1}, {5, 6, 7}},
        {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}},
        {{-1, -2, -3}, {-4, -5, -6}, {-7, -8, -9}},
        {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}},
        {{-5, 0, 5}, {0, -5, 5}, {0, 5, -5}}
    };

    int size = sizeof(arr) / sizeof(arr[0]);

    quickSort(arr, 0, size - 1);

    // Printing the sorted array
    for (int i = 0; i < size; i++) {
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 3; k++) {
                printf("%f ", arr[i][j][k]);
            }
            printf("\n");
        }
        printf("\n");
    }

    return 0;
}

This macro defines the 'SWAP' macro to swap two elements in the array. The 'quickSort' function takes the array, the low index, and the high index as parameters and recursively sorts the array using the 'partition' function. The 'partition' function selects the last element as the pivot and rearranges the elements such that all elements smaller than the pivot come before it, and all elements greater than the pivot come after it. Finally, the main function initializes the three-dimensional array, calls 'quickSort', and prints the sorted array.

C Macro for Quick Sorting 3D Arrays

原文地址: https://www.cveoy.top/t/topic/N0I 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录