Merge pull request #3067 from apense/patch-10

Updated some example code in algorithm
This commit is contained in:
Dominik Picheta 2015-07-06 17:16:42 +01:00
commit a7f769c6bd

View file

@ -1,7 +1,7 @@
# #
# #
# Nim's Runtime Library # Nim's Runtime Library
# (c) Copyright 2012 Andreas Rumpf # (c) Copyright 2015 Andreas Rumpf
# #
# See the file "copying.txt", included in this # See the file "copying.txt", included in this
# distribution, for details about the copyright. # distribution, for details about the copyright.
@ -97,7 +97,7 @@ proc lowerBound*[T](a: openArray[T], key: T, cmp: proc(x,y: T): int {.closure.})
## ##
## var arr = @[1,2,3,5,6,7,8,9] ## var arr = @[1,2,3,5,6,7,8,9]
## arr.insert(4, arr.lowerBound(4)) ## arr.insert(4, arr.lowerBound(4))
## `after running the above arr is `[1,2,3,4,5,6,7,8,9]` ## # after running the above arr is `[1,2,3,4,5,6,7,8,9]`
result = a.low result = a.low
var count = a.high - a.low + 1 var count = a.high - a.low + 1
var step, pos: int var step, pos: int
@ -160,8 +160,9 @@ proc merge[T](a, b: var openArray[T], lo, m, hi: int,
proc sort*[T](a: var openArray[T], proc sort*[T](a: var openArray[T],
cmp: proc (x, y: T): int {.closure.}, cmp: proc (x, y: T): int {.closure.},
order = SortOrder.Ascending) = order = SortOrder.Ascending) =
## Default Nim sort. The sorting is guaranteed to be stable and ## Default Nim sort (an implementation of merge sort). The sorting
## the worst case is guaranteed to be O(n log n). ## is guaranteed to be stable and the worst case is guaranteed to
## be O(n log n).
## The current implementation uses an iterative ## The current implementation uses an iterative
## mergesort to achieve this. It uses a temporary sequence of ## mergesort to achieve this. It uses a temporary sequence of
## length ``a.len div 2``. Currently Nim does not support a ## length ``a.len div 2``. Currently Nim does not support a
@ -290,7 +291,7 @@ proc nextPermutation*[T](x: var openarray[T]): bool {.discardable.} =
## ##
## var v = @[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ## var v = @[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
## v.nextPermutation() ## v.nextPermutation()
## echo v ## echo v # @[0, 1, 2, 3, 4, 5, 6, 7, 9, 8]
if x.len < 2: if x.len < 2:
return false return false
@ -319,7 +320,7 @@ proc prevPermutation*[T](x: var openarray[T]): bool {.discardable.} =
## ##
## var v = @[0, 1, 2, 3, 4, 5, 6, 7, 9, 8] ## var v = @[0, 1, 2, 3, 4, 5, 6, 7, 9, 8]
## v.prevPermutation() ## v.prevPermutation()
## echo v ## echo v # @[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
if x.len < 2: if x.len < 2:
return false return false
@ -367,4 +368,3 @@ when isMainModule:
for i in 0 .. high(arr1): for i in 0 .. high(arr1):
assert arr1.reversed(0, i) == arr1.reversed()[high(arr1) - i .. high(arr1)] assert arr1.reversed(0, i) == arr1.reversed()[high(arr1) - i .. high(arr1)]
assert arr1.reversed(i, high(arr1)) == arr1.reversed()[0 .. high(arr1) - i] assert arr1.reversed(i, high(arr1)) == arr1.reversed()[0 .. high(arr1) - i]