zoukankan      html  css  js  c++  java
  • 蛋疼的回忆一下算法

    #include <iostream>
    #define len(x) (unsigned int)sizeof(x)/sizeof(x[0])
    
    using namespace std;
    
    void Swap(int x[],int i,int j){
        int tem = x[i];
        x[i] = x[j];
        x[j] = tem;
    }
    
    void print(int x[],int length){
        for(int i = 0; i < length; i++){
            cout << x[i] << "	";
        }
        cout <<endl;
    }
    
    void bubbleSort(int r[],int length){
        bool flag = true;
        for(int i = 0; i < length-1 && flag; i++){
            flag = false;
            for(int j = 0; j < length - i-1;j++){
                if(r[j] < r[j+1]) {
                    Swap(r,j,j+1);
                    flag = true;
                }
            }
        }
    }
    
    void bubbleSort1(int r[],int length){
        for(int i = 0; i < length - 1; i++){
            for(int j = length - 1; j > i ; j--){
                if(r[j] > r[j-1]){
                    Swap(r,j,j-1);
                }
            }
        }
    }
    
    void selectSort(int r[],int length){
        for(int i = 0; i < length-1; i++){
            int _min = i;
            for(int j = i+1; j < length; j ++){
                if(r[j] < r[_min]) _min = j;
            }
            Swap(r,_min,i);
        }
    }
    
    void insertSort(int r[],int length){
        int _trans = 0;
        for(int i = 0; i < length - 1; i++){
            if(r[i] > r[i+1]){
                _trans = r[i+1];
                int k = i;
                while(r[k] > _trans && k >= 0){//这里是为了防止r[k]乱值导致错误k的索引
                    r[k+1] = r[k];
                    k--;
                }
                r[k+1] = _trans;
            }
        }
    }
    
    void shellSort(int r[],int length){
        int increment = length/2;
        while(increment >= 1){
            for(int i = increment; i < length ; i++){
                int j = i-increment;
                int key = r[i];
                while(j >=0 && r[j] > key){
                        r[j+increment] = r[j];
                        j = j-increment;
                }
                r[j+increment] = key;
            }
            increment = increment/2;
        }
    }
    
    int Partition(int r[],int low,int high){
        int value = r[low];
        while(low < high){
            while(low < high && r[high] >= value) high--;
            Swap(r,low,high);
            while(r[low] <= value && low < high) low++;
            Swap(r,low,high);
        }
        return low;
    }
    
    int Partition1(int r[],int low,int high){
        int value = r[high];
        while(low<high){
            while(low<high && r[low] <= value) low++;
            Swap(r,low,high);
            while(low<high && r[high] >= value) high--;
            Swap(r,low,high);
        }
        return high;
    }
    
    void quickSort(int r[],int low, int high){
        int pivot;
        if(low < high){
            pivot = Partition(r,low,high);
            quickSort(r,low,pivot-1);
            quickSort(r,pivot+1,high);
        }
    
    }
    
    /*
    //void stepSort(int r[],int length){
    //    int step = length/2;
    //    while(step>=1){
    //        for(int i = 0; i < step; i++){
    //            for(int j = i; j < length; j +=step){
    //                cout << j << "	";
    //                int key = r[j];
    //                int k = j;
    //                if(k-step>=0 && r[k-step] > key){
    //                    cout << r[k] <<"->" << r[k-step];
    //                    r[k] = r[k-step];
    //                    k-=step;
    //                }
    //                r[k] = key;
    //            }
    //            cout <<endl;
    //        }
    //        step/=2;
    //    }
    //}
    */
    
    void myShellSort(int r[],int length){
        int step = length/2;
        while(step>=1){
            for(int i = 0; i < step; i++){
                for(int j = i; j < length; j +=step){
                    cout << j << "	";
                    int key = r[j];
                    int k = j;
                    while(k-step>=0 && r[k-step] > key){
                        cout << r[k] <<"->" << r[k-step];
                        r[k] = r[k-step];
                        k-=step;
                    }
                    r[k] = key;
                }
                cout <<endl;
            }
            step/=2;
        }
    }
    
    int main()
    {
        int r[] = {6,2,7,3,4,8,1,9,5,0};
        print(r,len(r));
        //bubbleSort1(r,len(r));
        //selectSort(r,len(r));
        //insertSort(r,len(r));
        //quickSort(r,len(r));
        //myShellSort(r,len(r));
        quickSort(r,0,len(r)-1);
        print(r,len(r));
        return 0;
    }
    

      好久都没有写排序了,最近闲的无事加上又要面试了,就写点吧!以上几种算法的自己模拟版本。

      在写c++的过程当中,又发现了好多东西自己都不不会哎,比如说不同编译器对没有初始化的int赋值为多少呢?能不能写一个函数来求数组的长度呢?结果是不行的,c++中以参数的形式传递的时候,数组就退化为了指针,使用sizeof(arr)/sizeof(arr[0]),这就是为什么大神们网上晒出来的代码都要传递一个参数int length,别以为人家傻,人家聪明着呢!一开始都以为可以传一个数组的引用进来,然后递归的求长度,结果是不可行的!记住吧,以后就这么用了。

      当然还有一个办法就是使用memset先将空数组都赋值为0!在根据这个来求!

  • 相关阅读:
    HBuilder在线打包ipa步骤
    SWD烧录/仿真方式
    详解shell脚本括号区别--$()、$「 」、$「 」 、$(()) 、「 」 、「[ 」]
    Centos/Linux下调整分区大小(以home和根分区为例)
    Centos6.5安装中文支持和中文输入法
    如何用电路实现检测过零点?这个简单电路就能搞定
    ifconfig无输出的原因及解决办法
    Linux云服务器下Tomcat部署
    linux wget 命令用法详解(附实例说明)
    yum的repo文件详解、以及epel简介、yum源的更换
  • 原文地址:https://www.cnblogs.com/luomingchuan/p/3677078.html
Copyright © 2011-2022 走看看