Ranking of array element sorting written in C++
In the given problem, we need to rank all the given elements of the array, the smallest number has the smallest rank and the largest one has the largest rank. For example, we also need to change the ranking of numbers based on their frequency -
Input : 20 30 10 Output : 2.0 3.0 1.0 Input : 10 12 15 12 10 25 12 Output : 1.5, 4.0, 6.0, 4.0, 1.5, 7.0, 4.0 Here the rank of 10 is 1.5 because there are two 10s present in the given array now if we assume they both take different ranks i.e. 1 and 2 and we thus divide it within themselves so their rank becomes 1.5 and 1.5. Input : 1, 2, 5, 2, 1, 60, 3 Output : 1.5, 3.5, 6.0, 3.5, 1.5, 7.0, 5.0
Methods of finding the solution
There are two different ways to find the solution, they are-
Brute Force Method
In this method we will loop, select any particular element and determine its ranking.
Example
#include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 2, 5, 2, 1, 25, 2}; // given array int n = sizeof(arr) / sizeof(arr[0]); // size of our given array float rank[n] = {0}; // our ranking array for (int i = 0; i < n; i++) { int r = 1; // the number of elements greater than arr[i] int s = 1; // the number of elements equal to arr[i] for (int j = 0; j < n; j++) { if (j != i && arr[j] < arr[i]) r += 1; if (j != i && arr[j] == arr[i]) s += 1; } rank[i] = r + (float)(s - 1) / (float) 2; // using formula //to obtain rank of particular element } for (int i = 0; i < n; i++) // outputting the ranks cout << rank[i] << ' '; return 0; }
Output
1.5 4 6 4 1.5 7 4
The time complexity of this program is O(N*N), where N is the given array now size; as you can see, our time complexity is not good, so we will increase its efficiency to fit better with higher constraints.
Efficient method
In this method we will take a new array and sort it, since the array is sorted, now we know that all the elements with the same rank will be together, so Now we rank them as usual and then calculate the rank of the specific element.
Example
#include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 2, 5, 2, 1, 60, 3}; // given array int n = sizeof(arr) / sizeof(arr[0]); // size of our given array float rank[n] = {0}; // our ranking array int old[n]; for(int i = 0; i < n; i++) old[i] = arr[i]; sort(arr, arr+n); // sorting the array int prev = arr[0]; int r = 1; // ranks int s = 0; // frequency int tot = 0; // will stack up all the rank contained by an element map<int, float> rrank; for (int i = 0; i < n; i++) { if(prev == arr[i]) { s++; tot += r; } else { float now = 0; now = (float)tot/s; // dividing the ranks equally rrank[prev] = now; prev = arr[i]; tot = r; s = 1; } r++; } rrank[arr[n-1]] = (float)tot/s; for (int i = 0; i < n; i++) // outputting the ranks cout << rrank[old[i]] << " "; return 0; }
Output
1.5 3.5 6 3.5 1.5 7 5
Explanation of the above code
In this method, we sort the array and then start from the beginning for each Elements are ranked (ranking starts from 1). Now, if our previous element is equal to the current element, we increment s and add to our rank sum. When our element changes, we separate the previous element's ranks, refresh s and the total, and continue with our code.
Conclusion
In this article, we solved a problem to find the ranking of all elements in an array. We also learned a C program to solve this problem and a complete way to solve this problem (normal and efficient). We can write the same program in other languages, such as C, java, python and other languages.
The above is the detailed content of Ranking of array element sorting written in C++. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The 2024CSRankings National Computer Science Major Rankings have just been released! This year, in the ranking of the best CS universities in the United States, Carnegie Mellon University (CMU) ranks among the best in the country and in the field of CS, while the University of Illinois at Urbana-Champaign (UIUC) has been ranked second for six consecutive years. Georgia Tech ranked third. Then, Stanford University, University of California at San Diego, University of Michigan, and University of Washington tied for fourth place in the world. It is worth noting that MIT's ranking fell and fell out of the top five. CSRankings is a global university ranking project in the field of computer science initiated by Professor Emery Berger of the School of Computer and Information Sciences at the University of Massachusetts Amherst. The ranking is based on objective

PHP form processing: form data sorting and ranking In web development, forms are a common user input method. After we collect form data from users, we usually need to process and analyze the data. This article will introduce how to use PHP to sort and rank form data to better display and analyze user-submitted data. 1. Form data sorting When we collect form data submitted by users, we may find that the order of the data does not necessarily meet our requirements. For those that need to be displayed or divided according to specific rules

The ranking and comparison functions of Vue statistical charts are implemented in the field of data visualization. Statistical charts are an intuitive and clear way to display data. As a popular front-end framework, Vue provides a wealth of tools and components to implement various charts. This article will introduce how to use Vue to implement the ranking and comparison functions of statistical charts. Before starting, we need to install Vue and related chart libraries. We will use Chart.js as the charting library, which provides rich chart types and interactive functions. C can be installed via the following command

Fast array sorting method in PHP that preserves key names: Use the ksort() function to sort the keys. Use the uasort() function to sort using a user-defined comparison function. Practical case: To sort an array of user IDs and scores by score while retaining the user ID, you can use the uasort() function and a custom comparison function.

We all know numbers that are not the square of any number, such as 2, 3, 5, 7, 8, etc. There are N non-square numbers, and it is impossible to know every number. So, in this article, we will explain everything about squareless or non-square numbers and ways to find the Nth non-square number in C++. Nth non-square number If a number is the square of an integer, then the number is called a perfect square. Some examples of perfect square numbers are -1issquareof14issquareof29issquareof316issquareof425issquareof5 If a number is not the square of any integer, then the number is called non-square. For example, the first 15 non-square numbers are -2,3,5,6,

To deeply understand JS array sorting: the principles and mechanisms of the sort() method, specific code examples are required. Introduction: Array sorting is one of the very common operations in our daily front-end development work. The array sorting method sort() in JavaScript is one of the most commonly used array sorting methods. However, do you really understand the principles and mechanisms of the sort() method? This article will give you an in-depth understanding of the principles and mechanisms of JS array sorting, and provide specific code examples. 1. Basic usage of sort() method

In this article, we will learn about the reversal algorithm to rotate a given array to the right by k elements, for example −Input:arr[]={4,6,2,6,43,7,3,7},k= 4Output:{43,7,3,7,4,6,2,6}Explanation:Rotatingeachelementofarrayby4-elementtotherightgives{43,7,3,7,4,6,2,6}.Input:arr[]={8 ,5,8,2,1,4,9,3},k=3Output:{4,9,3,8,5,8,2,1} Find the solution

A circle is a closed figure. All points on a circle are equidistant from a point inside the circle. The center point is called the center of the circle. The distance from a point to the center of a circle is called the radius. Area is a quantitative representation of the span of dimensions of a closed figure. The area of a circle is the area enclosed within the dimensions of the circle. The formula to calculate the area of a circle, Area=π*r*r To calculate the area, we give the radius of the circle as input, we will use the formula to calculate the area, algorithm STEP1: Takeradiusasinputfromtheuserusingstdinput.STEP2: Calculatetheareaofcircleusing, area=(
