一、排序
1、选择排序
void selectSort(){
for(int i=1;i<=n;i++){
int k=i;
for(int j=i;j<=n;j++){
if(A[j]<A[k]){
k=j;
}
}
int temp=A[i];
A[i]=A[k];
A[k]=temp;
}
}
2、插入排序
int A[maxn],n;
void insertSort(){
for(int i=2;i<=n;i++){
int temp=A[i],j=i;
while(j>i&&temp<A[j-1]){
A[j]=A[j-1];
j--;
}
A[j]=temp;
}
}
3、排序题与sort函数的应用
示例:结构体的定义
struct Student{
char name[10];
char id[10];
int score;
int r;
}stu[100010];
cmp函数的编写
bool cmp(Student a,Student b){
if(a.score!=b.score) return a.sort>b.sort;
else return strcmp(a.name,b.name)<0;
}