更多資料請點擊:我的目錄
本篇僅用於記錄自己所學知識及應用,代碼仍可優化,僅供參考,如果發現有錯誤的地方,儘管留言於我,謝謝。
文末有運行結果。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define N 1000000
void shell(int len , int data[]);//希爾排序
int main()
{
srand(time(NULL));//隨機數
FILE *src = fopen("numbers.txt","w+");//讀寫方式打開
setvbuf(src, NULL, _IONBF, 0);//設置緩衝模式爲無緩衝
for(int i = 0; i < N; i++)//循環一百萬遍
{
fprintf(src ,"%d\n", rand());//寫入隨機數
}
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);
src = fopen("numbers.txt","r");//已有數據文本文檔(未排序),只讀方式打開
FILE *dst = fopen("sort_num.txt","w+");//以讀寫方式打開空文本
n = 0;
for(int i = 0; i < N; i++)//循環一百萬遍
{
fscanf(src , "%d", &temp[i]);//將文本數據寫入數組中(已排序)
n++;
}
fclose(src);
printf("讀取完畢!讀取次數 %d次。\n",n);
shell(N,temp);//希爾排序
n = 0;
for(int i = 0; i < N; i++)//循環一百萬遍
{
printf("data[%d] ==%d \n",i ,temp[i]);//排序後數據輸出
fprintf(dst ,"%d\n", temp[i]);//將排序後數據寫入新的文本
n++;
}
fclose(dst);
printf("寫入完畢!寫入次數 %d次。\n",n);
dst = fopen("sort_num.txt","r");//已有數據文本文檔(已排序),只讀方式打開
for(int i = 0; i < N; i++)//循環一百萬遍
{
fscanf(dst , "%d", &temp[i]);//將所有數據存入到數組中
n++;
}
fclose(dst);
printf("讀取完畢!讀取次數 %d次。\n",n);
n = 0;
for(int j = 0; j < 10; j++)
{
char file[10] = {0};//數組用於存放文件的名稱
sprintf(file , "%d_txt", j+1);//將字符寫入file
FILE *dst = fopen(file,"w+");//打開file
for(int i = n; i < N; i++)//循環一百萬遍
{
fprintf(dst ,"%d\n", temp[i]);//寫入數據
n++;
if((n/100000) == j+1)//每寫入十萬次就跳出此循環
{
printf("寫入第%2d個文件中...共寫入%d個數據。\n\n",j+1,n);
break;
}
}
fclose(dst);//關閉當前文件
}
printf("寫入完畢!\n");
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);//遞歸循環
}
}
}
後面道理也一樣,不一一列出。
在程序目錄終端下輸入測試命令:
sed -n '1,2p' sort_num.txt
sed -n '1,2p' 1_txt
sed -n '99999,100002p' sort_num.txt
tail -n +99999 1_txt
sed -n '1,2p' 2_txt
sed -n '199999,200002p' sort_num.txt
tail -n +99999 2_txt
sed -n '1,2p' 3_txt
sed -n '299999,300002p' sort_num.txt
tail -n +99999 3_txt
sed -n '1,2p' 4_txt
sed -n '399999,400002p' sort_num.txt
tail -n +99999 4_txt
sed -n '1,2p' 5_txt
sed -n '499999,500002p' sort_num.txt
tail -n +99999 5_txt
sed -n '1,2p' 6_txt
sed -n '599999,600002p' sort_num.txt
tail -n +99999 6_txt
sed -n '1,2p' 7_txt
sed -n '699999,700002p' sort_num.txt
tail -n +99999 7_txt
sed -n '1,2p' 8_txt
sed -n '799999,800002p' sort_num.txt
tail -n +99999 8_txt
sed -n '1,2p' 9_txt
sed -n '899999,900002p' sort_num.txt
tail -n +99999 9_txt
sed -n '1,2p' 10_txt
tail -n +999999 sort_num.txt
tail -n +99999 10_txt
以下將上面完整代碼分開三個部分:
生成N個隨機數組並存入txt文本文件
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define N 1000000//一百萬個隨機數組
int main()
{
srand(time(NULL));//隨機數
FILE *src = fopen("numbers.txt","w+");
setvbuf(src, NULL, _IONBF, 0);//設置緩衝模式爲無緩衝
for(int i = 0; i < N; i++)
{
fprintf(src ,"%d\n", rand());//寫入隨機數
}
fclose(src);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define N 1000000
void 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);//遞歸循環
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define N 1000000
int main()
{
FILE *src = fopen("sort_num.txt","r");
int temp[N] = {0};
int *p = temp;
int n = 0;
for(int i = 0; i < N; i++)
{
fscanf(src , "%d", &temp[i]);//將所有數據存入到數組中
n++;
}
fclose(src);
printf("讀取完畢!讀取次數 %d次。\n",n);
n = 0;
for(int j = 0; j < 10; j++)
{
char file[10] = {0};//數組用於存放文件的名稱
sprintf(file , "%d_txt", j+1);//將字符寫入file
FILE *dst = fopen(file,"w+");//打開file
for(int i = n; i < N; i++)
{
fprintf(dst ,"%d\n", temp[i]);//寫入數據
n++;
if((n/100000) == j+1)//寫入十萬次就跳出此循環
{
printf("寫入第%2d個文件中...共寫入%d個數據。\n\n",j+1,n);
break;
}
}
fclose(dst);//關閉當前文件
}
printf("寫入完畢!\n");
return 0;
}
更多資料請點擊:我的目錄