Permalink
Browse files

Selection sort and radix sort

  • Loading branch information...
1 parent dc4e79d commit 4476c34065c216bc2ce713c9c39d3e0a5bb8b102 Andrey Paramonov committed May 15, 2010
Showing with 122 additions and 7 deletions.
  1. +42 −0 p102.groovy
  2. +80 −7 recursion.groovy
View
42 p102.groovy
@@ -0,0 +1,42 @@
+/*
+ * Three distinct points are plotted at random on a Cartesian plane,
+ * for which -1000 <= x, y <= 1000, such that a triangle is formed.
+ *
+ * Consider the following two triangles:
+ *
+ * A(-340,495), B(-153,-910), C(835,-947)
+ * X(-175,41), Y(-421,-714), Z(574,-645)
+ *
+ * It can be verified that triangle ABC contains the origin, whereas
+ * triangle XYZ does not.
+ *
+ * Using triangles.txt, a 27K text file containing the co-ordinates
+ * of one thousand "random" triangles, find the number of triangles
+ * for which the interior contains the origin.
+ */
+
+boolean insideTriangle(t, x) {
+ insideTriangle(t[0,1], t[2,3], t[4,5], x)
+}
+
+boolean insideTriangle(a, b, c, x) {
+ onSemiPlane(a, b, c, x) && onSemiPlane(b, c, a, x) && onSemiPlane(c, a, b, x)
+}
+
+boolean onSemiPlane(a, b, c, d) {
+ onLine(a, b, c) * onLine(a, b, d) >= 0
+}
+
+long onLine(a, b, c) {
+ (c[0] - a[0]) * (b[1] - a[1]) - (c[1] - a[1]) * (b[0] - a[0])
+}
+
+assert insideTriangle([-340,495],[-153,-910],[835,-947],[0,0])
+assert !insideTriangle([-175,41,-421,-714,574,-645],[0,0])
+
+int count = 0
+new File('triangles.txt').eachLine {
+ def triangle = it.split(/,/)*.toInteger()
+ if (insideTriangle(triangle, [0,0])) count++
+}
+println count
View
87 recursion.groovy
@@ -132,16 +132,17 @@ assert [1,2,3,4,5,6,7,8] == heapsort([6,1,5,3,8,7,4,2])
/*
* http://en.wikipedia.org/wiki/Insertion_sort
+ * CLRS, p. 26. Worst case Θ(n2)
*/
def insertionSort(list) {
for (int i in 1..<list.size()) {
- int c = i
- while (0 < c) {
- if (list[c] < list[c-1]) {
- swap(list, c-1, c)
- c = c - 1
- } else break
+ def key = list[i]
+ int j = i - 1
+ while (0 <= j && key < list[j]) {
+ list[j + 1] = list[j]
+ j = j - 1
}
+ list[j + 1] = key
}
list
}
@@ -151,7 +152,30 @@ assert [1,2,3,4,5,6,7,8] == insertionSort([6,1,5,3,8,7,4,2])
/*
+ * http://en.wikipedia.org/wiki/Selection_sort
+ */
+def selectionSort(list) {
+ for (int i in 0..<list.size()) {
+ int index = i
+ def min = list[i]
+ for (int j in i..<list.size()) {
+ if (list[j] < min) {
+ min = list[j]
+ index = j
+ }
+ }
+ swap(list, i, index)
+ }
+ list
+}
+
+assert [1,2,3,4,5,6,7,8] == selectionSort([6,1,5,3,8,7,4,2])
+
+
+
+/*
* http://en.wikipedia.org/wiki/Selection_algorithm
+ * Random select. O(n) expected running time. O(n2) worst case
*/
def select(list, p, q, i) {
if (p == q) return list[p]
@@ -187,6 +211,11 @@ assert 4 == median([6,1,5,3,8,7,4,2])
+// ------------------------------------------------------------------
+// Outside of comparison sort algorithms
+// http://www.catonmat.net/blog/mit-introduction-to-algorithms-part-three
+// ------------------------------------------------------------------
+
/*
* http://en.wikipedia.org/wiki/Counting_sort
*/
@@ -205,4 +234,48 @@ def countingSort(list) {
result
}
-assert [3,4,4,5,6,7] == countingSort([7,3,6,4,5,4])
+assert [3,4,4,5,6,7] == countingSort([7,3,6,4,5,4])
+
+
+
+/*
+ * http://en.wikipedia.org/wiki/Radix_sort
+ */
+def radixSort(list) {
+ int passes = maxItemLength(list)
+ def result = list
+ (1..passes).each { digit ->
+ result = mergeBuckets(splitToBuckets(result, digit))
+ }
+ result
+}
+
+int maxItemLength(list) {
+ def max = Integer.MIN_VALUE
+ list.each { if (max < it) max = it }
+ Math.ceil(Math.log10(max+1)) as int
+}
+
+def splitToBuckets(list, k) {
+ def buckets = emptyBuckets(10)
+ list.each { buckets[getDigit(it, k)] << it }
+ buckets
+}
+
+def emptyBuckets(k) {
+ def buckets = new LinkedList[10]
+ (0..<k).each { buckets[it] = new LinkedList() }
+ buckets
+}
+
+int getDigit(number, k) {
+ number.intdiv(10**(k-1)).mod(10)
+}
+
+def mergeBuckets(buckets) {
+ def result = []
+ buckets.each { result += it }
+ result
+}
+
+assert [2,24,45,66,75,90,170,802] == radixSort([170,45,75,90,2,24,802,66])

0 comments on commit 4476c34

Please sign in to comment.