zoukankan      html  css  js  c++  java
  • 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 ", 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 ", 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 ", 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 ", 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 ", 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 ", 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 ", 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 ", p[i]);

    fputs(out, write);
    }

    }
    fclose(read);
    free(p);
    system("pause");

    }

  • 相关阅读:
    Leetcode Python笔记
    Jenkins 自动化构建基于svn的vue项目 ,以及uniapp的H5项目
    Jenkins 持续化部署
    java中的各种锁详细介绍
    Maven私有仓库搭建 Nexu3
    Spring Cloud构建微服务架构(一)服务注册与发现
    SpringCloud学习笔记(开篇)
    介绍一款 redis 可视化工具
    java 在线网络考试系统源码 springboot mybaits vue.js 前后分离跨域
    java OA办公系统源码 Springboot Activiti工作流 vue.js 前后分离 集成代码生成器
  • 原文地址:https://www.cnblogs.com/xiaodaxiaonao/p/9032152.html
Copyright © 2011-2022 走看看