随机生成100万个数,排序后保存在文件中
来源:互联网 发布:软件下载万一网 编辑:程序博客网 时间:2024/05/22 03:37
随机生成100万个数,存储在文件out1.txt中,使用内部排序完成,并重新储存在文件out2.txt中。
(一)使用STL中的qsort函数进行操作:
#include "stdio.h"#include "string.h"#include "stdlib.h"#include "time.h"int a[1000000];void load(char filename[]) //写文件{int i;FILE *fp;fp=fopen(filename,"w");if(fp==NULL){printf("cannot open file/n");return;}for(i=0;i<1000000;i++) fprintf(fp,"%d ",a[i]); }int cmp(const void *a, const void *b){return (*(int*)a)-(*(int*)b); //从小到大进行排序}void paixu(){qsort(a,1000000,sizeof(int),cmp);}int main(void){int i;char filename[20];srand( (unsigned)time( NULL ) ); //初始化随机数for(i=0;i<1000000;i++) //打印出10个随机数a[i]=rand();strcpy(filename,"out1.txt");load(filename);paixu();//快速排序strcpy(filename,"out2.txt");load(filename);system("pause");return 0;}
(二)递归实现qsort函数进行操作:
#include "string.h"#include "stdlib.h"#include "stdio.h"#include "time.h"int a[1000000];void load(char filename[]) //写文件{int i;FILE *fp;fp=fopen(filename,"w");if(fp==NULL){printf("cannot open file/n");return;}for(i=0;i<1000000;i++) fprintf(fp,"%d ",a[i]); }int partitions(int a[],int low,int high) {int pivotkey=a[low]; //基准while(low<high){while(low<high && a[high]>=pivotkey)--high;a[low]=a[high];while(low<high && a[low]<=pivotkey)++low;a[high]=a[low];}a[low]=pivotkey;return low;} void qsort(int a[],int low,int high) //快速排序{int pivotkey;if(low<high){//递归调用pivotkey=partitions(a,low,high);qsort(a,low,pivotkey-1);qsort(a,pivotkey+1,high);}}int main(void){int i;char filename[20];srand( (unsigned)time( NULL ) ); //初始化随机数for(i=0;i<1000000;i++) //打印出10个随机数a[i]=rand();strcpy(filename,"out1.txt");load(filename);qsort(a,0,1000000); //快速排序strcpy(filename,"out2.txt");load(filename);system("pause");return 0;}
- 随机生成100万个数,排序后保存在文件中
- 随机生成100万个数,排序后保存在文件中
- 随机生成100万个数,排序后保存在文件中
- 随机生成100万个数,排序后保存在文件中
- C++: 生成100万随机数, 排序后保存到文件中
- 在1-10000中随机生成100个数
- 随机生成100个数并排序
- 在存有10亿个数的文件中找出100万个数
- 在存有10亿个数的文件中找到最大的100万个数
- 在1-10中随机生成5个数不重复。
- [C/C++]随机生成信息,并保存到文件中
- VB随机生成10个数并排序
- 随机生成1000个数并排序
- 逆向随机生成100个数
- 冒泡排序(随机生成10个数排序)
- java经典算法_025对随机生成的100以内的10个数进行排序
- 随机生成N个数,并从大到小排序。。。。。。。
- 随机生成一个三维数组,编程求深度的平均值,保存在一个二维数组中
- hdu1969之二分查找
- C语言通讯录管理系统
- 黑马程序员---JAVA基础3
- 程序员求职之道(《程序员面试笔试宝典》)之学业与求职,孰轻孰重?
- jpa 多表查询,转载留底学习
- 随机生成100万个数,排序后保存在文件中
- 黑马程序员---JAVA基础4
- Nagios学习
- 微博营销中的 KOL 分析
- sgu 222 - Little Rooks 简单的状态压缩DP
- 遍历二叉树的各种操作(非递归遍历)
- hdu 3342 Legal or Not ( 拓扑排序 )
- 黑马程序员——规划程序员的人生
- 黑马程序员---继承1