更多资料请点击:我的目录
本篇仅用于记录自己所学知识及应用,代码仍可优化,仅供参考,如果发现有错误的地方,尽管留言于我,谢谢。
#include <stdio.h>#include <stdlib.h>#include <time.h>#include <unistd.h>#define N 1000000void shell(int len , int data[]);//希尔排序int main(){FILE *src = fopen("numbers.txt","r");//已有数据文本文档FILE *dst = fopen("sort_num.txt","w+");//空文本文档int temp[N] = {0};int n = 0;for(int i = 0; i < N; i++){fscanf(src , "%d", &temp[i]);//将文本文档数据写入数组中n++;}fclose(src);printf("读取完毕!读取次数 %d次。\n",n);shell(N,temp);//希尔排序for(int i = 0; i < N; i++){//printf("data[%d] ==%d \n",i ,temp[i]);//排序后数据输出fprintf(dst ,"%d\n", temp[i]);//将排序后数据写入新的文本文档}fclose(dst);return 0;}void insert_sort(int data[], int len, int space)//希尔排序{if(len <= 1)return;for(int i = space; i < len*space; i += space){int j, tmp = data[i];for(j = i-space; j >= 0; j -= space){if(data[j] < tmp)break;data[j+space] = data[j];}data[j+space] = tmp;}}void shell(int len , int data[])//希尔排序{printf("\n正在排序...\n");if(len <= 1)return;for(int space = len/2; space > 0; space /= 2){for(int i = 0; i < space; ++i){insert_sort(data+i, len/space, space);//递归循环}}}
更多资料请点击:我的目录