快排
基于分治的思想
模板题:
给定你一个长度为 n的整数数列。
请你使用快速排序对这个数列按照从小到大进行排序。
并将排好序的数列按顺序输出。
输入格式
输入共两行,第一行包含整数 n。
第二行包含 n个整数(所有整数均在 1∼109 范围内),表示整个数列。
输出格式
输出共一行,包含 n个整数,表示排好序的数列。
数据范围
1≤n≤100000
输入样例:
5
3 1 2 4 5
输出样例:
1 2 3 4 5
输入样例:
2
2 1
输出样例:
1 2
答案:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void quick_sort(int[] a, int begin, int end){
if(begin>=end) return;
int x = a[begin], t = 0;
int i = begin-1, j = end+1;
while (i<j){
while (a[++i]<x);
while (a[--j]>x);
if(i<j){
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
quick_sort(a,begin,j);
quick_sort(a,j+1,end);
}
public static void main(String[] args) throws Exception{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.valueOf(bufferedReader.readLine());
int[] a = new int[n];
String[] str = bufferedReader.readLine().split(" ");
for (int i=0;i<n;i++){
a[i] = Integer.parseInt(str[i]);
}
quick_sort(a,0,n-1);
for (int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
}
例题:第k个数:
给定一个长度为 n的整数数列,以及一个整数 k,请用快速选择算法求出数列从小到大排序后的第 k个数。
输入格式
第一行包含两个整数 n 和 k。
第二行包含 n个整数(所有整数均在 1∼109 范围内),表示整数数列。
输出格式
输出一个整数,表示数列的第 k小数。
数据范围
1≤n≤100000,
1≤k≤n
输入样例:
5 3
2 4 1 5 3
输出样例:
3
答案:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void quick_sort(int[] a, int begin, int end){
if(begin>=end) return;
int x = a[begin], t = 0;
int i = begin-1, j = end+1;
while (i<j){
while (a[++i]<x);
while (a[--j]>x);
if(i<j){
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
quick_sort(a,begin,j);
quick_sort(a,j+1,end);
}
public static void main(String[] args) throws Exception{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String[] str = bufferedReader.readLine().split(" ");
int n = Integer.valueOf(str[0]);
int k = Integer.valueOf(str[1]);
int[] a = new int[n];
str = bufferedReader.readLine().split(" ");
for (int i=0;i<n;i++){
a[i] = Integer.parseInt(str[i]);
}
quick_sort(a,0,n-1);
System.out.println(a[k-1]);
}
}
归并排序
基于分治的思想
模板题:
给定你一个长度为 n的整数数列。
请你使用归并排序对这个数列按照从小到大进行排序。
并将排好序的数列按顺序输出。
输入格式
输入共两行,第一行包含整数 n。
第二行包含 n个整数(所有整数均在 1∼109范围内),表示整个数列。
输出格式
输出共一行,包含 n个整数,表示排好序的数列。
数据范围
1≤n≤100000
输入样例:
5
3 1 2 4 5
输出样例:
1 2 3 4 5
答案:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void mergeSort(int[] a, int begin, int end){
if(begin>=end) return;
int mid = (begin+end)/2;
mergeSort(a,begin,mid);
mergeSort(a,mid+1,end);
merge(a,begin,end,mid);
}
public static void merge(int[] a, int i, int j, int k){
int n = j-i+1;
int[] c = new int[n];
int x = i, y = k+1, t = 0;
while (x<=k&&y<=j){
if(a[x]<a[y]) c[t++] = a[x++];
else c[t++] = a[y++];
}
while (x<=k) c[t++] = a[x++];
while (y<=j) c[t++] = a[y++];
for (t=0;t<n;t++){
a[i++] = c[t];
}
}
public static void main(String[] args) throws Exception{
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String[] str = bufferedReader.readLine().split(" ");
int n = Integer.valueOf(str[0]);
int[] a = new int[n];
str = bufferedReader.readLine().split(" ");
for (int i=0;i<n;i++){
a[i] = Integer.parseInt(str[i]);
}
mergeSort(a,0,n-1);
for (int i=0;i<n;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
}
例题:逆序对的数量:
给定一个长度为 n的整数数列,请你计算数列中的逆序对的数量。
逆序对的定义如下:对于数列的第 i个和第 j个元素,如果满足 i<j 且 a[i]>a[j],则其为一个逆序对;否则不是。
输入格式
第一行包含整数 n,表示数列的长度。
第二行包含 n个整数,表示整个数列。
输出格式
输出一个整数,表示逆序对的个数。
数据范围
1≤n≤100000,
数列中的元素的取值范围 [1,
1
0
9
10^9
109]。
输入样例:
6
2 3 4 5 6 1
输出样例:
5
答案:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
static long res = 0;
public static void mergeSort(int[] a, int begin, int end) {
if (begin >= end) return;
int mid = (begin + end) / 2;
mergeSort(a, begin, mid);
mergeSort(a, mid + 1, end);
merge(a, begin, end, mid);
}
public static void merge(int[] a, int i, int j, int k) {
int n = j - i + 1;
int[] c = new int[n];
int x = i, y = k + 1, t = 0;
while (x <= k && y <= j) {
if (a[x] <= a[y]) c[t++] = a[x++];
else {
c[t++] = a[y++];
res += (k - x + 1);
}
}
while (x <= k) c[t++] = a[x++];
while (y <= j) c[t++] = a[y++];
for (t = 0; t < n; t++) {
a[i++] = c[t];
}
}
public static void main(String[] args) throws Exception {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String[] str = bufferedReader.readLine().split(" ");
int n = Integer.valueOf(str[0]);
int[] a = new int[n];
str = bufferedReader.readLine().split(" ");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(str[i]);
}
mergeSort(a, 0, n - 1);
System.out.println(res);
}
}