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]
- 结束如果
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是输入的大小。通过简单的方式,冒泡排序可以变得更快 检查是否在任何阶段都没有交换时,下一个连续阶段不会发生 改变任何事物。以上是C++程序:将数组元素按降序排序的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

使用foreach循环去除PHP数组中重复元素的方法如下:遍历数组,若元素已存在且当前位置不是第一个出现的位置,则删除它。举例而言,若数据库查询结果存在重复记录,可使用此方法去除,得到不含重复记录的结果。

PHP数组键值翻转方法性能对比表明:array_flip()函数在大型数组(超过100万个元素)下比for循环性能更优,耗时更短。手动翻转键值的for循环方法耗时相对较长。

PHP中深度复制数组的方法包括:使用json_decode和json_encode进行JSON编码和解码。使用array_map和clone进行深度复制键和值的副本。使用serialize和unserialize进行序列化和反序列化。

多维数组排序可分为单列排序和嵌套排序。单列排序可使用array_multisort()函数按列排序;嵌套排序需要递归函数遍历数组并排序。实战案例包括按产品名称排序和按销售量和价格复合排序。

PHP的array_group_by函数可根据键或闭包函数对数组中的元素分组,返回一个关联数组,其中键是组名,值是属于该组的元素数组。

在PHP中执行数组深度复制的最佳实践是:使用json_decode(json_encode($arr))将数组转换为JSON字符串,然后再将其转换回数组。使用unserialize(serialize($arr))将数组序列化为字符串,然后将其反序列化为新数组。使用RecursiveIteratorIterator迭代器对多维数组进行递归遍历。

PHP数组去重算法的复杂度:array_unique():O(n)array_flip()+array_keys():O(n)foreach循环:O(n^2)

PHP的array_group()函数可用于按指定键对数组进行分组,以查找重复元素。该函数通过以下步骤工作:使用key_callback指定分组键。可选地使用value_callback确定分组值。对分组元素进行计数并识别重复项。因此,array_group()函数对于查找和处理重复元素非常有用。
