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

C++程序:将数组元素按降序排序

WBOY
发布: 2023-09-09 19:09:03
转载
2309人浏览过

c++程序:将数组元素按降序排序

在解决一些问题时,以适当的形式排列数据项是一项重要的任务。

efficient way. The element sorting problem is one of the most commonly discussed 排列问题。在本文中,我们将看到如何排列数组元素 按照它们的值降序排列(在C++中)。

在这个领域中有许多不同的排序算法用于对数字或非数字进行排序

按给定顺序的元素。在本文中,我们将只介绍两种简单的方法 sorting. The bubble sort and the selection sort. Let us see them one by one with proper 算法和C++实现代码。

使用冒泡排序技术按降序对数组进行排序

冒泡排序技术是一种最常见且较简单的排序方法。

数组中的元素。此方法检查两个相邻的元素,如果它们是正确的 order, then skip to the next elements, otherwise interchange them to place them in correct 将其它元素顺序排列,然后跳过到下一个元素,否则交换它们以将其放置在正确位置 order. Then move towards right and do the same for the other pair of values. The bubble 按顺序排列。然后向右移动,并对另一对值执行相同操作。气泡 排序技术有几个阶段,在每个阶段结束时,一个元素被放置在 正确的预期位置。让我们来看一下冒泡排序技术的算法。

算法

  • 读取数组A及其大小n作为输入
  • 对于i从0到n-1的范围,执行
    • 对于 j 从 0 到 n - 2 的范围,执行
      • 如果 A[j]
      • 交换 A[j] 和 A[j + 1]
  • 结束如果
  • end for
  • end for
  • Example

    #include <iostream>
    using namespace std;
    void display( int arr[], int n ){
       for ( int i = 0; i < n; i++ ) {
          cout << arr[i] << ", ";
       }
    }
    void swap ( int &a, int &b ){
       int temp = a;
       a = b;
       b = temp;
    }
    void solve( int arr[], int n ){
       int i, j;
       for ( i = 0; i < n; i++ ) {
          for ( j = 0; j < n-1; j++ ) {
             if ( arr[j] < arr[ j+1 ] ) {
                swap( arr[j], arr[ j + 1 ] );
             }
          }
       }
    }
    int main(){
       int arr[] = {8, 45, 74, 12, 10, 36, 58, 96, 5, 2, 78, 44, 25, 12, 89, 95, 63, 84};
       int n = sizeof( arr ) / sizeof( arr[0] );
       cout << "Array before sorting: ";
       display(arr, n);
       solve( arr, n );
       cout << "\nArray After sorting: ";
       display(arr, n);
    }
    
    登录后复制

    输出

    Array before sorting: 8, 45, 74, 12, 10, 36, 58, 96, 5, 2, 78, 44, 25, 12, 89, 95, 63, 84, 
    Array After sorting: 96, 95, 89, 84, 78, 74, 63, 58, 45, 44, 36, 25, 12, 12, 10, 8, 5, 2, 
    
    登录后复制

    使用选择排序技术将数组按降序排序

    在选择排序技术中,我们找到最小元素或最大元素 从给定数组中的索引i开始,翻译为中文:element from the given array starting from index i to the end of this array. Assume we are. 找到最大元素。在每个阶段中,它从索引i到末尾找到最小值,然后 将元素放置在其所需的位置,然后再次搜索下一个最大元素 the index i + 1 and so on. After completing these phases, the entire array will be sorted 索引 i + 1 等等。完成这些阶段后,整个数组将被排序 相应地。

    算法

    • 读取数组A及其大小n作为输入
    • 对于i从0到n-1的范围,执行
      • ind := 从 i 到 n 中 A 的最大元素的索引
      • 如果 A[ i ]
      • 交换 A[ i ] 和 A[ ind ]
    • 结束如果
  • end for
  • Example

    #include <iostream>
    using namespace std;
    void display( int arr[], int n ){
       for ( int i = 0; i < n; i++ ) {
          cout << arr[i] << ", ";
       }
    }
    void swap ( int &a, int &b ){
       int temp = a;
       a = b;
       b = temp;
    }
    int max_index( int arr[], int n, int s, int e ){
       int max = 0, max_ind = 0;
       for ( int i = s; i < e; i++ ) {
          if ( arr[i] > max ) {
             max = arr[i];
             max_ind = i;
          }
       }
       return max_ind;
    }
    void solve( int arr[], int n ){
       int i, j, ind;
       for ( i = 0; i < n; i++ ) {
          ind = max_index( arr, n, i, n );
          if ( arr[i] < arr[ ind ] ) {
             swap( arr[i], arr[ ind ] );
          }
       }
    }
    int main(){
       int arr[] = {8, 45, 74, 12, 10, 36, 58, 96, 5, 2, 78, 44, 25, 12,89, 95, 63, 84};
       int n = sizeof( arr ) / sizeof( arr[0] );
       cout << "Array before sorting: ";
       display(arr, n);
       solve( arr, n );
       cout << "\nArray After sorting: ";
       display(arr, n);
    }
    
    登录后复制

    输出

    Array before sorting: 8, 45, 74, 12, 10, 36, 58, 96, 5, 2, 78, 44, 25, 12, 89, 95, 63, 84, 
    Array After sorting: 96, 95, 89, 84, 78, 74, 63, 58, 45, 44, 36, 25, 12, 12, 10, 8, 5, 2,
    
    登录后复制

    结论

    排序问题是一个基本问题,我们在其中排列数字或其他值

    立即学习C++免费学习笔记(深入)”;

    在给定的排列逻辑中。在这里有许多不同的排序技术可用 理解和实现 实现和易于理解。这两种方法是冒泡排序技术和 选择排序技术。使用这两种方法,我们已经对数据集进行了排序 降序(非递增)排序。这两种排序方法在效率上并不高 尊重时间,但它们很容易理解。这两种方法都需要O(n2)的时间 时间量,其中n是输入的大小。通过简单的方式,冒泡排序可以变得更快 检查是否在任何阶段都没有交换时,下一个连续阶段不会发生 改变任何事物。

    以上就是C++程序:将数组元素按降序排序的详细内容,更多请关注php中文网其它相关文章!

    c++速学教程(入门到精通)
    c++速学教程(入门到精通)

    c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

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

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