为什么不能用快速排序?
  • 板块P1104 生日
  • 楼主lxl2012
  • 当前回复4
  • 已保存回复4
  • 发布时间2024/1/28 21:44
  • 上次更新2024/1/29 08:29:10
查看原帖
为什么不能用快速排序?
1047961
lxl2012楼主2024/1/28 21:44

快速排序:

#include<bits/stdc++.h>
using namespace std;
int n;
struct student{
    int years,months,days,id;
    char name[20];
};
student a[110];
bool operator >(student s1,student s2){
    if(s1.years != s2.years) return s1.years > s2.years;
    if(s1.months != s2.months) return s1.months > s2.months;
    if(s1.days != s2.days) return s1.days > s2.days;
    return s1.id < s2.id;
}
bool operator <(student s1,student s2){
    if(s1.years != s2.years) return s1.years < s2.years;
    if(s1.months != s2.months) return s1.months < s2.months;
    if(s1.days != s2.days) return s1.days < s2.days;
    return s1.id > s2.id;
}
int randint(int l,int r){
	return rand() % (r - l + 1) + 1;
}
void quick(int l,int r){
	if(l >= r) return;
	int i = l,j = r;
	int s = randint(l,r);
	swap(a[l],a[s]);
	student tmp = a[l];
	while(i < j){
		while(i < j && a[j] > tmp) -- j;
		a[i] = a[j];
		while(i < j && a[i] < tmp) ++ i;
		a[j] = a[i];
	}
	a[i] = tmp;
	quick(l,i - 1);
	quick(i + 1,r);
}
int main(){
	cin>>n;
	for(int i = 1;i <= n;i ++){
		cin>>a[i].name>>a[i].years>>a[i].months>>a[i].days;
		a[i].id = i;
	}
	quick(1,n);
	for(int i = 1;i <= n;i ++){
		cout<<a[i].name<<endl;
	}
	return 0;
}

快速排序记录

#include<bits/stdc++.h>
using namespace std;
int n;
struct student{
    int years,months,days,id;
    char name[20];
};
student a[110];
bool operator >(student s1,student s2){
    if(s1.years != s2.years) return s1.years > s2.years;
    if(s1.months != s2.months) return s1.months > s2.months;
    if(s1.days != s2.days) return s1.days > s2.days;
    return s1.id < s2.id;
}
bool operator <(student s1,student s2){
    if(s1.years != s2.years) return s1.years < s2.years;
    if(s1.months != s2.months) return s1.months < s2.months;
    if(s1.days != s2.days) return s1.days < s2.days;
    return s1.id > s2.id;
}
void maopao(student *c,int n){
	for(int i = 1;i <= n - 1;i ++){
		bool b = true;
		for(int j = 1;j <= n - i + 1;j ++){
			if(*(c + j) < *(c + j - 1)){
				swap(*(c + j),*(c + j - 1));
				b = false;
			}
		}
		if(b){
			break;
		}
	}
}
int main(){
	cin>>n;
	for(int i = 1;i <= n;i ++){
		cin>>a[i].name>>a[i].years>>a[i].months>>a[i].days;
		a[i].id = i;
	}
	maopao(&a[0],n);
	for(int i = 1;i <= n;i ++){
		cout<<a[i].name<<endl;
	}
	return 0;
}

冒泡排序记录

2024/1/28 21:44
加载中...