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


    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;
    }


     

  • 相关阅读:
    premiere 图片突出滑块效果
    【网络安全】Docker部署DVWA靶机环境
    每天一个设计模式之原型模式
    ES6 入门教程 10 对象的扩展 10.6 对象的扩展运算符
    信安.网络安全.UDP协议拥塞
    均匀B样条曲线的表达式
    C++语法——详细剖析多态与虚函数
    机器视觉杂
    spring-cloud和spring-cloud-alibaba的关系
    CTF入门指南
  • 原文地址:https://blog.csdn.net/zzjlhlcd/article/details/127711958