Ticket #193: shell-sorts2.patch
File shell-sorts2.patch, 4.9 KB (added by hopscc, 15 years ago) |
---|
-
HowTo/290-TranslatePseudoCodeToCobra1.cobra
25 25 assert sortMethod([4,3,2,1]) == [1,2,3,4] 26 26 assert sortMethod(['c','d','b','a']) == ['a','b','c','d'] 27 27 assert sortMethod([1,1,1,1]) == [1,1,1,1] 28 assert sortMethod([1,2]) == [1,2] 29 assert sortMethod([2,1]) == [1,2] 30 assert sortMethod([3,2,1]) == [1,2,3] 31 assert sortMethod([5,1,2,3,4]) == [1,2,3,4,5] 32 assert sortMethod([9,1,2,3,4]) == [1,2,3,4,9] 33 assert sortMethod([7,5,1,2,3,4,0]) == [0,1,2,3,4,5,7] 28 34 29 35 def bubbleSort(list) as dynamic? 30 36 """ … … 195 201 result.addRange(.quickSort(more)) 196 202 return result 197 203 198 def main 199 .quickSort([1,2,3,4]) 204 def shellSort(list) as dynamic is shared 205 """ 206 From http://en.wikipedia.org/wiki/Shell_sort#Shell_sort_algorithm_in_pseudocode 207 input: an array a of length n with array elements numbered 0 to n - 1 208 inc = round(n/2) 209 while inc > 0 do: 210 for i in inc : n - 1 do: 211 temp = a[i] 212 j = i 213 while j >= inc and a[j - inc] > temp 214 a[j] = a[j - inc] 215 j = j - inc 216 a[j] = temp 217 inc = round(inc / 2.2) 218 """ 219 test 220 .testSort(ref .shellSort) 221 body 222 n = list.count 223 inc = n // 2 224 while inc > 0 225 for i in inc : n # vs n-1, Cobra range is exclusive of endval 226 temp = list[i] 227 j = i 228 while j >= inc and list[j - inc] > temp 229 list[j] = list[j - inc] 230 j = j - inc 231 list[j] = temp 232 inc = (inc+1) // 2.2 # round upwards 233 #inc = Math.round(Convert.toDecimal(inc) / 2.2, MidpointRounding.AwayFromZero) to int 234 return list 235 236 def shellSortAlt(list) as dynamic is shared 237 """ 238 Alternate shell sort implementation: uses bottom portion of range, end-of-loop conditions 239 and breaks early where no exchanges. 240 """ 241 test 242 .testSort(ref .shellSortAlt) 243 body 244 top = list.count 245 nExchg = 0 246 gap = top // 2 247 post while gap > 0 248 post while nExchg # exchanges happened 249 nExchg = 0 250 for i in 0 : top - gap 251 value = list[i] 252 if value > list[i+gap] 253 list[i] = list[i+gap] 254 list[i+gap] = value 255 nExchg += 1 256 gap = gap // 2 257 return list 258 259 260 def main 261 .quickSort([1,2,3,4]) -
HowTo/292-TranslatePseudoCodeToCobra2.cobra
22 22 assert sortMethod([4,3,2,1]) == [1,2,3,4] 23 23 assert sortMethod(['c','d','b','a']) == ['a','b','c','d'] 24 24 assert sortMethod([1,1,1,1]) == [1,1,1,1] 25 assert sortMethod([1,2]) == [1,2] 26 assert sortMethod([2,1]) == [1,2] 27 assert sortMethod([3,2,1]) == [1,2,3] 28 assert sortMethod([5,1,2,3,4]) == [1,2,3,4,5] 29 assert sortMethod([9,1,2,3,4]) == [1,2,3,4,9] 30 assert sortMethod([7,5,1,2,3,4,0]) == [0,1,2,3,4,5,7] 25 31 26 32 def bubbleSort(list as IList) as dynamic 27 33 """ … … 188 194 result.addRange(.quickSort(more)) 189 195 return result 190 196 191 def main 192 .quickSort([1,2,3,4]) 197 def shellSort(list) as dynamic 198 """ 199 From http://en.wikipedia.org/wiki/Shell_sort#Shell_sort_algorithm_in_pseudocode 200 input: an array a of length n with array elements numbered 0 to n - 1 201 inc = round(n/2) 202 while inc > 0 do: 203 for i in inc : n - 1 do: 204 temp = a[i] 205 j = i 206 while j >= inc and a[j - inc] > temp 207 a[j] = a[j - inc] 208 j = j - inc 209 a[j] = temp 210 inc = round(inc / 2.2) 211 """ 212 test 213 .testSort(ref .shellSort) 214 body 215 n = list.count 216 inc = n // 2 217 while inc > 0 218 for i in inc : n # vs n-1, Cobra range is exclusive of endval 219 temp = list[i] 220 j = i 221 while j >= inc and list[j - inc] > temp 222 list[j] = list[j - inc] 223 j = j - inc 224 list[j] = temp 225 inc = ((inc+1) // 2.2) # round upwards 226 #inc = Math.round(Convert.toDecimal(inc) / 2.2, MidpointRounding.AwayFromZero) to int 227 return list 228 229 def shellSortAlt(list) as dynamic 230 """ 231 Alternate shell sort implementation: uses bottom portion of range, end-of-loop conditions 232 and breaks early where no exchanges. 233 """ 234 test 235 .testSort(ref .shellSortAlt) 236 body 237 top = list.count 238 nExchg = false 239 gap = top // 2 240 post while gap > 0 241 post while nExchg # exchanges happened 242 nExchg = false 243 for i in 0 : top - gap 244 value = list[i] 245 if value > list[i+gap] 246 list[i] = list[i+gap] 247 list[i+gap] = value 248 nExchg = true 249 gap = gap // 2 250 return list 251 252 def main 253 .quickSort([1,2,3,4])