快速排序
快速排序也采用了分治的思想:把原始的数组筛选成较小和较大的两个子数组,然后递归地排序两个子数组。
快速排序算法模板:
void quickSort(int[] nums, int left, int high) { if (left >= high) { return; } int i = left - 1, j = high + 1; int x = nums[left]; while (i < j) { while (nums[++i] < x); while (nums[--j] > x); if (i < j) { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; } } quickSort(nums, left, j); quickSort(nums, j + 1, high); }
题目描述
给定你一个长度为 n 的整数数列。
请你使用快速排序对这个数列按照从小到大进行排序。
并将排好序的数列按顺序输出。
输入格式
输入共两行,第一行包含整数 n。
第二行包含 n 个整数(所有整数均在 1∼10^9 范围内),表示整个数列。
输出格式
输出共一行,包含 n 个整数,表示排好序的数列。
数据范围
1≤n≤100000
输入样例:
输出样例:
代码实现
Python3
N = int(input()) nums = list(map(int, input().split())) def quick_sort(nums, left, high): if left >= high: return i, j = left - 1, high + 1 x = nums[(left + high) >> 1] while i < j: while 1: i += 1 if nums[i] >= x: break while 1: j -= 1 if nums[j] <= x: break if i < j: nums[i], nums[j] = nums[j], nums[i] quick_sort(nums, left, j) quick_sort(nums, j + 1, high) quick_sort(nums, 0, N - 1) print(' '.join(list(map(str, nums))))
Java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; ++i) { nums[i] = sc.nextInt(); } quickSort(nums, 0, n - 1); for (int i = 0; i < n; ++i) { System.out.printf("%d ", nums[i]); } } public static void quickSort(int[] nums, int left, int high) { if (left >= high) { return; } int i = left - 1, j = high + 1; int x = nums[left]; while (i < j) { while (nums[++i] < x); while (nums[--j] > x); if (i < j) { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; } } quickSort(nums, left, j); quickSort(nums, j + 1, high); } }
JavaScript
var buf = ''; process.stdin.on('readable', function () { var chunk = process.stdin.read(); if (chunk) buf += chunk.toString(); }); let getInputArgs = line => { return line.split(' ').filter(s => s !== '').map(x => parseInt(x)); } function quickSort(nums, left, high) { if (left >= high) { return; } let i = left - 1; let j = high + 1; let x = nums[(left + high) >> 1]; while (i < j) { while (nums[++i] < x); while (nums[--j] > x); if (i < j) { const t = nums[i]; nums[i] = nums[j]; nums[j] = t; } } quickSort(nums, left, j); quickSort(nums, j + 1, high); } process.stdin.on('end', function () { buf.split('\n').forEach(function (line, lineIdx) { if (lineIdx % 2 === 1) { nums = getInputArgs(line); quickSort(nums, 0, nums.length - 1); console.log(nums.join(' ')); } }); });
Go
package main import "fmt" func quickSort(nums []int, left, high int) { if left >= high { return } i, j := left-1, high+1 x := nums[(left+high)>>1] for i < j { for { i++ if nums[i] >= x { break } } for { j-- if nums[j] <= x { break } } if i < j { nums[i], nums[j] = nums[j], nums[i] } } quickSort(nums, left, j) quickSort(nums, j+1, high) } func main() { var n int fmt.Scanf("%d\n", &n) nums := make([]int, n) for i := 0; i < n; i++ { fmt.Scanf("%d", &nums[i]) } quickSort(nums, 0, n-1) for _, v := range nums { fmt.Printf("%d ", v) } }