Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符串的快速排序数组在C中出现错误_C_Arrays_String_Sorting_Quicksort - Fatal编程技术网

字符串的快速排序数组在C中出现错误

字符串的快速排序数组在C中出现错误,c,arrays,string,sorting,quicksort,C,Arrays,String,Sorting,Quicksort,我是这个网站的新手,对C也是新手。在过去的两天里,我正在学习排序算法,并决定测试将整数快速排序转换为字符串数组快速排序的技能。基本上,我有这个文本文件: 2367 2011-11-15 15:00 2011-11-15 20:55 2368 2011-11-15 17:15 2011-11-16 01:50 2369 2011-11-15 20:00 2011-11-16 05:55 2370 2011-11-15 20:00 2011-11-16 05:50 2371 2011-11-15 2

我是这个网站的新手,对C也是新手。在过去的两天里,我正在学习排序算法,并决定测试将整数快速排序转换为字符串数组快速排序的技能。基本上,我有这个文本文件:

2367 2011-11-15 15:00 2011-11-15 20:55
2368 2011-11-15 17:15 2011-11-16 01:50
2369 2011-11-15 20:00 2011-11-16 05:55
2370 2011-11-15 20:00 2011-11-16 05:50
2371 2011-11-15 22:50 2011-11-16 03:45
2372 2011-11-12 17:00 2011-11-12 19:20
2373 2011-11-13 13:55 2011-11-13 21:35
2374 2011-11-14 03:40 2011-11-14 06:15
这个文件有3个部分,我已经存储在3个字符串数组中

  • sip数组包含
    sip[0]==“2367”
    sip[1]==“2368”
  • std数组包含第一个日期和时间,
    std[0]==“2011-11-15:00”
    std[1]==“2011-11-15 17:15”
  • 最后,etd数组包含第二个日期和时间,
    etd[0]==“2011-11-15 20:55”
    etd[1]==“2011-11-16 01:50”
因此,我尝试使用字符串数组的快速排序算法,根据std数组对上述文本文件进行排序,因此预期结果将是:

2372 2011-11-12 17:00 2011-11-12 19:20  (moved first)
2373 2011-11-13 13:55 2011-11-13 21:35  (moved second)
2374 2011-11-14 03:40 2011-11-14 06:15  (moved third)
2367 2011-11-15 15:00 2011-11-15 20:55
2368 2011-11-15 17:15 2011-11-16 01:50
2369 2011-11-15 20:00 2011-11-16 05:55
2370 2011-11-15 20:00 2011-11-16 05:50
2371 2011-11-15 22:50 2011-11-16 03:45
这是我目前的代码:

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

void quickSortMain(char items[][17], char etd[][17], char SP[][5], int count);
void quickSort(char items[][17], char etd[][17], char SP[][5], int left, int right);

int main () {
.

.

quickSortMain(std, etd, sip, count);
// std and etd have 16 characters, so std[][17] and etd[][17]
// sip has 4 characters, so sip[][5]

.

.

}

void quickSort(char items[][17], char etd[][17], char SP[][5], int left, int right)
{
  int i, j;
  char *x;
  char temp[10];

  i = left;
  j = right;
  x = items[(left+right)/2];

  do {
    while((strcmp(items[i],x) < 0) && (i < right)) {
       i++;
    }
    while((strcmp(items[j],x) > 0) && (j > left)) {
        j--;
    }
    if(i <= j) {
      strcpy(temp, items[i]);
      strcpy(items[i], items[j]);
      strcpy(items[j], temp);

      strcpy(temp1, etd[i]);
      strcpy(etd[i], etd[j]);
      strcpy(etd[j], temp1);

      strcpy(temp2, SP[i]);
      strcpy(SP[i], SP[j]);
      strcpy(SP[j], temp2);

      i++;
      j--;
   }
  } while(i <= j);

  if(left < j) {
     quickSort(items, etd, SP, left, j);
  }
  if(i < right) {
     quickSort(items, etd, SP, i, right);
  }
}
#包括
#包括
#包括
void quickSortMain(字符项[][17]、字符etd[][17]、字符SP[][5]、整数计数);
无效快速排序(字符项[][17]、字符etd[][17]、字符SP[][5]、左整数、右整数);
int main(){
.
.
quickSortMain(标准、etd、sip、计数);
//std和etd有16个字符,因此std[][17]和etd[][17]
//sip有4个字符,因此sip[][5]
.
.
}
无效快速排序(字符项[][17]、字符etd[][17]、字符SP[][5]、左整数、右整数)
{
int i,j;
char*x;
煤焦温度[10];
i=左;
j=右;
x=项目[(左+右)/2];
做{
而((strcmp(项目[i],x)<0)和(i<右)){
i++;
}
而((strcmp(项目[j],x)>0)和&(j>左)){
j--;
}

如果(i实际上,您不需要将文件拆分为三个数组。由于您按第一个数字进行排序,如果第一部分的值始终包含四个字符,那么结果将是正确的。如果它们不包含,那么您的代码将给出错误,因为strcpy假定目标已分配足够的空间。

有一个
qsort()
在C标准库中,您可以使用它对记录进行排序

以下是一个例子:

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

int
record_cmp (const void *pa, const void *pb)
{
    const char *a = (const char *)pa + 5;
    const char *b = (const char *)pb + 5;
    return strncmp(a, b, 16);
}

int
main(void) {
    char record[][40] = {
        "2367 2011-11-15 15:00 2011-11-15 20:55",
        "2368 2011-11-15 17:15 2011-11-16 01:50",
        "2369 2011-11-15 20:00 2011-11-16 05:55",
        "2370 2011-11-15 20:00 2011-11-16 05:50",
        "2371 2011-11-15 22:50 2011-11-16 03:45",
        "2372 2011-11-12 17:00 2011-11-12 19:20",
        "2373 2011-11-13 13:55 2011-11-13 21:35",
        "2374 2011-11-14 03:40 2011-11-14 06:15",
    };
    size_t ele_num = sizeof(record)/sizeof(record[0]);

    qsort(record, ele_num, sizeof(record[0]), record_cmp);

    for (size_t i = 0; i < ele_num; i++) {
        printf("record[%zu] = %s\n", i, record[i]);
    }

    return 0;
}
#包括
#包括
#包括
int
记录(常数无效*pa,常数无效*pb)
{
常量字符*a=(常量字符*)pa+5;
常量字符*b=(常量字符*)pb+5;
返回strncmp(a,b,16);
}
int
主(空){
字符记录[][40]={
"2367 2011-11-15 15:00 2011-11-15 20:55",
"2368 2011-11-15 17:15 2011-11-16 01:50",
"2369 2011-11-15 20:00 2011-11-16 05:55",
"2370 2011-11-15 20:00 2011-11-16 05:50",
"2371 2011-11-15 22:50 2011-11-16 03:45",
"2372 2011-11-12 17:00 2011-11-12 19:20",
"2373 2011-11-13 13:55 2011-11-13 21:35",
"2374 2011-11-14 03:40 2011-11-14 06:15",
};
size_t ele_num=sizeof(记录)/sizeof(记录[0]);
qsort(记录、元素、大小(记录[0])、记录\u cmp);
对于(大小i=0;i
参考您的源代码,我发现传递给以下函数的参数类型与其预期的参数列表不匹配:

 quickSort(items, 0, etd, SP, count-1);
 void quickSort(char items[][17], char etd[][17], char SP[][5], int left, int right)
您应该按以下方式调用该函数:

 quickSort(items, etd, SP, 0, count-1);

谢谢!

使用
qsort()
代替:)@BlueMoon:我不认为
qsort()当数据在3个不同的数组中展开时,将工作。此外,它不能保证实现QuasRead算法。@用户考虑将数据放在一个数组中,然后对单个数组进行排序,然后,如果需要的话,将原来的单个数组分成3个不同的数组。非常感谢这些答案,但我不想基于四个第一个字母,但其中一个是第一个日期时间,检查预期结果谢谢回答,但我想按日期时间排序,检查预期结果..原始问题将数据拆分为3个不同的数组。@pmg没有必要这样做来排序这些数据。如果他出于其他目的拆分数据,他仍然可以这样做在它们被排序之后。还有
qsort()
不一定实现快速排序算法。@pmg是的,你是对的。你可以用它来排序一些数据,但不能用它来学习快速排序算法的工作原理。如果询问者的意图是学习快速排序算法的工作原理,那么这是一个不相关的答案。感谢你注意到,我更改了它,但仍然不起作用,出于某种原因,它给出了segfault(更新了我最初的帖子)。