首页 > 文章列表 > C++程序:将数组元素按降序排序

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

数组 排序 降序
277 2023-09-11

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

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 + 1],那么
        • 交换 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[ ind ],那么
      • 交换 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,

结论

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

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