算法初步01-排序

qsort():c语言

use-method
  • void qsort(基地址,元素个数,元素大小,比较函数);
example1:对int排序
#include<stdio.h>
#include<stdlib.h>

int cmp(const void *a,const void *b){
//	int L = *((int*)a);
//	int R = *((int*)b);
//	if(L<R){
//		return -1;
//	}else if(L == R){
//		return 0;
//	}else{
//		return 1;
//	}
	return *((int*)a)-*((int*)b);
	
}
int main(){
	int a[] = {2,4,5,1,7,3,6,10,22,11};
	int n=10;
	qsort(a,n,sizeof(int),cmp);
	for(int i=0;i<n;++i){
		printf("%d\n",a[i]);
	}
	return 0;
}
example2:对double排序
  • 不能用example1中的return *((int*)a)-*((int*)b);
#include<stdio.h>
#include<stdlib.h>

int cmp(const void *a,const void *b){
	double L = *((double*)a);
	double R = *((double*)b);
	if(L<R){
		return -1;
	}else if(L == R){
		return 0;
	}else{
		return 1;
	}
	
}
int main(){
	double a[] = {2,4,5,1,7,3,6,10,22,11};
	int n=10;
	qsort(a,n,sizeof(double),cmp);
	for(int i=0;i<n;++i){
		printf("%lf\n",a[i]);
	}
	return 0;
}

在这里插入图片描述

example3:对char数组排序
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int cmp(const void *a,const void *b){
	return *((char*)a) - *((char*)b);
	
}
int main(){
	char s[] = "dasfweq";
	qsort(s,strlen(s),sizeof(char),cmp);
	puts(s);
	return 0;
}
example4:对二维char数组排序
 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
int cmp(const void *a,const void *b){
	char *L = *((char**)a);
	char *R = *((char**)b);
	return strcmp(L,R);
}
int main(){
	char *words[10] = {
		(char*)"def",
		(char*)"abc",
		(char*)"gh"
	};
	int n = 3;
	qsort(words,n,sizeof(words[0]),cmp);
	for(int i =0;i<3;++i){
		puts(words[i]);
	}
	return 0;
}
example5:对结构体进行排序
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
	int a;
	int b;
}structTest;
int cmp(const void *a,const void *b){
	int aOfa = ((structTest*)a)->a;
	int aOfb = ((structTest*)b)->a;
	if(aOfa<aOfb){
		return -1;
	}else if(aOfa>aOfb){
		return 1;
	}else{
		int bOfa = ((structTest*)a)->b;
		int bOfb = ((structTest*)b)->b;
		return bOfa-bOfb;
	}
	
}
int main(){
	structTest structArray[5] = {
		{1,2},
		{0,3},
		{2,1},
		{5,1},
		{-1,0}
	};
	int n = sizeof(structArray)/sizeof(structArray[0]);
	qsort(structArray,n,sizeof(structArray[0]),cmp);
	for(int i=0;i<n;++i){
		printf("%d,%d\n",structArray[i].a,structArray[i].b);
	}
	return 0;
}

sort():c++

use-method
  • void sort(基地址,基地址+n);
  • n为数组长度
  • 默认为正序
  • cmp()函数的参数最好加const,保证安全性
  • cmp()函数的参数可以加&变为引用型,增加效率,对于结构体,如果不加引用型会导致复制量过大,所以对于结构体,最好用引用型
example1
#include<iostream>
#include<algorithm>
using namespace std;

int main(){
	int arr[] = {4,5,3,1,8,9,2};//char,double也都一样
	int n = sizeof(arr)/sizeof(arr[0]);
	sort(arr,arr+n);
	for(int i=0;i<n;++i){
		cout<<arr[i]<<endl;
	}
	return 0;
}
example2(逆序):
  • cmp函数返回值为bool
  • cmp函数参数类型可以直接给定
  • 在qsort的cmp中,a>b时,返回为1,相当于true,为正序,而这里是逆序,注意区分
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(double a,double b){
	return a>b;
}
int main(){
	double arr[] = {4.1,5.2,3.5,1.4,8.9,9.1,2.5};
	int n = sizeof(arr)/sizeof(arr[0]);
	sort(arr,arr+n,cmp);
	for(int i=0;i<n;++i){
		cout<<arr[i]<<endl;
	}
	return 0;
}
example3(对字符串排序):
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
bool cmp(const char *a,const char *b){//这里不用const也行
	int result = strcmp(a,b);
	if(result>0){
		return false;
	}
	return true;
}
int main(){
	char *words[10] = {
		(char*)"def",
		(char*)"abc",
		(char*)"gh"
	};
	int n = 3;
	sort(words,words+n,cmp);
	for(int i =0;i<3;++i){
		cout<<words[i]<<endl;
	}
	return 0;
}
example4(对结构体排序):
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef struct{
	int a;
	int b;
}structTest;
bool cmp(const structTest &a,const structTest &b){
	if(a.a>b.a){
		return false;
	}else if(a.a<b.a){
		return true;
	}else{
		if(a.b>b.b){
			return false;
		}else{
			return true;
		}
	}
	
}
int main(){
	structTest structArray[] = {
		{1,2},
		{0,3},
		{1,1},
		{5,1},
		{-1,0}
	};
	int n = sizeof(structArray)/sizeof(structArray[0]);
	sort(structArray,structArray+n,cmp);
	for(int i=0;i<n;++i){
		printf("%d,%d\n",structArray[i].a,structArray[i].b);
	}
	return 0;
}
example5:

https://pintia.cn/problem-sets/994805342720868352/problems/994805468327690240
在这里插入图片描述

  • 有一个示例通过不了,不知为何
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef struct{
	int id;
	char name[9];
	int grade;
}student;
bool cmpOnId(const student &a,const student &b){
	return a.id<b.id;
	
}
bool cmpOnName(const student &a,const student &b){
	int result = strcmp(a.name,b.name);
	if(result == 0){
        return a.id<b.id;
    }else{
        if(result<0){
            return true;
        }else{
            return false;
        }
    }
	
}
bool cmpOnGrade(const student &a,const student &b){
	if(a.grade == b.grade)
        return a.id<b.id;
    else{
        return a.grade<b.grade;
    }
	
}
int main(){
	int n,c;
	student stu[10000];
	while(cin>>n>>c){
		for(int i=0;i<n;++i){
			cin>>stu[i].id>>stu[i].name>>stu[i].grade;
		}
		if(c == 1){
			sort(stu,stu+n,cmpOnId);
		}else if(c==2){
			sort(stu,stu+n,cmpOnName);
		}else if(c == 3){
			sort(stu,stu+n,cmpOnGrade);
		}
		for(int i=0;i<n;++i){
			printf("%06d %s %d\n",stu[i].id,stu[i].name,stu[i].grade);
		}
	}
	
	
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值