简体   繁体   English

使用带有定义结构的qsort

[英]Using qsort with a define struct

I'm learning C and I'm solving this challenge , I'm not planning to submit it to the uva platform and the reason I'm coding this exercise is for leaning, maybe is not the best approach to the problem but I'm trying. 我正在学习C并且正在解决此挑战 ,我不打算将其提交给uva平台,我编写此练习的原因是为了倾斜,也许不是解决问题的最佳方法,但是我我正在尝试。

The input I print in my terminal is the following: 我在终端中打印的输入如下:

4
3
20 30 40 50 30 40
Res: 2
4 
20 30 10 10 30 20 40 50
Res: 4
3
10 30 20 20 30 10
Res: 2
4
10 10 20 30 40 50 39 51
Res: 3

The answers from each input test are incorrect and I believe the reason is the qsort function. 每个输入测试的答案都不正确,我相信原因是qsort函数。 I'm confuse about how to use the qsort function using a structure, I'm calling my structure that is called array, followed by the size of my input, then using sizeof(int) but do I need to use int or sizeof my structure, lastly I'm calling my compare function. 我对如何使用结构使用qsort函数感到困惑,我在调用称为数组的结构,然后是输入的大小,然后使用sizeof(int),但是我是否需要使用int或sizeof结构,最后我叫我的比较功能。 My code is: 我的代码是:

#include <stdio.h>
#include <string.h>

struct Dolls{
  int w;
  int h;
}array[20005];

int cmp(struct Dolls a, struct Dolls b){
  if(a.w==b.w){
    return a.h < b.h;
  }else{
    return a.w > b.w;
  }
}

int arr[20005];
int dp[20005];
int n;

int bSearch(int num, int k){
  int low=1;
  int high = k;
  int mid;
  while(low<= high){
    mid = (low+high)/2;
    if(num>=dp[mid]){
      low=mid+1;
    }else{
      high=mid-1;
    }
  }
  return low;
}

int res_dolls(){
  int k=1;
  int i,pos;
  dp[i]=arr[1];
  for(i=2;i<=n;i++){
    if(arr[i]>=dp[k]){
      dp[++k] = arr[i];
    }else{
      pos = bSearch(arr[i],k);
      dp[pos] = arr[i];
    }
  }
  return k;
}

int main(){
  int t,j;
  scanf("%d",&t);
  while(t--){
    memset(array,0,sizeof(array));
    scanf("%d",&n);
    for(j=1;j<=n;j++){
      scanf("%d %d",&array[j].w, &array[j].h);
    }
    qsort(array,n,sizeof(int),cmp);
    for(j=1;j<=n;j++){
      arr[j] = array[j].h;
    }
    printf("%d\n",res_dolls());
  }
  return 0;
}

Your cmp function needs to be defined as int (*)(const void *, const void *) for it to work for qsort . 您的cmp函数需要定义为int (*)(const void *, const void *)才能用于qsort

The way you're performing the comparisons is also not correct. 您执行比较的方式也不正确。 From the man page for qsort: 在qsort的手册页中:

The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respec- tively less than, equal to, or greater than the second. 如果认为第一个参数分别小于,等于或大于第二个参数,则比较函数必须返回小于,等于或大于零的整数。 If two members compare as equal, their order in the sorted array is undefined. 如果两个成员比较相等,则它们在排序数组中的顺序是不确定的。

The comparisons you're doing return the result of the < or > operators, which is either 0 or 1. You need to explicitly check each case and return the proper value. 您进行的比较将返回<>运算符的结果,该结果为0或1。您需要显式检查每种情况并返回正确的值。

int cmp(const void *va, const void *vb){
  const struct Dolls *a = va;
  const struct Dolls *b = vb;

  if(a->w > b->w) {
      return 1;
  } else if(a->w < b->w){
      return -1;
  } else if(a->h > b->h) {
      return 1;
  } else if(a->h < b->h){
      return -1;
  } else {
    return 0;
  }
}

As for the call to qsort , you need to give it the size of an array element, ie the whole struct, not the size of the subfield: 至于对qsort的调用,您需要给它一个数组元素的大小,即整个结构的大小,而不是子字段的大小:

qsort(array,n,sizeof(struct Dolls),cmp);

EDIT: 编辑:

Fixed the error with the parameter names. 修复了参数名称错误。 Also changed how the sort is performed to conform to how the comparison function must behave. 还更改了执行排序的方式,以符合比较功能的行为方式。

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

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