首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用选择排序对数组进行排序

使用选择排序对数组进行排序
EN

Stack Overflow用户
提问于 2020-05-10 10:36:02
回答 2查看 61关注 0票数 0
代码语言:javascript
复制
#include<stdio.h>
#include <time.h>

int selectionSort(int data[], int count)
{
  int a,b;
  int tmp;
  int minimum;
  for(a=0; a<count-1;a++){
  minimum = a;
    for(b=a+1;b<count;b++){
          if(data[minimum]>data[b]) {
            minimum = b;
            }
        }
        tmp = data[a];
        data[a]=data[minimum];
        data[minimum]=tmp;
}
  return 0;
}


int main(){
    int randomarray[10];
    int i;
    int k;
    srand(time(NULL));
    for(i=1; i<=10; i++){
    randomarray[i]=rand()%100;
    selectionSort(randomarray[i], k = 10 );
     printf("%d\n",randomarray[i]);

  }
return 0;

}

我正在尝试创建一个对随机整数数组进行排序的程序。我认为我的函数声明有问题,但我不习惯使用C。错误如下:

代码语言:javascript
复制
semihosting_example.c:13:5: warning: implicit declaration of function 'srand' [-Wimplicit-function-declaration]
semihosting_example.c:15:5: warning: implicit declaration of function 'rand' [-Wimplicit-function-declaration]
semihosting_example.c:16:5: warning: passing argument 1 of 'selectionSort' from incompatible pointer type [enabled by default]
semihosting_example.c:6:6: note: expected 'int *' but argument is of type 'int (*)[10]'ing_example.c:6:6: note: expected 'int *' but argument is of type 'int (*)[10]
EN

回答 2

Stack Overflow用户

发布于 2020-05-10 10:49:56

下面是更新后的代码,其中包含适当的标头。我正在尝试创建一个包含数组的函数,该函数调用包含数组的函数:

代码语言:javascript
复制
#include<stdio.h>
#include <time.h>
#include<stdlib.h>

int selectionSort(int (*)[10], int count)
{
  int a,b;
  int tmp;
  int minimum;
  for(a=0; a<count-1;a++){
  minimum = a;
    for(b=a+1;b<count;b++){
          if(data[minimum]>data[b]) {
            minimum = b;
            }
        }
        tmp = data[a];
        data[a]=data[minimum];
        data[minimum]=tmp;
}
  return 0;
}


int main(){
    int randomarray[10];
    int i;
    int k;
    srand(time(NULL));
    for(i=1; i<=10; i++){
    randomarray[i]=rand()%100;
    selectionSort(randomarray[i], k = 10 );
     printf("%d\n",randomarray[i]);

  }
return 0;

}
票数 0
EN

Stack Overflow用户

发布于 2020-05-10 10:50:49

这是你的程序的修订版:

代码语言:javascript
复制
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

static void selectionSort(int data[], int count)
{
    for (int a = 0; a < count - 1; a++)
    {
        int minimum = a;
        for (int b = a + 1; b < count; b++)
        {
            if (data[minimum] > data[b])
                minimum = b;
        }
        int tmp = data[a];
        data[a] = data[minimum];
        data[minimum] = tmp;
    }
}

int main(void)
{
    int randomarray[10];

    srand(time(NULL));
    for (int i = 0; i < 10; i++)
        randomarray[i] = rand() % 100;

    printf("Before:\n");
    for (int i = 0; i < 10; i++)
        printf("%d: %d\n", i, randomarray[i]);
    selectionSort(randomarray, 10);
    printf("After:\n");
    for (int i = 0; i < 10; i++)
        printf("%d: %d\n", i, randomarray[i]);
    return 0;
}

运行时,它会生成如下输出:

代码语言:javascript
复制
Before:
0: 73
1: 63
2: 73
3: 80
4: 28
5: 19
6: 63
7: 96
8: 82
9: 46
After:
0: 19
1: 28
2: 46
3: 63
4: 63
5: 73
6: 73
7: 80
8: 82
9: 96

请注意,它在排序前和排序后都会打印数组,这样您就可以判断排序是否正确。

代码不使用k;它尽可能晚地声明变量。

如果替换该行:

代码语言:javascript
复制
selectionSort(randomarray, 10);

通过以下方式:

代码语言:javascript
复制
selectionSort(&randomarray, 10);

您将看到以下错误消息:

代码语言:javascript
复制
$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes \
>     sort53.c -o sort53 
sort53.c: In function ‘main’:
sort53.c:32:19: error: passing argument 1 of ‘selectionSort’ from incompatible pointer type [-Werror=incompatible-pointer-types]
   32 |     selectionSort(&randomarray, 10);
      |                   ^~~~~~~~~~~~
      |                   |
      |                   int (*)[10]
sort53.c:5:31: note: expected ‘int *’ but argument is of type ‘int (*)[10]’
    5 | static void selectionSort(int data[], int count)
      |                           ~~~~^~~~~~
$

这来自于GCC 10.1.0,但它与问题中发布的错误消息非常相似。如果您将调用替换为:

代码语言:javascript
复制
selectionSort(randomarray[0], 10);

则错误消息为:

代码语言:javascript
复制
sort53.c: In function ‘main’:
sort53.c:32:30: error: passing argument 1 of ‘selectionSort’ makes pointer from integer without a cast [-Werror=int-conversion]
   32 |     selectionSort(randomarray[0], 10);
      |                   ~~~~~~~~~~~^~~
      |                              |
      |                              int
sort53.c:5:31: note: expected ‘int *’ but argument is of type ‘int’
    5 | static void selectionSort(int data[], int count)
      |                           ~~~~^~~~~~
cc1: all warnings being treated as errors
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61706464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档