【问题标题】:Sort arrays with pointers using quicksort使用快速排序对带有指针的数组进行排序
【发布时间】:2020-01-08 20:04:55
【问题描述】:

我正在尝试使用快速排序对数组“选项卡”中的值进行排序,但它不起作用。主函数只是为每个选项卡设置名称和薪水[n]

typedef struct employee Employee;

struct employee{
  char name[81];
  float salary;
};

Employee *tab[10];

void sort(Employee **tab, int begin, int end){
  int p = tab[(begin + end) / 2] , i = end, j = begin;  
  /*p is the pivot*/
  do{
    while(tab[i]->salary < p && i < end) i++; 
    while(tab[j]->salary > p && j > begin) j--; 

    if(i <= j){
      int tmp = tab[i]->salary;
      tab[i]->salary = tab[j]->salary;
      tab[j]->salary = tmp;
      i++; j--;
    }
  }while(i <= j);

  if(begin < j) sort(tab, begin, j);
  if(end > i) sort(tab, i, end);
}

【问题讨论】:

  • 您的支点pEmployee*,而不是int
  • 它在什么方面不起作用?它在什么大小的数组上失败(0、1、2、3、4,更多元素)?它失败的样本数据是什么? Employee 结构是什么样的?为什么你只交换薪水而不是整个员工记录?名字在前的人薪水最低,名字在后的人薪水最高?反之亦然?
  • 我可能错了,但不应该是 float p = tab[(begin + end) / 2] ->salary 吗?在接下来的 2 个 while 循环中,您将浮点数 (tab[i]->salary 和 tab[j]->salary) 与 int (p) 进行比较,而 int (p)(正如 mangusta 指出的那样)又等同于结构元素指针,你应该交换第i和第j个结构元素,而不仅仅是薪水。
  • 你得到了我的支持@JL2210,但我不确定从长远来看我们会赢。大约 5 年后的某个时候,我可能会停止使用该版本(这是我复制粘贴的预设序列,而不是我在需要时从头开始输入的内容)。
  • @JL2210:我将它们保存在 GitHub 上我的 SOQ 存储库中 - 在 src/scripts 目录中。这个是sscce。我主要在 Mac 上工作,所以 sscce | sed 5q | pbcopy 在粘贴缓冲区中为我提供了上面引用的文本,然后 command-V (⌘-V) 将其放入评论中。第 6 行以上的其他链接是:另见 How to s the Smart WayWriting the perfect question

标签: c pointers quicksort


【解决方案1】:

在 cmets 中记录的更改。这是降序排序(在后续问题中提出)。

#include <stdio.h>

typedef struct employee{
    char name[81];
    float salary;
}Employee;

void sort(Employee **tab, int begin, int end){
    float p = tab[(begin + end) / 2]->salary; /* float needed for compare == */
    int i = begin, j = end;
    Employee *tmp;                          /* microsoft is c89 */

    while(i <= j){                      /* using while */
        while(tab[i]->salary > p) i++;  /* >, <= pivot stops scan */
        while(tab[j]->salary < p) j--;  /* <, >= pivot stops scan */
        if(i > j)                       /* using break */
            break;
        tmp = tab[i];
        tab[i] = tab[j];
        tab[j] = tmp;
        i++; j--;
    }

    if(begin < j) sort(tab, begin, j);
    if(end > i) sort(tab, i, end);
}

int main(int argc, char**argv)
{
    Employee tab[] = {{"john", 525.}, {"jack", 520.},
                      {"mary", 537.}, {"jane", 523.},
                      {"joan", 548.}, {"sam",  524.},
                      {"lisa", 527.}, {"ann",  541.},
                      {"tom",  521.}, {"ted",  531.}};
    Employee *ptr[sizeof(tab)/sizeof(tab[0])];
    int i;
    /* create array of pointers */
    for(i = 0; i < (sizeof(tab)/sizeof(tab[0])); i++)
        ptr[i] = &tab[i];
    sort(ptr, 0, sizeof(ptr)/sizeof(ptr[0])-1);
    for(i = 0; i < (sizeof(ptr)/sizeof(ptr[0])); i++)
        printf("%5s %6.2f\n", ptr[i]->name, ptr[i]->salary);
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-05
    • 2014-05-16
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多