C语言下使用快速排序qsort
来源:互联网 发布:矩阵分析引论详细答案 编辑:程序博客网 时间:2024/05/01 03:30
/**
*to read the data from the file ,and call the quick-sort methods to sort the data
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int x;
int y;
}Node;
int comp(const void *, const void *);
int main(int argc, char *argv[])
{
int n,
//*a,
i,j,k;
struct node * a;
FILE *file;
if((file =fopen("data","r"))==NULL ){
printf("can not open file./n");
exit(0);
}
fscanf(file,"%d",&n);
if(n < 0){
printf("no number./n");
exit(0);
}
//a = (int *)malloc(n * sizeof(int));
a = (struct node *)malloc(n * sizeof(struct node));
//fread(a,sizeof(int),n,file); this method is used to reand the binary file
for(i= 0 ; i < n; i++)
fscanf(file,"%d %d",&a[i].x,&a[i].y);
qsort(a,n,sizeof(Node),comp);
for(i = 0 ;i < n; i++)
printf("%3d%3d/n",a[i].x,a[i].y);
printf("/n");
fclose(file);
system("PAUSE");
return 0;
}
/*to sort the node by x ascendingly, if the x of both two nodes equals,than compare the y*/
int comp(const void *a, const void *b){
Node *aa = (Node *)a;
Node *bb = (Node *)b;
if(aa->x > bb ->x)
return 1;
else if(aa->x == bb->x)
return (aa->y - bb->y);
else
return -1;
//return (*(int *)a - *(int *)b); //to compare the int type ,and you can imply on
}
- C语言下使用快速排序qsort
- C语言qsort快速排序
- C语言stdlib.h下的qsort<快速排序>
- c语言知识点---qsort快速排序函数的使用
- C语言中用qsort()快速排序
- C语言中用qsort()快速排序
- 快速排序 qsort c语言代码
- qsort函数 C语言快速排序函数
- c语言字符串快速排序qsort()
- C语言快速排序函数qsort
- C中qsort快速排序使用
- C语言qsort排序
- C语言中的快速排序 qsort函数详解
- c语言调用库函数qsort()进行快速排序
- 如何利用C语言中的qsort库函数实现快速排序?
- 快速排序 C语言的qsort 以及 C++的 sort
- c语言调用库函数qsort()进行快速排序
- 快速排序 C语言的qsort及C++的sort
- 软件加密锁原理
- HTML Table的几个样式
- 再谈oracle服务器内存(SGA+PGA+OS)设置
- sqlserver一些入门的知识
- 关于oracle sga设置的总结
- C语言下使用快速排序qsort
- 通过对oracle内存(SGA和PGA)进行调整,优化数据库性能
- 设计模式在EJB中的应用
- 学习《Windows驱动开发技术详解》(第三章)
- 想换工作了,但是心里没底!
- Oracle9i的物理内存管理
- extern "C" 的用意
- 数值分析
- MSDN 订阅地址