• 数据结构和算法之排序和查找


    1、排序
    冒泡

    选择法 排序
    代码:
    #include
    #include
    #include

    void paixu_xuanze(int puke[],int count);
    int main()
    {

        int puke[]={9,6,3,5,2,4,7};  //2 6 3 5 9 
        paixu_xuanze(puke,7);

        for(int i=0;i<7;i++)
        {
            printf("%d\t",puke[i]);
        }
        return 0;
    }

    void paixu_xuanze(int puke[],int count)
    {
        int min_xuhao;
        int temp;
        int i;
        
        for(int j=0;j     {
            min_xuhao=j;            //0--3
            for(i=j+1;i         {
                if(puke[min_xuhao]>puke[i])            //1---4
                {
                    min_xuhao=i;
                }
                else
                {
                    
                }
            }
            if(min_xuhao!=j)                //0--3
            {
                temp=puke[min_xuhao];
                puke[min_xuhao]=puke[j];
                puke[j]=temp;
            }

        }


        return;
    }


    插入法排序


    9,6,3,5,2,4,7

    9,6,3,5,2,4,7
    6 9 3 5 2 4 7
    6 3 9 5 2 4 7
    3 6 9 5 2 4 7
    3 5 6 9 2 4 7
    2 3 5 6 9 4 7
    2 3 4 5 6 9 7 
    2 3 4 5 6 7 9


    代码:
    #include
    #include
    #include

    void paixu_charu(int puke[],int count)
    {
        int min_xuhao=0;
        int temp;
        if(puke[0]>puke[1])
        {
            temp=puke[1];
            puke[1]=puke[0];
            puke[0]=temp;
        }
        else
        {
            ;
        }


        if(puke[1]>puke[2])
        {
            temp=puke[2];
            puke[2]=puke[1];
            puke[1]=temp;
        }
        else
        {
            ;
        }

        if(puke[0]>puke[1])
        {
            temp=puke[1];
            puke[1]=puke[0];
            puke[0]=temp;
        }
        else
        {
        ;
        }

        return;
    }


    int main()
    {

        int puke[]={9,6,3,5,2,4,7};  //2 6 3 5 9 
        paixu_charu(puke,7);

        for(int i=0;i<7;i++)
        {
            printf("%d\t",puke[i]);
        }
        return 0;
    }


    //完善后的插入排序算法 代码:
    #include
    #include
    #include

    void paixu_charu(int puke[],int count)
    {
        int temp;
        int i;
        int j;    
        for(j=0;j<4;j++)
        {
            for(i=j;i>=0;i--)            //0-3
            {
                if(puke[i]>puke[i+1])            //0--1   //1--2  0--1
                {
                    temp=puke[i+1];
                    puke[i+1]=puke[i];
                    puke[i]=temp;            
                }
                else
                {
                    break;
                }            
            }
        }    
        return;
    }

    int main()
    {    
        int puke[]={9,6,3,5,2}; //3 6 9 5 2
        paixu_charu(puke,5);
        
        for(int i=0;i<5;i++)
        {
            printf("%d\t",puke[i]);
        }
        return 0;
    }

    查找:
    顺序查找
    二分查找--------

    3  6  9  23  56 

    找 23,如果找到,请返回下标;如果没找到,返回-1

    代码:----还需要完善的代码
    #include
    #include
    #include

    int find_binarySearch(int puke[],int count,int data)
    {
        int pos=-1;
        int low=0;
        int hign=count-1;
        int mid=(low+hign)/2;

        if(data==puke[mid])
        {
            pos=mid;
        }
        else if(data     {
            pos=find_binarySearch(puke+low,mid,data);
        }
        else
        {
            int temp=find_binarySearch(puke+mid+1,count-mid-1,data);
            pos=temp+mid+1;
        }


        return pos;
    }

    int main()
    {
        
        int puke[]={3,6,9,23,56}; 
        int pos=find_binarySearch(puke,5,57);
        printf("%d\n",pos);
        return 0;
    }

    //完善后的二分法查找
    #include
    #include
    #include

    int find_binarySearch(int puke[],int count,int data)
    {
        if(count<1)
        {
            return -1;
        }
        int pos=-1;
        int low=0;
        int hign=count-1;
        int mid=(low+hign)/2;

        if(data==puke[mid])
        {
            pos=mid;
        }
        else if(data     {
            pos=find_binarySearch(puke+low,mid,data);
        }
        else
        {
            int temp=find_binarySearch(puke+mid+1,count-mid-1,data);
            if(temp!=-1)
            {
                pos=temp+mid+1;
            }
        }
        
        
        return pos;
    }

    int main()
    {
        
        int puke[]={3,6,9,23,56}; 
        int pos=find_binarySearch(puke,5,56);
        printf("%d\n",pos);
        return 0;
    }


     

  • 相关阅读:
    MyCat简介与安装
    【手把手教你写Go】01.为什么要选择Go语言
    云计算四层介绍
    pytorch 中 nn.Conv2d 解释
    天锐绿盾加密软件——企业数据防泄密-CAD图纸、文档、源代码加密管理系统@德人合科技
    svelte组件:Svelte3自定义Navbar+Tabbr组件|svelte自定义插件
    【牛客 - 剑指offer】JZ8 二叉树的下一个结点 Java实现
    获取dubbo源码编译并导入idea以及启动入门项目dubbo-demo
    springboot整合redis,并使用@Cacheable等注解进行缓存
    Jetson nano安装torch和torchvision
  • 原文地址:https://blog.csdn.net/zzjlhlcd/article/details/127711958