我们上节课,处理排序的文本数据,就10个数据(一行一个).
如果有10个数据.
第一次需要循环9次.
第二次需要循环8次.
第三次需要循环7次.
……
第八次需要循环2次.
如果有100万个数据.
第1次需要循环999999次
第2次需要循环999998次
第3次需要循环999997次
第999998次需要2次.
如果数据是100万,1000万,需要多长时间呢?我们来测试一下
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int gettimestamp() {
time_t l_now;
time(&l_now);
return(int)l_now;
}
void randmize(int p_v1) {
srand(p_v1);
}
int rand(int p_min, int p_max) {
return rand() % (p_max - p_min + 1) + p_min;
}
void paixu(int *l_array, int count) {
int l_length = count;
char l_temp;
for (size_t i = 0; i < l_length - 1; i++)
{
for (size_t ii = i + 1; ii <= l_length - 1; ii++)
{
if (l_array[i] < l_array[ii]) {
l_temp = l_array[ii];
l_array[ii] = l_array[i];
l_array[i] = l_temp;
}
}
}
}
void main1() {
int time = gettimestamp();
randmize(time);
int l_length = 10;
int *p = (int *)calloc(l_length, sizeof(int));
for (size_t i = 0; i < l_length; i++)
{
p[i] = rand(1, 100);
printf("%d\n", p[i]);
}
FILE * write = fopen("1.txt", "w");
if (write != NULL) {
char out[20] = { 0 };
for (size_t i = 0; i < l_length; i++)
{
sprintf(out, "%d\n", p[i]);
fputs(out, write);
}
}
fclose(write);
free(p);
system("pause");
}
void main() {
FILE*read = fopen("1.txt", "r");
char out[20] = { 0 };
int *p = NULL;
int count = NULL;
int ccc = NULL;
int length = 10;
if (read != NULL)
{
while (feof(read) == 0) {
fgets(out, length, read);
int cccc = ftell(read);
if (ccc != cccc) {
count++;
p = (int *)realloc(p, count * sizeof(int));
p[count - 1] = atoi(out);
}
}
}
int strat = gettimestamp();
int * b = (int *)calloc(1000, sizeof(int));
for (size_t i = 0; i < count; i++)
{
int bibi = p[i];
b[bibi - 1] = b[bibi - 1] + 1;
}
int end = gettimestamp();
printf("%d\n", end - strat);
FILE * write = fopen("2.txt", "w");
if (write != NULL) {
char out[20] = { 0 };
for (size_t i = 0; i < 1000; i++)
{
sprintf(out, "%d\n", i+1);
for (size_t ii = 0; ii < b[i]; ii++)
{
fputs(out, write);
}
}
}
fclose(read);
free(p);
system("pause");
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int gettimestamp() {
time_t l_now;
time(&l_now);
return(int)l_now;
}
void randmize(int p_v1) {
srand(p_v1);
}
int rand(int p_min, int p_max) {
return rand() % (p_max - p_min + 1) + p_min;
}
void paixu(int *l_array, int count) {
int l_length = count;
char l_temp;
for (size_t i = 0; i < l_length - 1; i++)
{
for (size_t ii = i + 1; ii <= l_length - 1; ii++)
{
if (l_array[i] < l_array[ii]) {
l_temp = l_array[ii];
l_array[ii] = l_array[i];
l_array[i] = l_temp;
}
}
}
}
void main1() {
int time = gettimestamp();
randmize(time);
int l_length = 10;
int *p = (int *)calloc(l_length, sizeof(int));
for (size_t i = 0; i < l_length; i++)
{
p[i] = rand(1, 100);
printf("%d\n", p[i]);
}
FILE * write = fopen("1.txt", "w");
if (write != NULL) {
char out[20] = { 0 };
for (size_t i = 0; i < l_length; i++)
{
sprintf(out, "%d\n", p[i]);
fputs(out, write);
}
}
fclose(write);
free(p);
system("pause");
}
void main() {
FILE*read = fopen("1.txt", "r");
char out[20] = { 0 };
int *p = NULL;
int count = NULL;
int ccc = NULL;
int length = 10;
if (read != NULL)
{
while (feof(read) == 0) {
fgets(out, length, read);
int cccc = ftell(read);
if (ccc != cccc) {
count++;
p = (int *)realloc(p, count * sizeof(int));
p[count - 1] = atoi(out);
}
}
}
int strat = gettimestamp();
paixu(p, count);
int end = gettimestamp();
printf("%d\n", end - strat);
FILE * write = fopen("2.txt", "w");
if (write != NULL) {
char out[20] = { 0 };
for (size_t i = 0; i < count; i++)
{
sprintf(out, "%d\n", p[i]);
fputs(out, write);
}
}
fclose(read);
free(p);
system("pause");
}