Arranging data items in a proper form is an important task when solving some problems.
efficient way. The element sorting problem is one of the most commonly discussed Arrangement problem. In this article we will see how to arrange array elements Sort in descending order of their values (in C).There are many different sorting algorithms used in this field to sort numbers or non-numbers
elements in a given order. In this article, we will introduce only two simple methods sorting. The bubble sort and the selection sort. Let us see them one by one with proper Algorithm and C implementation code.Bubble sorting technology is one of the most common and simple sorting methods.
elements in the array. This method checks two adjacent elements if they are correct order, then skip to the next elements, otherwise interchange them to place them in correct Arrange the other elements in order and then skip to the next element, otherwise swap them to place them in the correct position order. Then move towards right and do the same for the other pair of values. The bubble Arranged in order. Then move to the right and do the same with the other pair of values. bubble The sorting technique has several stages, at the end of each stage an element is placed in Correct expected position. Let’s take a look at the algorithm of bubble sort technique.#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,
#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,
A sorting problem is a fundamental problem in which we arrange numbers or other values
in a given permutation logic. There are many different sorting techniques available here understand and implement Implemented and easy to understand. These two methods are bubble sorting technique and Selection sorting techniques. Using these two methods, we have sorted the dataset Descending (non-increasing) sort. These two sorting methods are not very efficient Respect the time, but they are easy to understand. Both methods require O(n2) time Amount of time, where n is the size of the input. Bubble sort can be made faster with simple way Check if there is no swapping in any phase, the next consecutive phase will not happen Change anything.The above is the detailed content of C++ program: Sort array elements in descending order. For more information, please follow other related articles on the PHP Chinese website!