在 C 中,我无法将索引从高到低排序,反之亦然

In C, I'm having trouble getting an Index sorted High to Low and vice versa

我正在做一个涉及预设数组的项目: primaryArray[8] = {8, 4, 2, 16, 32, 124, 64, 256}; 我在 main 之前调用一个函数,它不太工作。这是代码。

    void lowhighSort(int primaryArray[], int arrayLength) {
    int i;
    int a;
    int indexMin;

    for (i = 0; i < arrayLength - 1; ++i){
        indexMin = i;

        for (a = (i + 1) + 1; a < arrayLength; ++a) {
            if (primaryArray[a] < primaryArray[indexMin]){
                indexMin = a;
            }
                simpleSwap(&primaryArray[indexMin], &primaryArray[i]);
        }
    }
}

    void highlowSort(int primaryArray[], int arrayLength) {
        int i;
    int a;
    int indexMin;

    for (i = 0; i < arrayLength - 1; ++i){
        indexMin = i;
        for (a = i + 1; a < arrayLength; ++a) {
            if(primaryArray[a] > primaryArray[indexMin])
                indexMin = a;
                simpleSwap(&primaryArray[indexMin], &primaryArray[i]);
        }
    }
}

我的“simpleSwap”可能是错误的,在学习时我并没有完全理解 * 的作用。无论如何,这里也是:

    void simpleSwap(int* x, int* y) {
    int temp = *x;

    *x = *y;
    *y = temp;
} 

*编辑 这是“更大”代码的一部分,所以我不完全确定错误是否出在其他地方。

#include <stdio.h>
#include <stdlib.h>
void simpleSwap(double* x, double* y) {
    double temp = *x;

    *x = *y;
    *y = temp;
}

double largest(double Array[], double Length) {
    double largestNum = Array[0];

    for (int i = 1; i < Length; ++i) {
        if(largestNum < Array[i]) {
            largestNum = Array[i];
        }
    }
    return largestNum;
}

double smallest(double Array[], double Length) {
    double smallestNum = Array[0];

    for (int i = 1; i < Length; ++i) {
        if(smallestNum > Array[i]) {
            smallestNum = Array[i];
        }
    }
    return smallestNum;
}
void lowhighSort(double Array[], double Length) {

    int a;
    int indexMin;

    for (int i = 0; i < Length; ++i){
        indexMin = i;

        for (a = (i + 1); a < Length; ++a) {
            if (Array[a] < Array[indexMin]){
                indexMin = a;

                simpleSwap(&Array[indexMin], &Array[i]);
            }
        }
    }
}

void highlowSort(double Array[], double Length) {

    int a;
    int indexMin;

    for (int i = 0; i < Length; ++i){
        indexMin = i;

        for (a = (i + 1); a < Length; ++a) {
            if(Array[a] > Array[indexMin]){
                indexMin = a;
                simpleSwap(&Array[indexMin], &Array[i]);
            }
        }
    }
}

float arrayAverage (double Array[], double Length) {
    int i;
    double arrayAverage = 0;
    for (i = 0; i < Array[i]; ++i) {
        arrayAverage = Array[i] + arrayAverage;
    }
    return arrayAverage;
}


int main()
{

    double primaryArray[8] = {8, 4, 2, 16, 32, 124, 64, 256};
    double arrayLength = sizeof(primaryArray)/sizeof(primaryArray[0]);
    double ancillaryArray[10] = {8, 4, 2, 16, 32, 124, 64, 256};

    printf("Primary Array:\n");

    printf("\nLargest Array Element: %.1lf\n", largest(primaryArray, arrayLength));

    printf("Smallest Array Element: %.1lf\n\n", smallest(primaryArray, arrayLength));

    lowhighSort(primaryArray, arrayLength);
    printf("Array Ascending Order: ");
    for(int i = 0; i < 8; ++i){
        printf("%.1lf, ", primaryArray[i]);
    }
    printf("\n");

    highlowSort(primaryArray, arrayLength);
    printf("Array Descending Order: ");
    for(int i = 0; i < 8; ++i){
        printf("%.1lf, ", primaryArray[i]);
    }
    printf("\n");
    printf("\nArray Element Average: %.3lf", (arrayAverage(primaryArray, arrayLength))/8);


    int ancillaryarrayLength = sizeof(ancillaryArray)/sizeof(ancillaryArray[0]);

    printf("\n\nPlease Finish Ancillary Array (Max 10 Elements)\nCurrent Elements: ");
    for(int i = 0; i < arrayLength; ++i) {
        printf("%.1lf, ", ancillaryArray[i]);
    }

    printf("\nEnter Elements: ");
    for (int i = 8; i < 10; ++i) {
        scanf("%lf", &ancillaryArray[i]);
    }


    printf("\nLargest Array Element: %.1lf\n", largest(ancillaryArray, ancillaryarrayLength));

    printf("Smallest Array Element: %.03lf\n", smallest(ancillaryArray, ancillaryarrayLength));

    lowhighSort(ancillaryArray, ancillaryarrayLength);
    printf("Array Ascending Order: ");
    for(int i = 0; i < 10; ++i){
        printf("%.3lf, ", ancillaryArray[i]);
    }
    printf("\n");

    highlowSort(ancillaryArray, ancillaryarrayLength);
    printf("Array Descending Order: ");
    for(int i = 0; i < 10; ++i){
        printf("%.3lf, ", ancillaryArray[i]);
    }
    printf("\n");

    printf("Array Element Average: %.4lf", (arrayAverage(ancillaryArray, ancillaryarrayLength))/10);


    return 0;
}

这是最终代码,它按预期工作!谢谢大家的帮助。