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


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


     

  • 相关阅读:
    IDEA(安装和使用)
    亚马逊审核儿童滑板车CPC认证 ASTMF963检测
    常见应用层协议
    第十一章 JSP开发模型
    没有事情做 随手写的小程序
    医院信息系统源码 HIS源码
    【数据结构】树与二叉树
    [RoarCTF 2019]Simple Upload
    人机交互中的数字与文字
    字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
  • 原文地址:https://blog.csdn.net/zzjlhlcd/article/details/127711958