【数字排序】时间复杂度为O(n^2)的遍历;

使用map和遍历的特性,时间复杂度大概是小于等于O(n^2);
代码:```cpp

include

include

using namespace std;
int n;
map<int,int> mp;

int main(){
cin >> n;
for(int i = 0;i < n;i++){
int temp;
cin >> temp;
if(mp.count(temp) > 0){
mp[temp]++;
}else{
mp.insert({temp,1});
}
}
while(mp.size()>0){
pair<int,int> max_num={0,0};
for(auto e:mp){
if(e.second > max_num.second){
max_num = e;
}
}
cout << max_num.first <<" "<<max_num.second<<endl;
mp.erase(max_num.first);
}
}

posted @ 2025-12-01 18:07  q_z_chen  阅读(1)  评论(0)    收藏  举报