Array.sort(数组名,起始下标,终止下标)(默认升序)
转载:
Sort()函数用法:比较函数写法_Coding-优快云博客
头文件:
#include <algorithm>
用法
Sort函数有三个参数:(第三个参数可不写)
(1)第一个是要排序的数组的起始地址。
(2)第二个是结束的地址
(3)第三个参数是排序的方法,可以是「降序」也可是「升序」,
第三个参数可省略,此时默认的排序方法是「升序」排序。
给vector排序
例子:Leetcode047全排列
vector<vector<int>> permuteUnique(vector<int>& nums) {
temp=vector<int>(nums.size());
st=vector<bool>(nums.size());
//重复数字我们需要先进行排序
sort(nums.begin(),nums.end());
dfs(nums,0);
return ans;
}
两个参数(升序)
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[20]={2,4,1,23,5,76,0,43,24,65},i;
for(i=0;i<20;i++) cout<<a[i]<<endl;
sort(a,a+20);
for(i=0;i<20;i++) cout<<a[i]<<endl;
return 0;
}
三个参数
sort(arr.begin(),arr.end(),compare);
可以通过编写compare函数改变Sort的排序规则。
降序:
bool compare(int a,int b){
return a>b; //降序排列,如果改为return a<b,则为升序
}
int a[20]={2,4,1,23,5,76,0,43,24,65},i;
sort(a,a+20,compare);
绝对值:
#include <cmath>
bool compare(int a, int b){
return abs(a) > abs(b);
}
结构体排序:
struct node{
int u, v, w;
};
bool cmp(node a, node b){
return a.w<b.w;//升序
}
当然cmp函数也可以写的稍微复杂点,也就是说,按照优先级对结构体的多个成员按照某种规则排序
//先按照w的值升序排序,如果w相等,再按照v的值升序排序
bool cmp(node a, node b)
{
if(a.w==b.w)
return a.v<b.v;
else
return a.w<b.w;
}
====java版本====
转载自:
java中sort函数的使用_Yqifei的博客-优快云博客_java中sort方法怎么用
需要用到的包
import java.util.Arrays;
第一种基本格式
Array.sort(数组名,起始下标,终止下标)(默认升序)
例子
import java.util.Arrays;
public class test {
public static void main(String args[]) {
int[] a ={1,8,6,9,4,7,6};
Arrays.sort(a,0,a.length);
for(int i : a){
System.out.print(i+" ");
}
}
}
第二种基本格式
Arrays.sort(数组名)(默认升序,这种情况只适合数组已被初始化)
例子:
import java.util.Arrays;
public class test {
public static void main(String args[]) {
int[] a ={1,8,6,9,4,7,6};
Arrays.sort(a);
for(int i : a){
System.out.print(i+" ");
}
}
}
第三种基本格式cmp
Debug试图解释源码
底层BinarySort()方法
函数的基本格式
int compare (Object o1,Object o2);
传入参数的类型是java中的类
这时的sort函数的格式变成了:
Arrays.sort(数组名,起始下标,终止下标,new cmp());
返回值的基本方法:
int compare(Object o1, Object o2) 返回一个基本类型的整型 如果要按照升序排序, 则o1 小于o2,返回-1(负数),相等返回0,01大于02返回1(正数) 如果要按照降序排序 则o1 小于o2,返回1(正数),相等返回0,01大于02返回-1(负数)
例子
利用匿名内部类实现接口Comparator实现自定义排序
public class Sort {
public static void main(String[] args) {
Integer num []={1,4,3,5,2};//写成int 不行
//自定义排序(匿名内部类实现接口Comparator,要求实现compare()方法)
Arrays.sort(num,new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
//这里是降序
return o2-o1;//返回正负数决定升序还是降序
}
});
System.out.println("排序后数组");
System.out.println(Arrays.toString(num));
}
}
没学会匿名内部类时用到蠢办法
package qiuqiu;
import java.util.*;
import java.util.Arrays;
class node{
int x;
}
class cmp implements Comparator<node>{
public int compare(node a,node b){
if(a.x<b.x)
return 1;
else if(a.x>b.x)
return -1;
else
return 0;
}
}
public class yun{
public static void main(String args[]){
node[] a = new node[10];
Scanner cin = new Scanner(System.in);
System.out.println("请输入10个整数:");
int t=10,i=0;
while(t>0){
a[i] = new node(); // 这里不要漏掉,第19行只是创建10个node对象,这里才是创建具体的空间
a[i].x=cin.nextInt();
i++;
t--;
}
Arrays.sort(a,0,a.length,new cmp());
System.out.println("从小到大排序结果后:");
for (node k :a){
System.out.print(k.x+" ");
}
System.out.print("\n");
}
}