排序算法和搜索学习

#include<iostream>
#include<algorithm>
#include <random>

using namespace std;
template<typename  T>
class Sort
{
public:
    void BubbleSort(T list[], int n);
    void SelectSort(T list[], int n);
    void InsertionSort(T list[], int n);
    void QuickSort(T list[], const int left, const int right);
    void Merge(T* initList, T* mergedList, const int l, const int m, const int n);
    void MergeSort(T* initList, T* mergedList, const int n, const int s=2);
    void CreatRandomArray(T* initList, const int range1,const int range2,const int n)
    {
        default_random_engine e;
        uniform_int_distribution<unsigned> u(range1, range2);
        for (int i = 0; i < n; i++)
        {
            initList[i] = u(e);
        }
    }
    void CheckList(T* initList,const int n)
    {
        for (int i = 0; i < n - 1; i++)
        {
            if (initList[i] > initList[i + 1])
            {
                cout << "Sort error" << endl;
                return;
            }
        }
        cout << "Sort right" << endl;
    }
    void printlist(T list[],int num)
    {
        printf("数组有%d个数 数组:",num);
        for (int i = 0; i < num; i++)
            printf("%d ", list[i]);
        printf("\n");
    }
};
template<typename T>
//冒泡排序
void Sort<T>::BubbleSort(T list[], int n)
{
for(int i=0;i<n-1;i++)
    for (int j = 0; j < n - 1-i; j++)
    {
        if (list[j] > list[j + 1])
        {
            std::swap(list[j], list[j + 1]);
        }
    }
}
template<typename T>
//选择排序
void Sort<T>::SelectSort(T list[], int n)
{
    
    for (int i = 0; i < n-1; i++)
    {
        int min = i;
        for (int j = i+1; j < n; j++)
        {
            if (list[j] < list[min])
                min = j;
        }
        std::swap(list[i], list[min]);
    }
}
template<typename T>
//插入排序
void Sort<T>::InsertionSort(T list[], int n)
{
    int out, in;
    //先出去一个 
    for (out = 1; out < n; out++)
    {
        int temp = list[out];
        in = out;
        while (in>0&&list[in-1]>=temp)
        {
            list[in] = list[in - 1];
            in--;
        }
        list[in] = temp;
    }
}
template<typename T>
//快速排序
void Sort<T>::QuickSort(T list[], const int left, const int right)
{
    if (left < right)
    {
        int pivot = list[left];
        int i = left;
        int j = right;
        do {
            do i++; while (list[i] < pivot&& i<= right);
            do j--; while (list[j] > pivot&& j >= left);
            if(i<j)std::swap(list[i], list[j]);
        } while (i < j);
        std::swap(list[left],list[j]);

        QuickSort(list, left, j - 1);
        QuickSort(list, j + 1, right);
    }
}
//归并排序
/*
将数组两部分分别排序
param l:前半部分起始id
param m:后半部分起始id
param n:数组最后id+1
*/
template<typename T>
void Sort<T>::Merge(T* initList, T* mergedList, const int l, const int m,const int n)
{
    int i1, i2, iResult;
    for (i1 = l, i2 = m , iResult = l; i1 < m && i2 < n; iResult++)
    {
        if (initList[i1] <= initList[i2])
        {
            mergedList[iResult] = initList[i1];
            i1++;
        }
        else
        {
            mergedList[iResult] = initList[i2];
            i2++;
        }
    }
    /* //copy用法一:  
16     //将数组myints中的七个元素复制到myvector容器中  
17     copy ( myints, myints+7, myvector.begin() );  */
    copy(initList + i1, initList + m , mergedList + iResult);
    copy(initList + i2, initList + n , mergedList + iResult);
}
template<typename T>
void Sort<T>::MergeSort(T* initList, T* mergedList, const int n, const int s)
{
    if(s<n)
    {
        for (int i = 0; i < n; i += s)
        {
            //if (s + i > n)
            //    break;
            if ((i+s) > n)
            {
                //Merge(initList, mergedList, i, i + s - s / 2, n);
                //copy(mergedList + i, mergedList + n, initList + i);//将变化后的数组段传给initList
                for (int outnum = i + 1; outnum < n; outnum++)
                {
                    int temp = initList[outnum];
                    int innum = outnum;
                    while (innum > i && initList[innum - 1] > temp)
                    {
                        initList[innum] = initList[innum - 1];
                            innum--;
                    }
                    initList[innum] = temp;
                }
            }
            else 
            {
                Merge(initList, mergedList, i, i + s - s / 2, i + s);
                copy(mergedList + i, mergedList + i + s, initList + i);//将变化后的数组段传给initList
            }
        }
        //MergePass(mergedList, mergedList, n, s * 2); 当这样时 传入的initList会和mergedList指向同一地址
        MergeSort(initList, mergedList, n, s * 2);
    }
    else
    {
        Merge(initList, mergedList, 0, s / 2, n);
    }
}
class Search
{
public:
    int SequentialSearch(int* list, int n, int x)
    {
        int i;
        for (i = 0; i < n; i++)
        {
            if (list[i] == x)
                return i;
        }
        if (i == n)
            return -1;
    }
    int BinarySearch(int* list, int n, int x)
    {
        int low, high, mid;
        low = 0;
        high = n-1;
        while (low<=high)
        {
            mid = (low + high) / 2;
            if (list[mid] == x)
                return mid;
            else if (list[mid] > x)
                high = mid - 1;
            else low = mid + 1;
        }
        return -1;
    }
    int BinarySearch(int* list, int low, int high, int x)
    {
        if (low <= high)
        {
            int mid = (low + high) / 2;
            if (list[mid] == x)
                return mid;
            else if (list[mid] > x)
                BinarySearch(list, low, mid - 1, x);
            else BinarySearch(list, mid + 1, high, x);
        }
        else return -1;
    }
};
void show(char* p, int m)
{
    for (int i = 0; i <= m; i++)
        cout << p[i];
    cout << endl;
}
void Perm(char *p,int k,int m)
{
    //递归排列
    //比如abc 
    //a排列 b排列 c排列
    //一层一层下去
    if (k == m)
    {
        //show(p, m);
    }
    else for (int i = k; i <= m; i++)
    {
        std::cout << "递归前,交换前";
        show(p, m);
        std::swap(p[k],p[i]);
        std::cout << "递归前,交换后";
        show(p, m);
        Perm(p,k+1,m);//第一次出现m种情况,第二次出现m-1种情况,递归下去直到只有1种情况
        std::cout << "递归后,交换前";
        show(p, m);
        std::swap(p[k], p[i]);
        std::cout << "递归后,交换后";
        show(p, m);
    }
}

int main()
{
    int a[] = { 5,6,4,2,3,1,8,5,7 };
    int b[20];
    int c[20];
    int num = sizeof(a) / sizeof(*a);
    Sort<int> sort;
    sort.QuickSort(a,0,num);
    for (int i = 0; i < 100; i++)
    {
        sort.CreatRandomArray(b,0,100,20);
        sort.MergeSort(b, c, 20);
        sort.CheckList(c, 20);
    }
    sort.printlist(c, 20);
    Search search;
    int id_num =search.BinarySearch(a, num, 56);
    int id_num2 = search.BinarySearch(a,0, num, 56);

    //printf("id=%d,num=%d\n", id_num,a[id_num]);
    //printf("id=%d,num=%d\n", id_num2, a[id_num2]);
    char p[] = "abc";
    //Perm(p, 0, 2);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值