C++ program: Sort array elements in descending order
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.Sort the array in descending order using bubble sort technique
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.algorithm
- Read array A and its size n as input
- For the range of i from 0 to n-1, execute
- For the range of j from 0 to n - 2, execute
- If A[j]
- Exchange A[j] and A[j 1]
- End if
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); }
Output
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,
Use selection sort technique to sort the array in descending order
In selection sort technique we find minimum element or maximum element Starting from index i in the given array, translated into Chinese: element from the given array starting from index i to the end of this array. Assume we are. Find the largest element. In each stage it finds the minimum value from index i to the end, then Place the element where it needs to be and search again for the next largest element the index i 1 and so on. After completing these phases, the entire array will be sorted index i 1 and so on. After completing these stages, the entire array will be sorted Correspondingly.algorithm
- Read array A and its size n as input
- For the range of i from 0 to n-1, execute
- ind := Index of the largest element of A from i to n
- If A[ i ]
- Exchange A[ i ] and A[ ind ]
- End if
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); }
Output
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,
in conclusion
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!

The DOM and SAX methods can be used to parse XML data in C. 1) DOM parsing loads XML into memory, suitable for small files, but may take up a lot of memory. 2) SAX parsing is event-driven and is suitable for large files, but cannot be accessed randomly. Choosing the right method and optimizing the code can improve efficiency.

C is widely used in the fields of game development, embedded systems, financial transactions and scientific computing, due to its high performance and flexibility. 1) In game development, C is used for efficient graphics rendering and real-time computing. 2) In embedded systems, C's memory management and hardware control capabilities make it the first choice. 3) In the field of financial transactions, C's high performance meets the needs of real-time computing. 4) In scientific computing, C's efficient algorithm implementation and data processing capabilities are fully reflected.

C is not dead, but has flourished in many key areas: 1) game development, 2) system programming, 3) high-performance computing, 4) browsers and network applications, C is still the mainstream choice, showing its strong vitality and application scenarios.

The main differences between C# and C are syntax, memory management and performance: 1) C# syntax is modern, supports lambda and LINQ, and C retains C features and supports templates. 2) C# automatically manages memory, C needs to be managed manually. 3) C performance is better than C#, but C# performance is also being optimized.

You can use the TinyXML, Pugixml, or libxml2 libraries to process XML data in C. 1) Parse XML files: Use DOM or SAX methods, DOM is suitable for small files, and SAX is suitable for large files. 2) Generate XML file: convert the data structure into XML format and write to the file. Through these steps, XML data can be effectively managed and manipulated.

Working with XML data structures in C can use the TinyXML or pugixml library. 1) Use the pugixml library to parse and generate XML files. 2) Handle complex nested XML elements, such as book information. 3) Optimize XML processing code, and it is recommended to use efficient libraries and streaming parsing. Through these steps, XML data can be processed efficiently.

C still dominates performance optimization because its low-level memory management and efficient execution capabilities make it indispensable in game development, financial transaction systems and embedded systems. Specifically, it is manifested as: 1) In game development, C's low-level memory management and efficient execution capabilities make it the preferred language for game engine development; 2) In financial transaction systems, C's performance advantages ensure extremely low latency and high throughput; 3) In embedded systems, C's low-level memory management and efficient execution capabilities make it very popular in resource-constrained environments.

The choice of C XML framework should be based on project requirements. 1) TinyXML is suitable for resource-constrained environments, 2) pugixml is suitable for high-performance requirements, 3) Xerces-C supports complex XMLSchema verification, and performance, ease of use and licenses must be considered when choosing.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment
