排序
例2.1 排序
代码 2.1
#include<iostream>
using namespace std;
int main(){
int n,i,j,t;
int m[105];
while(cin >> n){
for(i=0;i<n;i++){
cin >> m[i];
}
for(i=n-1;i>0;i--){
for(j=0;j<i;j++){
if(m[j]>m[j+1]){
t = m[j];
m[j] = m[j+1];
m[j+1] = t;
}
}
}
for(i=0;i<n;i++){
cout << m[i] << " ";
}
cout << endl;
}
return 0;
}
代码 2.2
使用了algorithm头文件中的sort进行快速排序。
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,i,j,t;
int m[105];
while(cin >> n){
for(i=0;i<n;i++){
cin >> m[i];
}
sort(m,m+n);
for(i=0;i<n;i++){
cout << m[i] << " ";
}
cout << endl;
}
return 0;
}
代码 2.3
新定义一个cmp函数,实现对于新的排序规则的定义。(cmp返回值为true时,cmp函数的第一个参数会排在第二个参数之前)
此时调用sort函数的另一种重载方式:sort(排序起始地址,排序
结束地址,比较函数)
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int x,int y){
return x>y;
}
int main(){
int n,i,j,t;
int m[105];
while(cin >> n){
for(i=0;i<n;i++){
cin >> m[i];
}
sort(m,m+n,cmp);
for(i=0;i<n;i++){
cout << m[i] << " ";
}
cout << endl;
}
return 0;
}
例2.2 成绩排序
代码 2.4
创建结构student,建立函数cmp设置sort对student的排序规则。
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
struct student{
char name[105];
int age;
int score;
}s[1005];
bool cmp(student a,student b){
if(a.score != b.score){
return a.score < b.score;
}
if(strcmp(a.name,b.name) != 0){
return strcmp(a.name,b.name) < 0;
}
return a.age < b.age;
}
int main(){
int n,i;
while(cin >> n){
for(i=0;i<n;i++){
cin >> s[i].name >> s[i].age >> s[i].score;
}
sort(s,s+n,cmp);
for(i=0;i<n;i++){
cout << s[i].name << " " << s[i].age << " " << s[i].score << endl;
}
}
return 0;
}
代码2.5
使用重载算符的方法定义结构体之间大小的比较。
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
struct student{
char name[105];
int age;
int score;
bool operator < (const student &b){
if(score != b.score){
return score < b.score;
}
if(strcmp(name,b.name) != 0){
return strcmp(name,b.name) < 0;
}
return age < b.age;
}
}s[1005];
int main(){
int n,i;
while(cin >> n){
for(i=0;i<n;i++){
cin >> s[i].name >> s[i].age >> s[i].score;
}
sort(s,s+n);
for(i=0;i<n;i++){
cout << s[i].name << " " << s[i].age << " " << s[i].score << endl;
}
}
return 0;
}
特殊排序
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,i;
int a[1005];
while(cin >> n){
if(n == 1){
cin >> a[0];
cout << a[0] << endl;
cout << -1 << endl;
}
else{
for(i=0;i<n;i++){
cin >> a[i];
}
sort(a,a+n);
cout << a[n-1] << endl;
for(i=0;i<n-1;i++){
cout << a[i] << " ";
}
cout << endl;
}
}
return 0;
}
EXCEL排序
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
struct student{
char num[10];
char name[10];
int score;
}s[100005];
bool cmp1(student a,student b){
return strcmp(a.num,b.num) < 0;
}
bool cmp2(student a,student b){
if(strcmp(a.name,b.name) != 0){
return strcmp(a.name,b.name) < 0;
}
return strcmp(a.num,b.num) < 0;
}
bool cmp3(student a,student b){
if(a.score != b.score){
return a.score < b.score;
}
return strcmp(a.num,b.num) < 0;
}
int main(){
int N,C,i;
while(cin >> N){
if(N == 0)
break;
cin >> C;
for(i=0;i<N;i++){
cin >> s[i].num >> s[i].name >> s[i].score;
}
if(C == 1){
sort(s,s+N,cmp1);
}
else if(C == 2){
sort(s,s+N,cmp2);
}
else{
sort(s,s+N,cmp3);
}
cout << "Case:" << endl;
for(i=0;i<N;i++){
cout << s[i].num << " " << s[i].name << " " << s[i].score << endl;
}
}
return 0;
}
字符串内排序
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int main(){
string s;
while(cin >> s){
sort(s.begin(), s.begin() + s.length()); //s.end()
cout << s << endl;
}
return 0;
}