鱼C论坛

 找回密码
 立即注册
查看: 1593|回复: 3

[已解决]c++

[复制链接]
发表于 2023-10-22 11:32:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
随机输入一组数组元素值,利用冒泡排序使一个数组有序。然后随机输入一个数,用折半查找法在数组中查找,如在数组中,则输出元素在数组中的位置;如不在,则输出提示。
最佳答案
2023-10-22 11:32:51
冒泡排序是一种简单的排序算法,它通过多次比较和交换来将数组中的元素按照升序或降序排列。下面是使用冒泡排序算法对数组进行排序的C++代码:
#include <iostream>
using namespace std;

void bubbleSort(int arr[], int size) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int binarySearch(int arr[], int size, int target) {
    int left = 0;
    int right = size - 1;
    
    while (left <= right) {
        int mid = (left + right) / 2;
        
        if (arr[mid] == target) {
            return mid;
        }
        
        if (arr[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    
    return -1;
}

int main() {
    const int size = 10;
    int arr[size];
    
    // 随机输入一组数组元素值
    cout << "请输入" << size << "个整数:" << endl;
    for (int i = 0; i < size; i++) {
        cin >> arr[ i];
    }
    
    // 使用冒泡排序使数组有序
    bubbleSort(arr, size);
    
    // 随机输入一个数
    int target;
    cout << "请输入要查找的数:" << endl;
    cin >> target;
    
    // 使用折半查找法在数组中查找
    int index = binarySearch(arr, size, target);
    
    // 输出结果
    if (index != -1) {
        cout << "元素在数组中的位置为:" << index << endl;
    } else {
        cout << "元素不在数组中" << endl;
    }
    
    return 0;
}

以上代码首先使用冒泡排序算法对输入的数组进行排序,然后通过折半查找法在排序后的数组中查找目标数。如果找到目标数,则输出其在数组中的位置;如果未找到,则输出提示。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-22 11:32:51 | 显示全部楼层    本楼为最佳答案   
冒泡排序是一种简单的排序算法,它通过多次比较和交换来将数组中的元素按照升序或降序排列。下面是使用冒泡排序算法对数组进行排序的C++代码:
#include <iostream>
using namespace std;

void bubbleSort(int arr[], int size) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int binarySearch(int arr[], int size, int target) {
    int left = 0;
    int right = size - 1;
    
    while (left <= right) {
        int mid = (left + right) / 2;
        
        if (arr[mid] == target) {
            return mid;
        }
        
        if (arr[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    
    return -1;
}

int main() {
    const int size = 10;
    int arr[size];
    
    // 随机输入一组数组元素值
    cout << "请输入" << size << "个整数:" << endl;
    for (int i = 0; i < size; i++) {
        cin >> arr[ i];
    }
    
    // 使用冒泡排序使数组有序
    bubbleSort(arr, size);
    
    // 随机输入一个数
    int target;
    cout << "请输入要查找的数:" << endl;
    cin >> target;
    
    // 使用折半查找法在数组中查找
    int index = binarySearch(arr, size, target);
    
    // 输出结果
    if (index != -1) {
        cout << "元素在数组中的位置为:" << index << endl;
    } else {
        cout << "元素不在数组中" << endl;
    }
    
    return 0;
}

以上代码首先使用冒泡排序算法对输入的数组进行排序,然后通过折半查找法在排序后的数组中查找目标数。如果找到目标数,则输出其在数组中的位置;如果未找到,则输出提示。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-22 11:32:54 | 显示全部楼层
这是一个C++程序,它首先使用冒泡排序对一个随机输入的数组进行排序,然后使用二分查找法在排序后的数组中查找一个随机输入的数。如果找到了,它会输出该元素在数组中的位置;如果没有找到,它会输出一个提示。
#include <iostream>
using namespace std;

void bubbleSort(int arr[], int n) {
    for(int i = 0; i < n-1; i++) {
        for(int j = 0; j < n-i-1; j++) {
            if(arr[j] > arr[j+1]) {
                swap(arr[j], arr[j+1]);
            }
        }
    }
}

int binarySearch(int arr[], int l, int r, int x) {
    if (r >= l) {
        int mid = l + (r - l) / 2;
        if (arr[mid] == x)
            return mid;
        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);
        return binarySearch(arr, mid + 1, r, x);
    }
    return -1;
}

int main() {
    int n;
    cout << "请输入数组的大小: ";
    cin >> n;
    int arr[n];
    cout << "请输入数组元素: ";
    for(int i = 0; i < n; i++) {
        cin >> arr[ i];
    }
    bubbleSort(arr, n);
    cout << "排序后的数组是: ";
    for(int i = 0; i < n; i++) {
        cout << arr[ i] << " ";
    }
    cout << "\
请输入要查找的元素: ";
    int x;
    cin >> x;
    int result = binarySearch(arr, 0, n - 1, x);
    (result == -1) ? cout << "元素不在数组中"
                   : cout << "元素在数组中的位置: " << result;
    return 0;
}

在这个程序中,我们首先定义了一个冒泡排序函数bubbleSort,它会对输入的数组进行排序。然后我们定义了一个二分查找函数binarySearch,它会在输入的数组中查找指定的元素。在main函数中,我们首先获取用户输入的数组大小和数组元素,然后对数组进行排序,并输出排序后的数组。然后我们获取用户想要查找的元素,并使用二分查找法在数组中查找这个元素。如果找到了,我们就输出这个元素在数组中的位置;如果没有找到,我们就输出一个提示。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-22 11:33:14 | 显示全部楼层
问题分析:

你的问题需要让用户随机输入一组数组元素值,然后使用冒泡排序算法对数组进行排序。接着,用户需要随机输入一个数,使用折半查找法在排序后的数组中查找该数。如果找到,则输出元素在数组中的位置;如果未找到,则输出相应的提示。

解决方案:

以下是基于你的问题描述所修改的代码:
#include <iostream>using namespace std;// 冒泡排序void bubbleSort(int arr[], int length) {    for (int i = 0; i < length - 1; i++) {        for (int j = 0; j < length - i - 1; j++) {            if (arr[j] > arr[j + 1]) {                swap(arr[j], arr[j + 1]);            }        }    }}// 折半查找int binarySearch(int arr[], int length, int target) {    int left = 0;    int right = length - 1;    while (left <= right) {        int mid = left + (right - left) / 2;        if (arr[mid] == target) {            return mid; // 找到目标元素,返回索引位置        } else if (arr[mid] < target) {            left = mid + 1; // 目标元素在右半部分        } else {            right = mid - 1; // 目标元素在左半部分        }    }    return -1; // 未找到目标元素}int main() {    int n;    cout << "请输入数组长度:";    cin >> n;    int* arr = new int[n];    cout << "请输入数组元素值:";    for (int i = 0; i < n; i++) {        cin >> arr[i];    }    // 冒泡排序    bubbleSort(arr, n);    int target;    cout << "请输入要查找的数:";    cin >> target;    // 折半查找    int result = binarySearch(arr, n, target);    if (result == -1) {        cout << "该数不在数组中!" << endl;    } else {        cout << "元素在数组中的位置为:" << result << endl;    }    delete[] arr;    return 0;}
在这段代码中,我们首先使用冒泡排序算法对输入的数组进行排序,然后根据用户输入的目标数,使用折半查找算法在排序后的数组中查找。如果找到目标数,则输出元素在数组中的位置;如果未找到,则输出提示信息。

希望以上代码和解释能够帮助到你!如有其他疑问,请继续提问。

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-2-24 14:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表