鱼C论坛

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

[已解决]c++

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

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

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

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

  1. #include <iostream>
  2. using namespace std;

  3. void bubbleSort(int arr[], int size) {
  4.     for (int i = 0; i < size - 1; i++) {
  5.         for (int j = 0; j < size - i - 1; j++) {
  6.             if (arr[j] > arr[j + 1]) {
  7.                 int temp = arr[j];
  8.                 arr[j] = arr[j + 1];
  9.                 arr[j + 1] = temp;
  10.             }
  11.         }
  12.     }
  13. }

  14. int binarySearch(int arr[], int size, int target) {
  15.     int left = 0;
  16.     int right = size - 1;
  17.    
  18.     while (left <= right) {
  19.         int mid = (left + right) / 2;
  20.         
  21.         if (arr[mid] == target) {
  22.             return mid;
  23.         }
  24.         
  25.         if (arr[mid] < target) {
  26.             left = mid + 1;
  27.         } else {
  28.             right = mid - 1;
  29.         }
  30.     }
  31.    
  32.     return -1;
  33. }

  34. int main() {
  35.     const int size = 10;
  36.     int arr[size];
  37.    
  38.     // 随机输入一组数组元素值
  39.     cout << "请输入" << size << "个整数:" << endl;
  40.     for (int i = 0; i < size; i++) {
  41.         cin >> arr[ i];
  42.     }
  43.    
  44.     // 使用冒泡排序使数组有序
  45.     bubbleSort(arr, size);
  46.    
  47.     // 随机输入一个数
  48.     int target;
  49.     cout << "请输入要查找的数:" << endl;
  50.     cin >> target;
  51.    
  52.     // 使用折半查找法在数组中查找
  53.     int index = binarySearch(arr, size, target);
  54.    
  55.     // 输出结果
  56.     if (index != -1) {
  57.         cout << "元素在数组中的位置为:" << index << endl;
  58.     } else {
  59.         cout << "元素不在数组中" << endl;
  60.     }
  61.    
  62.     return 0;
  63. }
复制代码


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

使用道具 举报

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

  1. #include <iostream>
  2. using namespace std;

  3. void bubbleSort(int arr[], int size) {
  4.     for (int i = 0; i < size - 1; i++) {
  5.         for (int j = 0; j < size - i - 1; j++) {
  6.             if (arr[j] > arr[j + 1]) {
  7.                 int temp = arr[j];
  8.                 arr[j] = arr[j + 1];
  9.                 arr[j + 1] = temp;
  10.             }
  11.         }
  12.     }
  13. }

  14. int binarySearch(int arr[], int size, int target) {
  15.     int left = 0;
  16.     int right = size - 1;
  17.    
  18.     while (left <= right) {
  19.         int mid = (left + right) / 2;
  20.         
  21.         if (arr[mid] == target) {
  22.             return mid;
  23.         }
  24.         
  25.         if (arr[mid] < target) {
  26.             left = mid + 1;
  27.         } else {
  28.             right = mid - 1;
  29.         }
  30.     }
  31.    
  32.     return -1;
  33. }

  34. int main() {
  35.     const int size = 10;
  36.     int arr[size];
  37.    
  38.     // 随机输入一组数组元素值
  39.     cout << "请输入" << size << "个整数:" << endl;
  40.     for (int i = 0; i < size; i++) {
  41.         cin >> arr[ i];
  42.     }
  43.    
  44.     // 使用冒泡排序使数组有序
  45.     bubbleSort(arr, size);
  46.    
  47.     // 随机输入一个数
  48.     int target;
  49.     cout << "请输入要查找的数:" << endl;
  50.     cin >> target;
  51.    
  52.     // 使用折半查找法在数组中查找
  53.     int index = binarySearch(arr, size, target);
  54.    
  55.     // 输出结果
  56.     if (index != -1) {
  57.         cout << "元素在数组中的位置为:" << index << endl;
  58.     } else {
  59.         cout << "元素不在数组中" << endl;
  60.     }
  61.    
  62.     return 0;
  63. }
复制代码


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

使用道具 举报

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

  1. #include <iostream>
  2. using namespace std;

  3. void bubbleSort(int arr[], int n) {
  4.     for(int i = 0; i < n-1; i++) {
  5.         for(int j = 0; j < n-i-1; j++) {
  6.             if(arr[j] > arr[j+1]) {
  7.                 swap(arr[j], arr[j+1]);
  8.             }
  9.         }
  10.     }
  11. }

  12. int binarySearch(int arr[], int l, int r, int x) {
  13.     if (r >= l) {
  14.         int mid = l + (r - l) / 2;
  15.         if (arr[mid] == x)
  16.             return mid;
  17.         if (arr[mid] > x)
  18.             return binarySearch(arr, l, mid - 1, x);
  19.         return binarySearch(arr, mid + 1, r, x);
  20.     }
  21.     return -1;
  22. }

  23. int main() {
  24.     int n;
  25.     cout << "请输入数组的大小: ";
  26.     cin >> n;
  27.     int arr[n];
  28.     cout << "请输入数组元素: ";
  29.     for(int i = 0; i < n; i++) {
  30.         cin >> arr[ i];
  31.     }
  32.     bubbleSort(arr, n);
  33.     cout << "排序后的数组是: ";
  34.     for(int i = 0; i < n; i++) {
  35.         cout << arr[ i] << " ";
  36.     }
  37.     cout << "\
  38. 请输入要查找的元素: ";
  39.     int x;
  40.     cin >> x;
  41.     int result = binarySearch(arr, 0, n - 1, x);
  42.     (result == -1) ? cout << "元素不在数组中"
  43.                    : cout << "元素在数组中的位置: " << result;
  44.     return 0;
  45. }
复制代码


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

使用道具 举报

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

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

解决方案:

以下是基于你的问题描述所修改的代码:
  1. #include <iostream>
  2. using namespace std;
  3. // 冒泡排序
  4. void bubbleSort(int arr[], int length) {
  5.     for (int i = 0; i < length - 1; i++) {
  6.         for (int j = 0; j < length - i - 1; j++) {
  7.             if (arr[j] > arr[j + 1]) {
  8.                 swap(arr[j], arr[j + 1]);
  9.             }
  10.         }
  11.     }
  12. }
  13. // 折半查找
  14. int binarySearch(int arr[], int length, int target) {
  15.     int left = 0;
  16.     int right = length - 1;
  17.     while (left <= right) {
  18.         int mid = left + (right - left) / 2;
  19.         if (arr[mid] == target) {
  20.             return mid; // 找到目标元素,返回索引位置
  21.         } else if (arr[mid] < target) {
  22.             left = mid + 1; // 目标元素在右半部分
  23.         } else {
  24.             right = mid - 1; // 目标元素在左半部分
  25.         }
  26.     }
  27.     return -1; // 未找到目标元素
  28. }
  29. int main() {
  30.     int n;
  31.     cout << "请输入数组长度:";
  32.     cin >> n;
  33.     int* arr = new int[n];
  34.     cout << "请输入数组元素值:";
  35.     for (int i = 0; i < n; i++) {
  36.         cin >> arr[i];
  37.     }
  38.     // 冒泡排序
  39.     bubbleSort(arr, n);
  40.     int target;
  41.     cout << "请输入要查找的数:";
  42.     cin >> target;
  43.     // 折半查找
  44.     int result = binarySearch(arr, n, target);
  45.     if (result == -1) {
  46.         cout << "该数不在数组中!" << endl;
  47.     } else {
  48.         cout << "元素在数组中的位置为:" << result << endl;
  49.     }
  50.     delete[] arr;
  51.     return 0;
  52. }
复制代码

在这段代码中,我们首先使用冒泡排序算法对输入的数组进行排序,然后根据用户输入的目标数,使用折半查找算法在排序后的数组中查找。如果找到目标数,则输出元素在数组中的位置;如果未找到,则输出提示信息。

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

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-20 14:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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