1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

exampleы: clean up quick_sort (#5620)

This commit is contained in:
Ruofan XU 2020-07-02 19:32:02 +08:00 committed by GitHub
parent 703b060d09
commit aa270263c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,3 @@
import time
import rand import rand
const ( const (
@ -7,7 +6,6 @@ const (
) )
fn main() { fn main() {
rand.intn(gen_max) // skip the first
mut arr := []int{} mut arr := []int{}
for _ in 0..gen_len { for _ in 0..gen_len {
arr << rand.intn(gen_max) arr << rand.intn(gen_max)
@ -24,21 +22,14 @@ fn quick_sort(mut arr []int, l int, r int) {
for i in l+1..r+1 { for i in l+1..r+1 {
if arr[i] < arr[l] { if arr[i] < arr[l] {
sep++ sep++
swap(mut arr, i, sep) arr[i], arr[sep] = arr[sep], arr[i]
} }
} }
swap(mut arr, l, sep) arr[l], arr[sep] = arr[sep], arr[l]
quick_sort(mut arr, l, sep-1) quick_sort(mut arr, l, sep-1)
quick_sort(mut arr, sep+1, r) quick_sort(mut arr, sep+1, r)
} }
[inline]
fn swap(mut arr []int, i int, j int) {
temp := arr[i]
arr[i] = arr[j]
arr[j] = temp
}
fn is_sorted(arr []int) bool { fn is_sorted(arr []int) bool {
for i in 0..arr.len-1 { for i in 0..arr.len-1 {
if arr[i] > arr[i+1] { if arr[i] > arr[i+1] {