首页 > 文章列表 > C++程序:对数组元素进行升序排序

C++程序:对数组元素进行升序排序

数组 排序 升序
321 2023-09-03

为了有效地解决一些问题,将数据项排列在正确的位置非常重要 顺序。最流行的排列问题之一是元素排序问题。这 本文将演示如何在 C++ 中按升序排列数组成员(根据 值不断上升)。

要按特定顺序排列数字或非数字元素,有多种方法 排序算法可用于该领域。只需两种简单的排序技术即可 将在本文中介绍。选择排序和冒泡排序。让我们逐一检查一下 单独使用适当的技术和 C++ 实现代码。

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

对数组组件进行排序的最流行和最直接的方法之一是 冒泡排序方法。在此方法中,依次检查两个元素以 看看它们的顺序是否正确。如果不是,该方法会交换元素,直到它们 顺序正确。之后,向右移动并对另一组重复该过程 的价值观。单个元素在结束时被放置在正确的预期位置 冒泡分选技术的几个阶段的每个阶段。看看冒泡排序 算法。

算法

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

示例

#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: 2, 5, 8, 10, 12, 12, 25, 36, 44, 45, 58, 63, 74, 78, 84, 89, 95, 96, 

使用选择排序技术按升序对数组进行排序

当使用选择排序策略时,我们从索引 I 开始一直到结束 给定数组,找到最小或最大元素。假设我们是 揭示每一种成分。它定位从索引 I 到末尾的最小元素 每个阶段,将元素放置在适当的位置,然后重复该过程 从索引 I + 1 中查找下一个最大元素,依此类推。这些阶段即将完成, 然后整个数组将被适当排序。

算法

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

示例

#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 min_index( int arr[], int n, int s, int e ){
   int min = 99999, min_ind = -1;
   for ( int i = s; i < e; i++ ) {
      if ( arr[i] < min ) {
         min = arr[i];
         min_ind = i;
      }
   }
   return min_ind;
}
void solve( int arr[], int n ){
   int i, j, ind;
   for ( i = 0; i < n; i++ ) {
      ind = min_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: 2, 5, 8, 10, 12, 12, 25, 36, 44, 45, 58, 63, 74, 78, 84, 89, 95, 96, 

结论

一个基本问题是排序,它涉及根据顺序排列数字或其他项目 预定的布局逻辑。该领域还有许多其他可用的排序技术, 但在这篇文章中,我们将重点关注两个易于使用和理解的。这两个 排序技术有选择排序技术和冒泡排序技术。我们有 使用这两种技术按升序(非降序)排列数据集。 虽然时间效率不是很高,但这两种排序技术很简单。两者的 这两种技术需要 O(n2) 的时间投入,其中 n 是 输入。只要判断是否有变化,后续阶段就不会发生变化 任何阶段都没有交换,可以使冒泡排序更快。