文件排序及二分查找法与冒泡排序法

Posted 芬乐

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件排序及二分查找法与冒泡排序法相关的知识,希望对你有一定的参考价值。

char *fputs(char *s, int n, FILE *fp)
int fputs(char *s, FILE *fp)
fgets正常时返回读取字符串的首地址,出错或文件尾,返回NULL。
fputs正常时返回写入的最后一个字符,出错返回EOF。

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <stdlib.h>
#include <time.h>

//文件排序

//内存排序

void main1()
{
    time_t ts;
    unsigned int num = time(&ts);//获取随机数种子
    srand(num);

    int a[100];
    for (int i = 0; i < 100;i++)
    {
        a[i] = rand() % 100;
    }
    FILE *pf = fopen("C:\\1.data", "wb");
    fwrite(a, sizeof(int), 100, pf);
    fclose(pf);

    system("pause");





}
int com(void *p1, void *p2)
{
    int *pint1 = p1;
    int *pint2 = p2;
    if (*pint1 >*pint2)
    {
        return 1; //大于
    } 
    else if (*pint1 <*pint2)
    {
        return -1;//小于
    } 
    else
    {
        return 0;
    }
    //>  1,0

}
//读取到内存,然后排序
void memsort()
{
    int *p = malloc(400);
    int *pread = malloc(400);
    FILE *pf = fopen("C:\\1.data", "rb+");
    fread(p, sizeof(int), 100, pf);
    for (int i = 0; i < 100; i++)
    {
        printf("\n%d", p[i]);
    }
    fflush(pf);//刷新,生效

    qsort(p, 100, 4, com);//排序
    printf("sort later\n");
    for (int i = 0; i < 100; i++)
    {
        printf("\n%d", p[i]);
    }
    rewind(pf);
    fwrite(p, sizeof(int), 100, pf);
    fflush(pf);//刷新,生效
    rewind(pf);
    fread(pread, sizeof(int), 100, pf);
    fflush(pf);//刷新,生效
    for (int i = 0; i < 100;i++)
    {
        printf("\npread=%d", pread[i]);
    }


    fclose(pf);



}

void fileread()
{
    FILE *pf = fopen("C:\\1.data", "rb");
    for (int i = 0; i < 100;i++)
    {
        int num;
        fseek(pf,i*sizeof(int) , SEEK_SET);//i顺序,99-i逆序
        fread(&num, sizeof(int), 1, pf);
        if (num==3)
        {
            printf("%d", num);//顺序查找 100
        } 
        
        printf("\nnum=%d", num);
    }
    fclose(pf);

}

void binsearch(int findnum)
{
    FILE *pf = fopen("C:\\1.data", "rb");
    int tou = 0;
    int wei = 99;
    int flag = 0;
    int ci = 0;
    while (tou<=wei)
    {
        ci++;
        int  zhong = (tou + wei) / 2;
        //a[zhong]
        int  zhongdata;
        fseek(pf,zhong*sizeof(int), SEEK_SET);//i顺序,99-i逆序
        fread(&zhongdata, sizeof(int), 1, pf);
        if (findnum==zhongdata)
        {
            flag = 1;//找到
            printf("\n找到%d", zhongdata);
            break;
        } 
        else if (findnum > zhongdata)
        {
            tou = zhong + 1;
        } 
        else
        {
            wei = zhong - 1;
        }


    }
    if (flag)
    {
        printf("找到了\n");
    } 
    else
    {
        printf("没找到了\n");
    }
    printf("找了%d次",ci);




    fclose(pf);

}
void main3x()
{
    //memsort();
    //fileread();
    fileread();
    while (1)
    {
        int findnum;
        scanf("%d", &findnum);
        binsearch(findnum);
    }
    system("pause");


}


void randwirte()
{
    time_t ts;
    unsigned int num = time(&ts);//获取随机数种子
    srand(num);

    int a[100];
    for (int i = 0; i < 100; i++)
    {
        a[i] = rand() % 100;
    }
    FILE *pf = fopen("C:\\sort.data", "wb");
    fwrite(a, sizeof(int), 100, pf);
    fclose(pf);

}
void showfile()
{
    int *p = malloc(400);
    FILE *pf = fopen("C:\\sort.data", "rb");
    fread(p, sizeof(int), 100, pf);
    for (int i = 0; i < 100; i++)
    {
        printf("\n%d", p[i]);
    }
    fclose(pf);

}

void main()
{
    //showfile();
    showfile();
    printf("\n排序以后");
    FILE *pf = fopen("C:\\sort.data", "rb+");
    for (int i = 0; i < 100 - 1;i++)
    {
        for (int j = 0; j < 100 - 1 - i;j++)
        {
            int dataj = 0;
            fseek(pf, sizeof(int)*j, SEEK_SET);
            fread(&dataj, sizeof(int), 1, pf);

            int dataj1 = 0;
            fseek(pf, sizeof(int)*(j+1), SEEK_SET);
            fread(&dataj1, sizeof(int), 1, pf);
            if (dataj <dataj1)
            {
                fseek(pf, sizeof(int)*(j + 1), SEEK_SET);
                fwrite(&dataj, 4, 1, pf);

                fseek(pf, sizeof(int)*j, SEEK_SET);
                fwrite(&dataj1, 4, 1, pf);
            }

            /*if (a[j]<a[j+1])
            {
                swap(a[j] a[j+1])
            }*/
        }
    }
    fclose(pf);
    showfile();
    system("pause");
}

 

以上是关于文件排序及二分查找法与冒泡排序法的主要内容,如果未能解决你的问题,请参考以下文章

算法导论第2章 分治法与归并排序, 二分查找法

数组冒泡排序选择排序二分查找法

用JavaScript实现冒泡排序法选择排序法二分法查找

二分查找法 冒泡排序法

冒泡排序,递归二分查找法,二分查找法

冒泡排序二分查找法