[英]Quicksort array of strings gives error in C
我是这个网站的新手,也是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[0] == "2367"
, sip[1] == "2368"
等。 std[0] == "2011-11-15 15:00"
, std[1] == "2011-11-15 17:15"
等。 etd[0] == "2011-11-15 20:55"
, etd[1] == "2011-11-16 01:50"
等。 因此,我正在尝试对字符串数组使用Quicksort算法,以便根据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);
}
}
但是,出了点问题。 你能帮我找到它吗?
非常感谢!
实际上,您不需要将文件分成三个阵列。 由于您按第一个数字排序,因此只要第一部分的值始终包含四个字符,结果将是正确的。 如果没有,则您的代码将给出错误,因为strcpy假定目标已分配了足够的空间。
C标准库中有一个qsort()
,您可以使用它对记录进行排序。
这是一个例子:
#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;
}
通过引用您的源代码,我看到传递给以下函数的参数类型与其预期的参数列表之间存在不匹配:
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);
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.