c
运行时间:139ms
#include <stdio.h>
#define N 100010
int n;
int q[N];
void quick_sort(int l, int r) {
if (l >= r) return;
int x = q[l + r >> 1], i = l - 1, j = r + 1;
while (i < j) {
while (q[++i] < x);
while (q[--j] > x);
if (i < j) {
int tmp = q[j];
q[j] = q[i];
q[i] = tmp;
}
}
quick_sort(l, j);
quick_sort(j + 1, r);
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &q[i]);
quick_sort(0, n - 1);
for (int i = 0; i < n; i ++) printf("%d ", q[i]);
return 0;
}
cpp
运行时间:167ms
#include <iostream>
using namespace std;
const int N = 1e6 + 10;
int n;
int q[N];
void quick_sort(int l, int r)
{
if (l >= r) return;
int x = q[(l + r) / 2], i = l - 1, j = r + 1;
while (i < j)
{
do i ++; while (q[i] < x);
do j --; while (q[j] > x);
if (i < j) swap(q[i], q[j]);
}
quick_sort(l, j);
quick_sort(j + 1, r);
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i ++) scanf("%d", &q[i]);
quick_sort(0, n - 1);
for (int i = 0; i < n; i ++) printf("%d ", q[i]);
return 0;
}
java
运行时间:2231ms(优化前5000ms)
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
int[] arr = new int[n];
String[] strs = reader.readLine().split(" ");
for (int i = 0; i < n; i ++) {
arr[i] = Integer.parseInt(strs[i]);
}
quickSort(arr, 0, arr.length - 1);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i] + " ");
}
System.out.println(sb);
reader.close();
}
public static void quickSort(int[] arr, int l, int r) {
if (l >= r) return;
int x = arr[l + r >> 1], i = l - 1, j = r + 1;
while (i < j) {
while (arr[++i] < x);
while (arr[--j] > x);
if (i < j) {
int tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
}
}
quickSort(arr, l, j);
quickSort(arr, j + 1, r);
}
}
go
运行时间:162ms(优化前:50000ms)
package main
import (
"fmt"
"os"
"bufio"
"strings"
"strconv"
)
func main() {
var n int
fmt.Scanf("%d", &n)
q := make([]int, n)
reader := bufio.NewReader(os.Stdin)
line, _ := reader.ReadString('\n')
line = strings.TrimRight(line, "\n")
s := strings.Split(line, " ")
for i := 0; i < n; i++ {
q[i], _ = strconv.Atoi(s[i])
}
quickSort(q, 0, n - 1)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
for i := 0; i < n; i++ {
fmt.Fprintf(writer, "%d ", q[i])
}
return
}
func quickSort(q []int, l, r int) {
if l >= r {
return
}
x := q[(l + r) >> 1]
i, j := l-1, r+1
for i < j {
for {
i++
if q[i] >= x {
break
}
}
for {
j--
if q[j] <= x {
break
}
}
if i < j {
q[i], q[j] = q[j], q[i]
}
}
quickSort(q, l, j)
quickSort(q, j + 1, r)
}
CSharp
using System;
using System.Collections.Generic;
namespace QuickSort{
class Program{
static void Main(){
int n = int.Parse(Console.ReadLine());
List<int> a = new List<string>(Console.ReadLine().Split()).ConvertAll<int>(i => int.Parse(i));
quicksort(a, 0, n - 1);
string res = "";
foreach (int x in a){
Console.Write(string.Format("{0} ", x));
}
}
static void quicksort(List<int>a, int l, int r){
if (l >= r) return;
int x = a[(l + r)/2], i = l - 1, j = r + 1;
while (i < j){
while (a[++i] < x);
while (a[--j] > x);
if (i < j){
int tmp = a[j];
a[j] = a[i];
a[i] = tmp;
}
}
quicksort(a, l, j);
quicksort(a, j + 1, r);
}
}
}
python3
运行时间:1990ms
def quick_sort(nums, l, r):
if l >= r : return
x = nums[(l + r) // 2]
i = l - 1
j = r + 1
while i < j:
while True:
i += 1
if nums[i] >= x:
break
while True:
j -= 1
if nums[j] <= x:
break
if i < j:
nums[i], nums[j] = nums[j], nums[i]
quick_sort(nums, l, j)
quick_sort(nums, j + 1, r)
if __name__ == '__main__':
n = int(input())
nums = list(map(int, input().split()))
quick_sort(nums, 0, n - 1)
print(' '.join(map(str, nums)))
javascript
递归爆栈
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
});
let n = null;
let list = [];
readline.on('line', line => {
if (n === null) n = Number(line);
else list = line.split(' ').map(Number);
});
readline.on('close', () => {
const res = fn(list);
console.log(res.join(' '));
});
function fn(list) {
function swap(list, i, j) {
const tmp = list[i];
list[i] = list[j];
list[j] = tmp;
}
function quickSort(list, l, r) {
if(l >= r) return;
const x = list[l];
let i = l - 1;
let j = r + 1;
while (i < j) {
while (list[++i] < x);
while (list[--j] > x);
if (i < j) swap(list, i, j);
}
quickSort(list, l, j);
quickSort(list, j + 1, r);
}
quickSort(list, 0, list.length - 1);
return list;
}
可以,这go优化这么复杂么QAQ
有更好的办法,可以提出来哈,这是我能想到最简单的办法了,我看别人都是专门写了一个函数