数据结构(C++)折半查找,折半插入排序,双向冒泡排序,二叉排序树

折半查找

#include<bits/stdc++.h>
using namespace std;
const int MaxSize = 100;
int datas[MaxSize];
int length;

void LineSearch(int a[], int n) {
    for (int i = 0; i < n; i++) {
        datas[i + 1] = a[i];
        length = n;
    }
}
int SeqSearch(int k) {
    int i = length;
    datas[0] = k;
    while (datas[i] != k)i--;
    return i;
}

int BinSearch1(int k) {
    int mid, low = 1, high = length;
    while (low <= high)
    {
        mid = (low + high) / 2;
        if (k < datas[mid])high = mid - 1;
        else if (k > datas[mid])low = mid + 1;
        else return mid;
    }
    return 0;
}
int BinSearch2(int low, int high, int k) {
    if (low > high)return 0;
    else {
        int mid = (low + high) / 2;
        if (k < datas[mid])return BinSearch2(low, mid - 1, k);
        else if (k > datas[mid])return BinSearch2(mid + 1, high, k);
        else return mid;
    }
}

int main() {
    int arr1[10] = { 9,8,7,3,4,6,5,2,1,10 };
    int arr2[10] = { 1,2,3,4,5,6,7,8,9,10 };
    LineSearch(arr1, 10);
    LineSearch(arr2, 10);
    int select, key, index = 0;
    cout << "请输入要使用的查找算法:" << endl;
    cout << "1、顺序查找" << endl;
    cout << "2、折半查找(非递归)" << endl;
    cout << "3、折半查找(递归)" << endl;
    cout << "请输入序号:" << endl;
    cin >> select;
    cout << "请输入要查找的值: " << endl;
    cin >> key;
    switch (select) {
    case 1:
        index = SeqSearch(key);
        break;
    case 2:
        index = BinSearch1(key);
        break;
    case 3:
        index = BinSearch2(1, 10, key);
        break;
    default:
        cout << "输入有误" << endl;
        break;
    }
    cout << "位置为: " << index << endl;

    return 0;
}

双向冒泡排序

#include<bits/stdc++.h>
using namespace std;

void BidBubbleSort(int array[], int n)
{
    int low, high, flag, i;
    low = 0;
    high = n - 1;
    while (low < high)
    {
        flag = 0;
        for (i = low; i < high; i++)
        {
            if (array[i] > array[i + 1])
            {
                swap(array[i], array[i + 1]);
                flag = 1;
            }
        }
        if (!flag)
            break;
        high--;
        for (i = high; i > low; i--)
        {
            if (array[i] < array[i - 1])
                swap(array[i], array[i - 1]);
        }
        low++;
    }
}
int main()
{
    int arr[10] = { 8, 4, 2, 3, 5, 1, 6, 9, 0, 7 };
    for (int i = 0; i < 10; i++)
        cout << arr[i] <<" ";
    cout << endl;
    BidBubbleSort(arr, 10);
    for (int i = 0; i < 10; i++)
        cout << arr[i] << " ";
    return 0;
}

折半插入排序

#include<bits/stdc++.h>
using namespace std;
void sort(int a[], int n) {

    for (int i = 1; i < n; i++) {
        if (a[i] < a[i - 1]) {
            int temp = a[i];
            int left = 0, right = i - 1;
            while (left <= right) {
                int mid = (right - left) / 2 + left;
                if (temp < a[mid]) {
                    right = mid - 1;
                }
                else {
                    left = mid + 1;
                }
            }
            for (int j = i - 1; j >= right + 1; j--) {
                a[j + 1] = a[j];
            }
            a[left] = temp;
        }
    }
}
int main() {
    int arr[10] = { 4,5,6,7,1,0,2,7,5,8 };
    for (int i = 0; i < 10; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
    sort(arr, 10);
    for (int i = 0; i < 10; i++) {
        cout << arr[i] << " ";
    }
    return 0;
}

二叉排序树

#include<bits/stdc++.h>
using namespace std;
struct BiNode {
    int data;
    BiNode* lchild, * rchild;
};

void Release(BiNode* bt);
BiNode* InsertBST(BiNode* bt, int x);
BiNode* SearchBST(BiNode* bt, int k);
void InOrder(BiNode* bt);
BiNode* root;

void InOrder(BiNode* bt) {
    if (bt == NULL)return;
    else {
        InOrder(bt->lchild);
        cout << bt->data << " ";
        InOrder(bt->rchild);

    }
}
BiNode* SearchBST(BiNode* bt, int k) {
    if (bt == NULL)return NULL;
    if (bt->data == k)return bt;
    else if (bt->data > k)return SearchBST(bt->lchild, k);
    else return SearchBST(bt->rchild, k);
}
BiNode* InsertBST(BiNode* bt, int x) {
    if (bt == NULL) {
        BiNode* s = new BiNode;
        s->data = x;
        s->lchild = NULL; s->rchild = NULL;
        bt = s;
        return bt;
    }
    else if (bt->data > x)bt->lchild = InsertBST(bt->lchild, x);
    else bt->rchild = InsertBST(bt->rchild, x);
}
void BiSortTree(int a[], int n) {
    root = NULL;
    for (int i = 0; i < n; i++) {
        root = InsertBST(root, a[i]);
    }
}
void DeleteBST(BiNode* p, BiNode* f) {
    if ((p->lchild == NULL) && (p->rchild == NULL)) {
        f->lchild = NULL; delete p; return;
    }
    if (p->rchild == NULL) {
        f->lchild = p->lchild; delete p; return;

    }
    if (p->lchild == NULL) {
        f->lchild = p->rchild; delete p; return;
    }
    BiNode* par = p, * s = p->rchild;
    while (s->lchild != NULL) {
        par = s;
        s = s->lchild;
    }
    p->data = s->data;
    if (par == p)par->rchild = s->rchild;
    else par->lchild = s->rchild;
    delete s;
}
void Release(BiNode* bt) {
    if (bt == NULL)return;
    else {
        Release(bt->lchild);
        Release(bt->rchild);
        delete bt;
    }
}
int main() {
    BiNode* p = NULL;
    int arr[10] = { 7,2,3,10,5,6,1,8,9,4 };
    BiSortTree(arr, 10);
    InOrder(root);
    int key;
    cout << "请输入要查找的值:" << endl;
    cin >> key;
    p = SearchBST(root, key);
    if (p != NULL)
        cout << p->data << endl;
    else
        cout << "查找失败" << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

暗紫色的乔松(-_^)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值