C++面向对象程序设计 033:排序,又见排序! ---- (北大Mooc)
发布日期:2021-06-30 22:27:51 浏览次数:2 分类:技术文章

本文共 1051 字,大约阅读时间需要 3 分钟。

文章目录


专题博客链接


原题题目

在这里插入图片描述

#include 
using namespace std;bool Greater2(int n1,int n2) {
return n1 > n2;}bool Greater1(int n1,int n2) {
return n1 < n2;}bool Greater3(double d1,double d2){
return d1 < d2;}template
void mysort(// 在此处补充你的代码#define NUM 5int main(){
int an[NUM] = {
8,123,11,10,4 }; mysort(an,an+NUM,Greater1); //从小到大排序 for( int i = 0;i < NUM; i ++ ) cout << an[i] << ","; mysort(an,an+NUM,Greater2); //从大到小排序 cout << endl; for( int i = 0;i < NUM; i ++ ) cout << an[i] << ","; cout << endl; double d[6] = {
1.4,1.8,3.2,1.2,3.1,2.1}; mysort(d+1,d+5,Greater3); //将数组从下标1到下标4从小到大排序 for( int i = 0;i < 6; i ++ ) cout << d[i] << ","; return 0;}

代码实现

T1* startptr,T1* endptr,T2* judge){
T1 temp; for(T1* i=startptr;i<=endptr-2;i++) {
for(T1* j=i+1;j<=endptr-1;j++) {
if(!(*judge)(*i,*j)) {
temp = *i; *i = *j; *j = temp; } } }}

转载地址:https://love6.blog.csdn.net/article/details/115008682 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:C++面向对象程序设计 034:goodcopy ---- (北大Mooc)
下一篇:C++面向对象程序设计 032:这个模板并不难 ---- (北大Mooc)

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月17日 16时41分42秒