顺序表创建以及查找排序算法(含有顺序查找算法、带哨兵站顺序查找、折半查找算法、冒泡排序)的C++实现在vs2013环境下实现

查找排序算法的C++实现在vs2013环境下实现
<pre name="code" class="cpp">折半查找算法
<pre name="code" class="cpp">带哨兵站顺序查找算法
<pre name="code" class="cpp">冒泡排序
<pre name="code" class="cpp">含有顺序查找算法、带哨兵站顺序查找、折半查找算法、冒泡排序

#include<iostream>
#include<Windows.h>
#define MAX  100
#define MaxSize  100
typedef int DataType;
typedef int KeyType;
using namespace std;

typedef struct
{
    KeyType key;
}Datatype;

typedef struct
{
    Datatype r[MaxSize];
    int length;    //表长
}Sqlist;


int InitSqlist(Sqlist &S)
{
    cout << "请输入顺序表的表长即数据(整形数据)的个数:";
    cin >> S.length;
    for (int  i = 1; i <=S.length; i++)
    {
        cout << "请输入第" << i << "个数据:";
        cin >> S.r[i].key;
    }
    return 0;//顺序表创建成功
}
//顺序查找
int SeqSearch(Sqlist s, KeyType k)
{
    int i;
    for (i = 1; i <= s.length; i++)
    {
        if (s.r[i].key==k)
        {
            return i;    //返回下坐标,查找成功
        }
    }
    return -1;//查找失败
}

//带哨兵站顺序查找算法
int SeqSearch_guard(Sqlist S, KeyType k)
{
    int i;
    S.r[0].key = k;      // “哨兵”
    for (i = S.length; S.r[i].key != k; --i);
    return i;

}

//折半查找算法
int BinSearch(Sqlist S, KeyType k)
{

    int low, mid, high;
    low = 0;
    high = S.length;
    while (low<=high)
    {
        mid = (low + high) / 2;
        if (S.r[mid].key == k)
        {
            return mid;
        }
        else if (S.r[mid].key > k)
            high = mid - 1;//左区间查找
        else
            low = mid + 1;//右区间查找
    }
    return -1;//查找失败
}

void Search(Sqlist S)
{
    int key;
    cout << "请问客户要查找该数据的哪个数据?";
    cin >> key;
    Sleep(500);
    cout << "\n-----------你已进入顺序查找算法[复杂度O(n)]-----------\n";
    if (SeqSearch(S, key) == -1)
        cout << "未查到!!!";
    else
        cout << "已查到该查到该下标[下标从1计数]为:" << SeqSearch(S, key) << endl;
    Sleep(500);
    cout << "\n-----------你已进入带哨兵站顺序查找算法[复杂度O(n)]-----------\n";
    if (SeqSearch_guard(S, key) == 0)
        cout << "未查到!!!";
    else
        cout << "已查到该查到该下标[下标从1计数]为:" << SeqSearch(S, key) << endl;
}
int SearchBin(Sqlist S)
{
    cout << "你已进入有序表的折半查找算法中:\n";
    int key;
    cout << "请输入你要查找的关键字(int型):";
    cin >> key;
    if (BinSearch(S, key) == -1)
    {
        cout << "查找失败!!!";
        return false;
    }
    else
    {
        cout << "折半查找已查到该关键字" << key << "下标为:" << BinSearch(S, key) << endl;
        return true;

    }
        

}

//冒泡排序
void BubbleSort(DataType a[MAX], int length)
{
    DataType temp;
    cout << "冒泡排序前的结果:\n";
    for (int i = 0; i < length; i++)
    {
        cout << "\t" << a[i];
        if ((i + 1) % 6 == 0)
        {
            cout << "\n";
        }

    }
    for (int i = 0; i < length - 1; i++)
    {
        for (int j = 0; j < length - 1 - i; j++)
        {
            if (a[j] > a[j + 1])
            {
                temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }

    }
    cout << "\n冒泡排序后的结果:\n";
    for (int i = 0; i < length; i++)
    {
        cout << "\t" << a[i];
        if ((i + 1) % 6 == 0)
        {
            cout << "\n";
        }

    }
    cout << "\n";

}

void DisplayBubbleSort()//展示冒泡排序
{
    DataType a[MAX];
    int num;
    cout << "\n-----------------欢迎使用冒泡排序法------------------\n";
    cout << "请客户输入一组需要排序的个数num\n";
    cin >> num;
    cout << "请客户输入这组需要排序的数字";
    for (int i = 0; i < num; i++)
    {
        cin >> a[i];
    }
    BubbleSort(a, num);

}


int main()
{
    cout << "\n----------------数据结构中查找与排序算法的实现---------------\n";
    cout << "含有顺序查找算法、带哨兵站顺序查找、折半查找算法、冒泡排序---------------\n";
    Sqlist S;    //顺序表查找
    S.length = 0;
    InitSqlist(S);
    Search(S);//客户查找

    Sqlist s;    //有序表查找
    s.length = 0;
    cout << "\n----------------你已进入有序表的创建-----------------\n";
    InitSqlist(s);
    SearchBin(s); //客户查找

    DisplayBubbleSort();//展示冒泡排序
    system("pause");
}
<img src="https://img-blog.csdn.net/20151030113735563?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />


//冒泡排序
    原文作者:排序算法
    原文地址: https://blog.csdn.net/liuchuang_mfc/article/details/49508815
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞