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

use LF line endings in examples/quick_sort.v

This commit is contained in:
Hugo Locurcio 2020-03-03 14:41:05 +01:00 committed by GitHub
parent af3159791f
commit 69f256b900
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 63 deletions

View File

@ -1,50 +1,50 @@
import time import time
import rand import rand
const ( const (
LEN = 1000 // how many random numbers to generate LEN = 1000 // how many random numbers to generate
MAX = 10000 // max of the generated numbers MAX = 10000 // max of the generated numbers
) )
fn main() { fn main() {
rand.seed(time.now().unix) rand.seed(time.now().unix)
rand.next(MAX) // skip the first rand.next(MAX) // skip the first
mut arr := []int mut arr := []int
for _ in 0..LEN { for _ in 0..LEN {
arr << rand.next(MAX) arr << rand.next(MAX)
} }
println('length of random array is $arr.len') println('length of random array is $arr.len')
println('before quick sort whether array is sorted: ${is_sorted(arr)}') println('before quick sort whether array is sorted: ${is_sorted(arr)}')
quick_sort(mut arr, 0, arr.len-1) quick_sort(mut arr, 0, arr.len-1)
println('after quick sort whether array is sorted: ${is_sorted(arr)}') println('after quick sort whether array is sorted: ${is_sorted(arr)}')
} }
fn quick_sort(arr mut []int, l int, r int) { fn quick_sort(arr mut []int, l int, r int) {
if l>=r { return } if l>=r { return }
mut sep := l // what is sep: [...all_value<arr[sep]...sep...all_value>=arr[sep]...] mut sep := l // what is sep: [...all_value<arr[sep]...sep...all_value>=arr[sep]...]
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) swap(mut arr, i, sep)
} }
} }
swap(mut arr, l, sep) swap(mut arr, l, sep)
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] [inline]
fn swap(arr mut []int, i int, j int) { fn swap(arr mut []int, i int, j int) {
temp := arr[i] temp := arr[i]
arr[i] = arr[j] arr[i] = arr[j]
arr[j] = temp 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] {
return false return false
} }
} }
return true return true
} }

View File

@ -1,13 +1,13 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module builtin module builtin
/* /*
TODO: Implement hashmap for js when hashmap for V is done. TODO: Implement hashmap for js when hashmap for V is done.
*/ */
fn foo() { fn foo() {
} }