Sorting algorithms/Quicksort: Difference between revisions

Content added Content deleted
Line 886:
=={{header|CoffeeScript}}==
<lang coffeescript>
quicksort = (a[x, xs...]) ->
# This shows quicksort-in-place.
return if i[] ==unless jx?
quicksort = (a) ->
smallerOrEqual = (a for a in xs when a <= x)
swap = (i, j) ->
larger = (a for a in xs when a > x)
return if i == j
(quicksort smallerOrEqual).concat(x).concat(quicksort larger)
[a[i], a[j]] = [a[j], a[i]]
 
divide = (v, start, end) ->
first_big = start
j = start
while j <= end
if a[j] < v
swap first_big, j
first_big += 1
j += 1
first_big
 
partition = (start, end) ->
v = a[end]
first_big = divide v, start, end-1
swap first_big, end
first_big
 
qs = (start, end) ->
return if start >= end
m = partition start, end
qs start, m-1
qs m+1, end
 
qs 0, a.length - 1
 
# test
a = [1, 3, 5, 7, 9, 8, 6, 4, 2, 0, 3.5]
do ->
console.log quicksort a
a = [1, 3, 5, 7, 9, 8, 6, 4, 2, 0, 3.5]
# => console.log a # [ 0, 1, 2, 3, 3.5, 4, 5, 6, 7, 8, 9 ]
quicksort(a)
console.log a # [ 0, 1, 2, 3, 3.5, 4, 5, 6, 7, 8, 9 ]
</lang>
 
 
=={{header|Common Lisp}}==