宝藏排序2(数据范围)

题目


快速排序

java 复制代码
import java.util.Scanner;

//快速排序
public class Main {
	public static void quick_sort(int[] q,int l,int r) {
		if(l>=r)	return ;
		int i = l-1;
		int j = r+1;
		int x = q[(l+r)/2];
		while(i<j) {
			do	i++; while(q[i]<x);
			do	j--; while(q[j]>x);
			if(i<j) {
				int t = q[i];
				q[i] = q[j];
				q[j] = t;
			}
		}
		quick_sort(q, l, j);
		quick_sort(q, j+1, r);
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		int[] q = new int[n];
		for(int i=0;i<n;i++) {
			q[i] = sc.nextInt();
		}
		quick_sort(q,0,n-1);
		for(int i=0;i<n;i++) {
			System.out.print(q[i]+" ");
		}
		sc.close();
	}
}

Arrays的sort方法

java 复制代码
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		sc.nextLine();
		int[] q = new int[n];
		for(int i=0;i<n;i++) {
			q[i] = sc.nextInt();
		}
      	Arrays.sort(q);
	  	for(int i=0;i<n;i++) {
			System.out.print(q[i]+" ");
		}
		sc.close();
	}
}
相关推荐
朦胧之8 小时前
AI 编程-老项目改造篇
java·前端·后端
kisshyshy12 小时前
🍦 雪糕、食堂、火车厢:三幅漫画吃透栈、队列与链表
javascript·算法
程序猿大帅12 小时前
别再只当调包侠了:用 Spring AI 落地 Function Calling,我被大模型硬生生砸出了三个大坑
java
程序员晓琪13 小时前
约定大于配置:基于 Java 包名自动生成 API 版本路由的最佳实践
java·spring boot·后端
Flittly13 小时前
【AgentScope Java新手村系列】(11)中断与恢复
java·spring boot·spring
众少成多积小致巨14 小时前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
东坡白菜14 小时前
破局全栈:前端开发的Java入门实战记录—JPA(2)
java·后端
猿人谷19 小时前
不只是 CPU 阈值:STAR 如何用 GAT + Transformer 做容器级自动扩缩容?
人工智能·算法
SimonKing20 小时前
艹,维护AI写的代码,我心态崩了......
java·后端·程序员
用户2986985301420 小时前
Java Word 文档样式进阶:段落与文本背景色设置完全指南
java·后端