Hello, I am trying to figure out an easier way of sorting based on frequency to be specific:
"Sort the alphabet in decreasing order based on the frequency. If two or more symbols have the same frequency, then use the symbols ASCII values (higher ascii value = higher priority).
For context here's an example input:
aaaeeeiiiooouuuuuuuuuuuuuuuuaaeeeeeeeiiiiiiiiiiiiiiiiiiiiiiiiiiiiaaaaa
This would be:
i : 31
u : 16
e : 10
a : 10
o : 3
in descending order. Since 'e' has a higher ascii value of 101 compared to 'a' which has an ascii value of 97 'e' would come before 'a' (They have the same frequency)
The problem is I want to find an easier more simple way to sort this in descending order maybe using the std::sort or/and std::greater, but I'm not sure how I'd do it. Here's some info on what I'm using:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
struct variables{
char symbol;
int freq;
string code;
string* pointerContent;
};
int main(){
struct variables structVariables [128];
//...some functions calls and stuff here
//how to use sort function here?
}
| |
I already have a way of sorting in descending order but it seems a bit repetitive with what I'm doing and it's a bit long, any thoughts? Here's the code I had previously.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
std::map<char, int> priorityMap;
std::map<char, int>::iterator priorityItr;
for(long j = 0; j < (*contents).length(); j++){
priorityMap[(*contents)[j]]++;
}
int mapSize = map.size();
while(mapSize--){
unsigned int currMax = 0;
char maxChar;
for(priorityItr = priorityMap.begin(); priorityItr != priorityMap.end(); ++priorityItr){
if(priorityItr->second > currMax || (priorityItr->second == currMax && priorityItr->first > maxChar)){
maxChar = priorityItr->first;
currMax = priorityItr->second;
}
}
cout << "maxChar: " << maxChar << ", currMax: " << currMax << endl;
priorityMap.erase(maxChar);
}
| |