随机生成100万个数,排序后保存在文件中

随机生成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; }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会按照您的要求来实现。首先,让我们来生成排序序列并保存文件: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> // 生成指定长度的随机序列并保存文件 void generateRandomSequence(int length, const char* filename) { FILE* file = fopen(filename, "w"); if (file == NULL) { printf("无法创建文件 %s\n", filename); return; } srand(time(NULL)); for (int i = 0; i < length; i++) { int num = rand(); fprintf(file, "%d\n", num); } fclose(file); } int main() { int lengths[] = { 10000, 100000, 1000000 }; int num_lengths = sizeof(lengths) / sizeof(lengths[0]); for (int i = 0; i < num_lengths; i++) { char filename[20]; sprintf(filename, "sort in%d.txt", lengths[i]); generateRandomSequence(lengths[i], filename); } return 0; } ``` 上述代码会生成三个文件,分别是`sort in10000.txt`、`sort in100000.txt`和`sort in1000000.txt`,每个文件包含了对应长度的随机序列。 接下来,我们使用快速排序算法对这些序列进行排序,并记录每个排序花费的时间: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> // 快速排序算法 void quickSort(int arr[], int low, int high) { if (low < high) { int i = low, j = high, pivot = arr[low]; while (i < j) { while (i < j && arr[j] >= pivot) j--; if (i < j) arr[i++] = arr[j]; while (i < j && arr[i] <= pivot) i++; if (i < j) arr[j--] = arr[i]; } arr[i] = pivot; quickSort(arr, low, i - 1); quickSort(arr, i + 1, high); } } int main() { int lengths[] = { 10000, 100000, 1000000 }; int num_lengths = sizeof(lengths) / sizeof(lengths[0]); for (int i = 0; i < num_lengths; i++) { char filename[20]; sprintf(filename, "sort in%d.txt", lengths[i]); FILE* file = fopen(filename, "r"); if (file == NULL) { printf("无法打开文件 %s\n", filename); continue; } int* arr = malloc(lengths[i] * sizeof(int)); for (int j = 0; j < lengths[i]; j++) { fscanf(file, "%d", &arr[j]); } fclose(file); clock_t start_time = clock(); quickSort(arr, 0, lengths[i] - 1); clock_t end_time = clock(); double execution_time = (double)(end_time - start_time) / CLOCKS_PER_SEC; printf("长度为 %d 的序列排序花费时间:%f 秒\n", lengths[i], execution_time); free(arr); } return 0; } ``` 上述代码会读取每个文件的序列,使用快速排序算法进行排序,并记录排序所花费的时间。 最后,您可以根据需要进行调整和优化这些代码。希望能对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值