您好,欢迎访问一九零五行业门户网

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 + 1],那么交换 a[j] 和 a[j + 1]结束如果end forend forexample#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 forexample#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++程序:将数组元素按降序排序的详细内容。
其它类似信息

推荐信息