Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Selection sort and radix sort

  • Loading branch information...
commit 4476c34065c216bc2ce713c9c39d3e0a5bb8b102 1 parent dc4e79d
authored May 14, 2010
42  p102.groovy
... ...
@@ -0,0 +1,42 @@
  1
+/*
  2
+ * Three distinct points are plotted at random on a Cartesian plane,
  3
+ * for which -1000 <= x, y <= 1000, such that a triangle is formed.
  4
+ *
  5
+ * Consider the following two triangles:
  6
+ *
  7
+ * A(-340,495), B(-153,-910), C(835,-947)
  8
+ * X(-175,41), Y(-421,-714), Z(574,-645)
  9
+ *
  10
+ * It can be verified that triangle ABC contains the origin, whereas
  11
+ * triangle XYZ does not.
  12
+ *
  13
+ * Using triangles.txt, a 27K text file containing the co-ordinates
  14
+ * of one thousand "random" triangles, find the number of triangles
  15
+ * for which the interior contains the origin.
  16
+ */
  17
+
  18
+boolean insideTriangle(t, x) {
  19
+    insideTriangle(t[0,1], t[2,3], t[4,5], x)
  20
+}
  21
+
  22
+boolean insideTriangle(a, b, c, x) {
  23
+    onSemiPlane(a, b, c, x) && onSemiPlane(b, c, a, x) && onSemiPlane(c, a, b, x)
  24
+}
  25
+
  26
+boolean onSemiPlane(a, b, c, d) {
  27
+    onLine(a, b, c) * onLine(a, b, d) >= 0
  28
+}
  29
+
  30
+long onLine(a, b, c) {
  31
+    (c[0] - a[0]) * (b[1] - a[1]) - (c[1] - a[1]) * (b[0] - a[0])
  32
+}
  33
+
  34
+assert insideTriangle([-340,495],[-153,-910],[835,-947],[0,0])
  35
+assert !insideTriangle([-175,41,-421,-714,574,-645],[0,0])
  36
+
  37
+int count = 0
  38
+new File('triangles.txt').eachLine {
  39
+    def triangle = it.split(/,/)*.toInteger()
  40
+    if (insideTriangle(triangle, [0,0])) count++
  41
+}
  42
+println count
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])
132 132
 
133 133
 /*
134 134
  * http://en.wikipedia.org/wiki/Insertion_sort
  135
+ * CLRS, p. 26. Worst case Θ(n2)
135 136
  */
136 137
 def insertionSort(list) {
137 138
     for (int i in 1..<list.size()) {
138  
-        int c = i
139  
-        while (0 < c) {
140  
-            if (list[c] < list[c-1]) {
141  
-                swap(list, c-1, c)
142  
-                c = c - 1
143  
-            } else break
  139
+        def key = list[i]
  140
+        int j = i - 1
  141
+        while (0 <= j && key < list[j]) {
  142
+            list[j + 1] = list[j]
  143
+            j = j - 1
144 144
         }
  145
+        list[j + 1] = key
145 146
     }
146 147
     list
147 148
 }
@@ -151,7 +152,30 @@ assert [1,2,3,4,5,6,7,8] == insertionSort([6,1,5,3,8,7,4,2])
151 152
 
152 153
 
153 154
 /*
  155
+ * http://en.wikipedia.org/wiki/Selection_sort
  156
+ */
  157
+def selectionSort(list) {
  158
+    for (int i in 0..<list.size()) {
  159
+        int index = i
  160
+        def min = list[i]
  161
+        for (int j in i..<list.size()) {
  162
+            if (list[j] < min) {
  163
+                min = list[j]
  164
+                index = j
  165
+            }
  166
+        }
  167
+        swap(list, i, index)
  168
+    }
  169
+    list
  170
+}
  171
+
  172
+assert [1,2,3,4,5,6,7,8] == selectionSort([6,1,5,3,8,7,4,2])
  173
+
  174
+
  175
+
  176
+/*
154 177
  * http://en.wikipedia.org/wiki/Selection_algorithm
  178
+ * Random select. O(n) expected running time. O(n2) worst case
155 179
  */
156 180
 def select(list, p, q, i) {
157 181
     if (p == q) return list[p]
@@ -187,6 +211,11 @@ assert 4 == median([6,1,5,3,8,7,4,2])
187 211
 
188 212
 
189 213
 
  214
+// ------------------------------------------------------------------
  215
+// Outside of comparison sort algorithms
  216
+// http://www.catonmat.net/blog/mit-introduction-to-algorithms-part-three
  217
+// ------------------------------------------------------------------
  218
+
190 219
 /*
191 220
  * http://en.wikipedia.org/wiki/Counting_sort
192 221
  */
@@ -205,4 +234,48 @@ def countingSort(list) {
205 234
     result
206 235
 }
207 236
 
208  
-assert [3,4,4,5,6,7] == countingSort([7,3,6,4,5,4])
  237
+assert [3,4,4,5,6,7] == countingSort([7,3,6,4,5,4])
  238
+
  239
+
  240
+
  241
+/*
  242
+ * http://en.wikipedia.org/wiki/Radix_sort
  243
+ */
  244
+def radixSort(list) {
  245
+    int passes = maxItemLength(list)
  246
+    def result = list
  247
+    (1..passes).each { digit ->
  248
+        result = mergeBuckets(splitToBuckets(result, digit))
  249
+    }
  250
+    result
  251
+}
  252
+
  253
+int maxItemLength(list) {
  254
+    def max = Integer.MIN_VALUE
  255
+    list.each { if (max < it) max = it }
  256
+    Math.ceil(Math.log10(max+1)) as int
  257
+}
  258
+
  259
+def splitToBuckets(list, k) {
  260
+    def buckets = emptyBuckets(10)
  261
+    list.each { buckets[getDigit(it, k)] << it }
  262
+    buckets
  263
+}
  264
+
  265
+def emptyBuckets(k) {
  266
+    def buckets = new LinkedList[10]
  267
+    (0..<k).each { buckets[it] = new LinkedList() }
  268
+    buckets
  269
+}
  270
+
  271
+int getDigit(number, k) {
  272
+    number.intdiv(10**(k-1)).mod(10)
  273
+}
  274
+
  275
+def mergeBuckets(buckets) {
  276
+    def result = []
  277
+    buckets.each { result += it }
  278
+    result
  279
+}
  280
+
  281
+assert [2,24,45,66,75,90,170,802] == radixSort([170,45,75,90,2,24,802,66])

0 notes on commit 4476c34

Please sign in to comment.
Something went wrong with that request. Please try again.