Skip to content

Instantly share code, notes, and snippets.

@fumokmm
Created October 16, 2009 07:23
Show Gist options
  • Save fumokmm/211632 to your computer and use it in GitHub Desktop.
Save fumokmm/211632 to your computer and use it in GitHub Desktop.
I'm trying to write Quick Sort in Groovy. /update 2010-10-10 (using #g100pon No.47)
// g100pon #47 QuickSort
def quickSort(lambda, list) {
if (!list || list.size() < 2) return list
def pivot = list[0] // とりあえず先頭のものを基準にする
def middle = list.findAll{ it == pivot }
def left = list.findAll{ lambda(it, pivot) } - middle
def right = list.findAll{ !lambda(it, pivot) } - middle
quickSort(lambda, left) + middle + quickSort(lambda, right)
}
// 昇順
def quickAsc = this.&quickSort.curry(){ a, b -> a < b }
// 降順
def quickDesc = this.&quickSort.curry(){ a, b -> a > b }
def list = null
assert quickAsc(list) == null
assert quickDesc(list) == null
list = []
assert quickAsc(list) == []
assert quickDesc(list) == []
list = [1]
assert quickAsc(list) == [1]
assert quickDesc(list) == [1]
list = [2, 1]
assert quickAsc(list) == [1, 2]
assert quickDesc(list) == [2, 1]
list = [2, 1, 3]
assert quickAsc(list) == [1, 2, 3]
assert quickDesc(list) == [3, 2, 1]
list = (1..100).toList().sort{ Math.random() }
assert quickAsc(list) == (1..100).toList()
assert quickDesc(list) == (100..1).toList()
list = ('a'..'z').toList().sort{ Math.random() }
assert quickAsc(list) == ('a'..'z').toList()
assert quickDesc(list) == ('z'..'a').toList()
list = [1.0f, 'a', 100, null]
assert quickAsc(list) == [null, 1.0f, 'a', 100]
assert quickDesc(list) == [100, 'a', 1.0f, null]
list = 'Karin and Dierk'
assert quickAsc(list) == ' DKaadeiiknnrr'.toList()
assert quickDesc(list) == 'rrnnkiiedaaKD '.toList()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment