简体   繁体   English

用C对数字数组进行排序

[英]Sorting array of numbers in C

// code to sort array of 16 numbers, but output isnt quite correct.
// must use pointers to array addresses
// final output is -451 993 384 201 89 77 38 28 16 12 7 1 0 -2 -5 -17
// as you can see -451 isn't in the right place.

output输出

-451 7 993 1 0 16 -5 12 89 28 77 384 -2 38 -17 201 -451 7 993 1 0 16 -5 12 89 28 77 384 -2 38 -17 201

-451 -17 993 7 1 16 0 12 89 28 77 384 -2 38 -5 201 -451 -17 993 7 1 16 0 12 89 28 77 384 -2 38 -5 201

-451 993 -17 7 1 16 0 12 89 28 77 384 -2 38 -5 201 -451 993 -17 7 1 16 0 12 89 28 77 384 -2 38 -5 201

-451 993 7 -17 1 16 0 12 89 28 77 384 -2 38 -5 201 -451 993 7 -17 1 16 0 12 89 28 77 384 -2 38 -5 201

-451 993 7 1 -17 16 0 12 89 28 77 384 -2 38 -5 201 -451 993 7 1 -17 16 0 12 89 28 77 384 -2 38 -5 201

-451 993 16 7 1 -17 0 12 89 28 77 384 -2 38 -5 201 -451 993 16 7 1 -17 0 12 89 28 77 384 -2 38 -5 201

-451 993 16 7 1 0 -17 12 89 28 77 384 -2 38 -5 201 -451 993 16 7 1 0 -17 12 89 28 77 384 -2 38 -5 201

-451 993 16 12 7 1 0 -17 89 28 77 384 -2 38 -5 201 -451 993 16 12 7 1 0 -17 89 28 77 384 -2 38 -5 201

-451 993 89 16 12 7 1 0 -17 28 77 384 -2 38 -5 201 -451 993 89 16 12 7 1 0 -17 28 77 384 -2 38 -5 201

-451 993 89 28 16 12 7 1 0 -17 77 384 -2 38 -5 201 -451 993 89 28 16 12 7 1 0 -17 77 384 -2 38 -5 201

-451 993 89 77 28 16 12 7 1 0 -17 384 -2 38 -5 201 -451 993 89 77 28 16 12 7 1 0 -17 384 -2 38 -5 201

-451 993 384 89 77 28 16 12 7 1 0 -17 -2 38 -5 201 -451 993 384 89 77 28 16 12 7 1 0 -17 -2 38 -5 201

-451 993 384 89 77 28 16 12 7 1 0 -2 -17 38 -5 201 -451 993 384 89 77 28 16 12 7 1 0 -2 -17 38 -5 201

-451 993 384 89 77 38 28 16 12 7 1 0 -2 -17 -5 201 -451 993 384 89 77 38 28 16 12 7 1 0 -2 -17 -5 201

-451 993 384 89 77 38 28 16 12 7 1 0 -2 -5 -17 201 -451 993 384 89 77 38 28 16 12 7 1 0 -2 -5 -17 201

-451 993 384 201 89 77 38 28 16 12 7 1 0 -2 -5 -17 -451 993 384 201 89 77 38 28 16 12 7 1 0 -2 -5 -17

total exchanges: 68交易所总数:68

#include <stdio.h>

#define N 16

int xchg();

int main() {
    int numbers[16] = {7, 1, 993, -5, 0, 16, -451, 12, 89, 28, 77, 384, -2, 38, -17, 201}; 
    int cntr, cntr2, cntr3;
    int chgNum;

    for(cntr = 0; cntr < N; cntr++){
        for(cntr2 = 1; cntr2 < N; cntr2++){
            chgNum += xchg(&numbers[cntr], &numbers[cntr2]);
        }
        for(cntr3 = 0; cntr3 < N; cntr3++){
            if(cntr3 == 15){
                printf("%d", numbers[cntr3]);
            }
            else {
                printf("%d ", numbers[cntr3]);
            } 
        }

        printf("\n");


    }
    printf("total exchanges: %d\n", chgNum);
    return 0;
}

int xchg(int *p1, int *p2) {
    int tmp = 0;
    if(*p2 < *p1){
        tmp = *p1;
        *p1 = *p2;
        *p2 = tmp;
        return 1;
    }
    else {
        return 0;
    }
}

You need to change your loop in main .您需要更改main的循环。

for(cntr2 = cntr+1; cntr2 < N; cntr2++){

You might also want to check if xchg is giving you the direction ascending/descending sort order or if you need to invert your exchange condition.您可能还想检查xchg是否为您提供升序/降序排序方向,或者您是否需要反转交换条件。

Also: you forgot to initialize chgNum to zero.另外:您忘记将chgNum初始化为零。

Ok the problem is that the inner for loop starts at 1, after the first iteration it will be wrong it needs to start at the position that the first loop is at, ie cntr2 = cntr.好的,问题是内部 for 循环从 1 开始,在第一次迭代后它会出错,它需要从第一个循环所在的位置开始,即 cntr2 = cntr。

A few other things:其他一些事情:

  • The forward declaration of the `xchng` function isn't correct. `xchng` 函数的前向声明不正确。
  • The if else print statement doesn't do anything it will print the same thing no matter what. if else 打印语句不执行任何操作,无论如何它都会打印相同的内容。
  • If you are incrementing a number using `++` operator and it isn't being assigned to anything, always use `++(int)`, it requires fewer operations.如果您使用 `++` 运算符递增一个数字并且它没有被分配给任何东西,请始终使用 `++(int)`,它需要更少的操作。
  • Finally, there are some nasty variable names, it doesn't hurt to be more descriptive.最后,还有一些令人讨厌的变量名称,更具描述性也无妨。 Also you define a macro which is the size of the array, but you don't define the array using it, int numbers[N] would make more sense as then at least you can see why the the macro is used in the loop.您还定义了一个宏,它是数组的大小,但您没有使用它定义数组, int numbers[N] 会更有意义,因为至少您可以看到为什么在循环中使用宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM