zoukankan      html  css  js  c++  java
  • 蛮力法01

    ——在没有稳当的走之前,先不要急着跑。

    排序方法中的选择排序和冒泡排序均属蛮力法,即以常规的、也是易于理解的方式解决问题。

    #ifndef _FENGSORT_H_
    #define _FENGSORT_H_
     
    #include "basic_head.h"
     
    template<typename ObjectType>
    class export_type FengSort
    {
    public:
        //************************************
        // Method:    冒泡排序
        // FullName:  FengSort<ObjectType>::BubbleSort
        // Access:    public 
        // Returns:   void
        // Qualifier: 输入的数组集合可进行>符号运算,长度非空
        // Parameter: ObjectType * arr 数组集合
        // Parameter: int arrLen 数组长度
        //************************************
        void BubbleSort(ObjectType *arr,int arrLen);
        
        //************************************
        // Method:    选择排序
        // FullName:  FengSort<ObjectType>::SelectedSort
        // Access:    public 
        // Returns:   void
        // Qualifier: 输入的数组集合可进行<符号运算,长度非空
        // Parameter: ObjectType * arr 数组集合
        // Parameter: int arrLen 数组长度
        //************************************
        void SelectedSort(ObjectType *arr,int arrLen);    
     
        //************************************
        // Method:    改进冒泡排序,如果排序成功后则退出
        // FullName:  FengSort<ObjectType>::SelectedSort2
        // Access:    public 
        // Returns:   void
        // Qualifier: 输入的数组集合可进行>符号运算,长度非空
        // Parameter: ObjectType * arr
        // Parameter: int arrLen
        //************************************
        void BubbleSort2(ObjectType *arr,int arrLen);    
        
        //************************************
        // Method:    交换值
        // FullName:  FengSort<ObjectType>::ChangedValue
        // Access:    public 
        // Returns:   void
        // Qualifier:
        // Parameter: ObjectType & lsh
        // Parameter: ObjectType & rsh
        //************************************
        void ChangedValue(ObjectType &lsh,ObjectType &rsh);
    };
     
     
    template<typename ObjectType>
    void FengSort<ObjectType>::SelectedSort( ObjectType *arr,int arrLen )
    {
        ConsoleOutput<ObjectType> output;
     
        for (int i=0;i<arrLen-1;++i)
        {
            int minIndex = i;
            for (int j=i+1;j<arrLen;++j)
            {
                if (arr[j] < arr[minIndex])
                {
                    minIndex = j;
                }
            }
            if (minIndex != i)
            {
                ChangedValue(arr[minIndex],arr[i]);
            }
            cout<<"第"<<i<<"次: ";
            output.OutputArray(arr,arrLen);
        }
    }
     
    template<typename ObjectType>
    void FengSort<ObjectType>::ChangedValue( ObjectType &lsh,ObjectType &rsh )
    {
        ObjectType temp = lsh;
        lsh = rsh;
        rsh = temp;
    }
     
    template<typename ObjectType>
    void FengSort<ObjectType>::BubbleSort( ObjectType *arr,int arrLen )
    {
        ConsoleOutput<ObjectType> output;
     
        //i遍历arrLen-1次,每次将最大值“沉底”
        for (int i=0;i<arrLen-1;++i)
        {
            //j从索引0开始比较,到arrLen-1-i-1(arrLen-i-2),如果
            for (int j=0;j<arrLen-i-1;++j)
            {
                if (arr[j] > arr[j+1])
                {
                    cout<<"第"<<i<<"次,交换"<<arr[j]<<"和"<<arr[j+1]<<" ";
                    ChangedValue(arr[j],arr[j+1]);
                    output.OutputArray(arr,arrLen);
                }
            }
            cout<<"第"<<i<<"次结果:";
            output.OutputArray(arr,arrLen);
        }
    }
     
    template<typename ObjectType>
    void FengSort<ObjectType>::BubbleSort2( ObjectType *arr,int arrLen )
    {
        ConsoleOutput<ObjectType> output;
     
        //i遍历arrLen-1次,每次将最大值“沉底”
        for (int i=0;i<arrLen-1;++i)
        {
            bool isSorted = true;
            //j从索引0开始比较,到arrLen-1-i-1(arrLen-i-2),如果
            for (int j=0;j<arrLen-i-1;++j)
            {
                if (arr[j] > arr[j+1])
                {
                    cout<<"第"<<i<<"次,交换"<<arr[j]<<"和"<<arr[j+1]<<" ";
                    ChangedValue(arr[j],arr[j+1]);
                    output.OutputArray(arr,arrLen);
                    isSorted = false;
                }
            }
            cout<<"第"<<i<<"次结果:";
            output.OutputArray(arr,arrLen);
     
            if (isSorted == true)
            {
                cout<<"已经排序完毕,退出循环"<<endl;
                break;
            }
        }
    }
     
    #endif
     
    选择排序测试数据结果:
    image
     
    普通冒泡排序测试数据结果:

    将以下内容进行排序:
    87 57 46 42 36 47 13 12 81 70
    开始排序
    第0次,交换87和57 57 87 46 42 36 47 13 12 81 70
    第0次,交换87和46 57 46 87 42 36 47 13 12 81 70
    第0次,交换87和42 57 46 42 87 36 47 13 12 81 70
    第0次,交换87和36 57 46 42 36 87 47 13 12 81 70
    第0次,交换87和47 57 46 42 36 47 87 13 12 81 70
    第0次,交换87和13 57 46 42 36 47 13 87 12 81 70
    第0次,交换87和12 57 46 42 36 47 13 12 87 81 70
    第0次,交换87和81 57 46 42 36 47 13 12 81 87 70
    第0次,交换87和70 57 46 42 36 47 13 12 81 70 87
    第0次结果:57 46 42 36 47 13 12 81 70 87
    第1次,交换57和46 46 57 42 36 47 13 12 81 70 87
    第1次,交换57和42 46 42 57 36 47 13 12 81 70 87
    第1次,交换57和36 46 42 36 57 47 13 12 81 70 87
    第1次,交换57和47 46 42 36 47 57 13 12 81 70 87
    第1次,交换57和13 46 42 36 47 13 57 12 81 70 87
    第1次,交换57和12 46 42 36 47 13 12 57 81 70 87
    第1次,交换81和70 46 42 36 47 13 12 57 70 81 87
    第1次结果:46 42 36 47 13 12 57 70 81 87
    第2次,交换46和42 42 46 36 47 13 12 57 70 81 87
    第2次,交换46和36 42 36 46 47 13 12 57 70 81 87
    第2次,交换47和13 42 36 46 13 47 12 57 70 81 87
    第2次,交换47和12 42 36 46 13 12 47 57 70 81 87
    第2次结果:42 36 46 13 12 47 57 70 81 87
    第3次,交换42和36 36 42 46 13 12 47 57 70 81 87
    第3次,交换46和13 36 42 13 46 12 47 57 70 81 87
    第3次,交换46和12 36 42 13 12 46 47 57 70 81 87
    第3次结果:36 42 13 12 46 47 57 70 81 87
    第4次,交换42和13 36 13 42 12 46 47 57 70 81 87
    第4次,交换42和12 36 13 12 42 46 47 57 70 81 87
    第4次结果:36 13 12 42 46 47 57 70 81 87
    第5次,交换36和13 13 36 12 42 46 47 57 70 81 87
    第5次,交换36和12 13 12 36 42 46 47 57 70 81 87
    第5次结果:13 12 36 42 46 47 57 70 81 87
    第6次,交换13和12 12 13 36 42 46 47 57 70 81 87
    第6次结果:12 13 36 42 46 47 57 70 81 87
    第7次结果:12 13 36 42 46 47 57 70 81 87
    第8次结果:12 13 36 42 46 47 57 70 81 87

    排序后内容:
    12 13 36 42 46 47 57 70 81 87

    修改后的冒泡排序:

    将以下内容进行排序:
    70 50 6 71 94 33 23 3 63 75
    开始排序
    第0次,交换70和50 50 70 6 71 94 33 23 3 63 75
    第0次,交换70和6 50 6 70 71 94 33 23 3 63 75
    第0次,交换94和33 50 6 70 71 33 94 23 3 63 75
    第0次,交换94和23 50 6 70 71 33 23 94 3 63 75
    第0次,交换94和3 50 6 70 71 33 23 3 94 63 75
    第0次,交换94和63 50 6 70 71 33 23 3 63 94 75
    第0次,交换94和75 50 6 70 71 33 23 3 63 75 94
    第0次结果:50 6 70 71 33 23 3 63 75 94
    第1次,交换50和6 6 50 70 71 33 23 3 63 75 94
    第1次,交换71和33 6 50 70 33 71 23 3 63 75 94
    第1次,交换71和23 6 50 70 33 23 71 3 63 75 94
    第1次,交换71和3 6 50 70 33 23 3 71 63 75 94
    第1次,交换71和63 6 50 70 33 23 3 63 71 75 94
    第1次结果:6 50 70 33 23 3 63 71 75 94
    第2次,交换70和33 6 50 33 70 23 3 63 71 75 94
    第2次,交换70和23 6 50 33 23 70 3 63 71 75 94
    第2次,交换70和3 6 50 33 23 3 70 63 71 75 94
    第2次,交换70和63 6 50 33 23 3 63 70 71 75 94
    第2次结果:6 50 33 23 3 63 70 71 75 94
    第3次,交换50和33 6 33 50 23 3 63 70 71 75 94
    第3次,交换50和23 6 33 23 50 3 63 70 71 75 94
    第3次,交换50和3 6 33 23 3 50 63 70 71 75 94
    第3次结果:6 33 23 3 50 63 70 71 75 94
    第4次,交换33和23 6 23 33 3 50 63 70 71 75 94
    第4次,交换33和3 6 23 3 33 50 63 70 71 75 94
    第4次结果:6 23 3 33 50 63 70 71 75 94
    第5次,交换23和3 6 3 23 33 50 63 70 71 75 94
    第5次结果:6 3 23 33 50 63 70 71 75 94
    第6次,交换6和3 3 6 23 33 50 63 70 71 75 94
    第6次结果:3 6 23 33 50 63 70 71 75 94
    第7次结果:3 6 23 33 50 63 70 71 75 94
    已经排序完毕,退出循环

    排序后内容:
    3 6 23 33 50 63 70 71 75 94

  • 相关阅读:
    快速幂
    三角函数与反三角函数的使用
    poj1050 dp动态规划
    归并排序算法
    KMP算法(查找子序列)
    hdu1233 继续畅通工程 (最小生成树——并查集)
    set<pair<int,int> > 的运用
    HDU 4513 吉哥系列故事――完美队形II (manacher算法)
    Codeforces Round #360 (Div. 2) C. NP-Hard Problem (BFS)
    Codeforces Round #357 (Div. 2) C.Heap Operations 优先队列 + 模拟
  • 原文地址:https://www.cnblogs.com/sharpfeng/p/2659936.html
Copyright © 2011-2022 走看看