首页 > 后端开发 > C++ > 正文

重新排列一个数组,使得每个奇数索引的元素都大于其前一个元素

WBOY
发布: 2023-09-01 09:45:07
转载
1255人浏览过

重新排列一个数组,使得每个奇数索引的元素都大于其前一个元素

我们有一个正整数类型的数组,假设为arr[],大小任意。任务是重新排列数组,使得所有奇数索引位置的元素的值大于偶数索引位置的元素,并打印结果。

让我们看看各种输入输出情况:

输入 − int arr[] = {2, 1, 5, 4, 3, 7, 8}

输出 − 排列前的数组:2 1 5 4 3 7 8 将数组重新排列,使得每个奇数索引的元素都大于其前一个元素:1 4 2 5 3 8 7

解释  - 我们给定一个大小为7的整数数组。现在,如果偶数索引的元素较大,我们将交换偶数索引处的元素和奇数索引处的元素

Arr[0] > arr[1] = call swap = {1, 2, 5, 4, 3, 7, 8}
Arr[2] > arr[3] = call swap = {1, 2, 4, 5, 3, 7, 8}
Arr[6] > arr[5] = call swap = {1, 2, 4, 5, 3, 8, 7}
Arr[2] > arr[1] = call swap = {1, 4, 2, 5, 3, 8, 7}
登录后复制

输入− int arr[] = {3, 2, 6, 9}

输出− 排列前的数组: 3 2 6 9 Rearrangement of an array such that every odd indexed element is greater than it previous is: 2 3 6 9

Explanation − we are given an integer array of size 4. Now, we will swap the elements at even index with the elements at odd index if even indexed elements are greater i.e. Arr[0] > arr[1] = call swap = {2, 3, 6, 9}. No need to further call the swap method as all the elements at positions satisfies the conditions

Approach used in the below program is as follows

  • Input an array of integer type elements and calculate the size of an array.

  • Print the array before arrangement and call the function Rearrangement(arr, size)

  • Inside the function Rearrangement(arr, size)

    新CG儿
    新CG儿

    数字视觉分享平台 | AE模板_视频素材

    新CG儿 147
    查看详情 新CG儿
    • Create a variable of integer type let’s say, ptr and set it with size-1.

    • Start loop FOR, from i to 0 till i less than ptr and i = i + 1. Inside the loop, check if arr[i] is greater than arr[i+1] then call swap(arr[i], arr[i+1]).

    • Check IF size & 1 then start loop FOR from i to ptr till i greater than 0 and i = i - 2. Inside the loop, check IF arr[i] greater than arr[i - 1] then call swap(arr[i], arr[i-1])

  • Print the array after the rearrangement of values of an array.

Example

#include <iostream>
using namespace std;
void Rearrangement(int arr[], int size){
   int ptr = size - 1;
   for(int i = 0; i < ptr; i = i+2){
      if(arr[i] > arr[i+1]){
         swap(arr[i], arr[i+1]);
      }
   }
   if(size & 1){
      for(int i = ptr; i > 0; i = i-2){
         if(arr[i] > arr[i-1]){
            swap(arr[i], arr[i-1]);
         }
      }
   }
}
int main(){
   //input an array
   int arr[] = {2, 1, 5, 4, 3, 7, 8};
   int size = sizeof(arr) / sizeof(arr[0]);
   //print the original Array
   cout<<"Array before Arrangement: ";
   for (int i = 0; i < size; i++){
      cout << arr[i] << " ";
   }
   //calling the function to rearrange the array
   Rearrangement(arr, size);
   //print the array after rearranging the values
   cout<<"\nRearrangement of an array such that every odd indexed element is greater than it previous is: ";
   for(int i = 0; i < size; i++){
      cout<< arr[i] << " ";
   }
   return 0;
}
登录后复制

输出

如果我们运行上述代码,将会生成以下输出

Array before Arrangement: 2 1 5 4 3 7 8
Rearrangement of an array such that every odd indexed element is greater than it previous is: 1 4 2 5 3 8 7
登录后复制

以上就是重新排列一个数组,使得每个奇数索引的元素都大于其前一个元素的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号