086.冒泡排序的缺点及解决方案

 

我们上节课,处理排序的文本数据,就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");

}

 

转载于:https://www.cnblogs.com/xiaodaxiaonao/p/9032152.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值