qsort排序失败

狂日一条街 2012-05-26 04:26:50
这是我的代码这是我的测试结果各位大神求解释为什么会排序失败按道理来说不是升序就是降序偏偏这么奇葩
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int comp(const void *a,const void *b)
{
int c;
return c=((string *)a<=(string *)b?1:-1);
}
int main()
{
int a[7];
for(int x=0;x<7;x++)
cin>>a[x];
qsort(a,7,sizeof(int),comp);
qsort(a,7,sizeof(int),comp);

for(int x=0;x<7;x++)
cout<<a[x]<<" ";
return 0;

}
输入:7 6 5 4 3 2 1

输出:5 4 3 2 1 7 6 请按任意键继续. . .
...全文
193 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tongzhipeng5699 2012-05-27
  • 打赏
  • 举报
回复
++

#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
int comp(const void *a,const void *b)
{
return *((int*)a)>=*((int*)b);
}
int main()
{
int a[7];
for(int x=0;x<7;x++)
cin>>a[x];
qsort(a,7,sizeof(int),comp);
qsort(a,7,sizeof(int),comp);

for(int x=0;x<7;x++)
cout<<a[x]<<" ";
return 0;

}



[Quote=引用 1 楼 的回复:]
return c=((string *)a<=(string *)b?1:-1); ????转换成int吧
[/Quote]
qq120848369 2012-05-27
  • 打赏
  • 举报
回复
       For one example of use, see the example under bsearch(3).

Another example is the following program, which sorts the strings given in its command-line arguments:

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

static int
cmpstringp(const void *p1, const void *p2)
{
/* The actual arguments to this function are "pointers to
pointers to char", but strcmp(3) arguments are "pointers
to char", hence the following cast plus dereference */

return strcmp(* (char * const *) p1, * (char * const *) p2);
}

int
main(int argc, char *argv[])
{
int j;

if (argc < 2) {
fprintf(stderr, "Usage: %s <string>...\n", argv[0]);
exit(EXIT_FAILURE);
}

qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp);

for (j = 1; j < argc; j++)
puts(argv[j]);
exit(EXIT_SUCCESS);
}



这是个例子,楼主在==的时候应该返回0,但你没有。
qq120848369 2012-05-27
  • 打赏
  • 举报
回复
请使用<,而不是<=或者>=。

qsort的实现认为用户提供的是<号,而不是<=,如果你提供的不一致,必将导致排序结果混乱。
okili 2012-05-26
  • 打赏
  • 举报
回复
还是不行啊
ningto.com 2012-05-26
  • 打赏
  • 举报
回复
return c=((string *)a<=(string *)b?1:-1); ????转换成int吧
《数据结构》 课程设计报告 专 业 计算机科学与技术 班 级 姓 名 学 号 指导教师 起止时间 2012.12~2013.1 课程设计:排序综合 任务描述 排序综合 任务:利用随机函数产生n个随机整数(20000以上),对这些数进行多种方法进行排序 。 要求: (1)至少采用三种方法实现上述问题求解(提示,可采用的方法有插入排序、希尔排序 、起泡排序、快速排序、选择排序、堆排序、归并排序)。并把排序后的结果保存在不 同的文件中。 (2)统计每一种排序方法的性能(以上机运行程序所花费的时间为准进行对比),找出 其中两种较快的方法。 二、问题分析 1、功能分析 分析设计课题的要求,要求编程实现以下功能: (1)排序表的建立—即创建顺序表; (2)顺序表的排序—即直接插入排序、希尔排序、起泡排序、快速排序、简单选择排 序操作; (3)统计每一种排序方法的性能—即测试上机运行程序所花费的时间; 2、数据对象分析 数据信息:随机整数 数据数量可以预先确定(20000以上) 数据结构设计 使用顺序表实现,有关定义如下: typedef int Status; typedef int KeyType ; //设排序码为整型量 typedef int InfoType; typedef struct { //定义被排序记录结构类型 KeyType key ; //排序码 I nfoType otherinfo; //其它数据项 } RedType ; typedef struct { RedType * r; //存储带排序记录的顺序表 //r[0]作哨兵或缓冲区 int length ; //顺序表的长度 } SqList ; //定义顺序表类型 四、功能设计 (一)主控菜单设计 为实现通各种排序的功能,首先设计一个含有多个菜单项的主控菜单程序,然后再为 这些菜单项配上相应的功能。 程序运行后,给出6个菜单项的内容和输入提示,如下: 1. 直接插入排序 2. 希尔排序 3. 起泡排序 4. 快速排序 5. 简单选择排序 0. 退出系统 (二)程序模块结构 由课题要求可将程序划分为以下几个模块(即实现程序功能所需的函数): 主控菜单项选择函数menu() 创建排序表函数 InitList_Sq() 对顺序表L进行直接插入排序函数Insertsort() 希尔排序函数ShellSort(); 起泡排序函数Bubble_sort() 快速排序函数QSort ( ) 简单选择排序函数SelectSort() (三)函数调用关系 程序的主要结构(函数调用关系)如下图所示。 Menu() InitList_Sq(L) Main() Insertsort(L) ShellSort() Bubble_sort() QSort ( ) SelectSort() 其中main()是主函数,它进行菜单驱动,根据选择项0~5调用相应的函数。main()函数使 用for循环实现重复选择。其循环结构如下: for (;;) { long start,end; switch(menu()) { case 1: printf("* 直接插入排序 *\n"); start=clock(); Insertsort(L); end=clock(); printf("%d ms\n",end-start); fp=fopen("D:直接插入排序.txt","w"); if(fp==NULL)//打开文件失败 { printf("打开文件失败!\n"); exit(1); } for(i=1;i<=L.length;i++) fprintf(fp,"%d ",L.r[i]); fclose(fp); break; case 2: printf("* 希尔排序 *\n"); start=clock(); ShellSort(L,an,14); end=clock(); printf("%d ms\n",end-start); fp=fopen("D:希尔排序 .txt","w"); if(fp==NULL)//打开文件失败 { printf("打开文件失败!\n"); exit(1); } for(i=1;i<=L.length;i++) fprintf(fp,"%d ",L.r[i]); fclose(fp); break; case 3: printf("* 起泡排序 *\n"); start=clock(); Bubble_sort(L); end=clock(); printf("%d ms\n",end-start); fp=fopen("D: 起泡排序 .txt","w"); if(fp==NULL)//打开文件失败 { printf("打开文件失败!\n"); exi

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧