Changeset 1596 for cobra/trunk/HowTo/290-TranslatePseudoCodeToCobra1.cobra
- Timestamp:
- 08/28/08 05:23:09 (4 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
cobra/trunk/HowTo/290-TranslatePseudoCodeToCobra1.cobra
r1454 r1596 5 5 6 6 Note that were pseudo-code says "i = i+1", the Cobra code uses augmented assignment 7 operators such as "i += 1". However, you can still write "i = i+1" if you choose. 7 operators such as "i += 1". However, you can still write "i = i+1" if you like. 8 9 Note that the default type for an argument is dynamic? which means that the argument 10 can be any object or nil. 8 11 9 12 Authors: Max Grender-Jones, Chuck Esterbrook … … 23 26 assert sortMethod([1,1,1,1]) == [1,1,1,1] 24 27 25 def bubbleSort(list) as dynamic 28 def bubbleSort(list) as dynamic? 26 29 """ 27 30 From http://en.wikipedia.org/wiki/Bubble_sort#Pseudocode_implementation … … 41 44 .testSort(ref .bubbleSort) 42 45 body 43 swapped = false 44 post while swapped 46 if list 45 47 swapped = false 46 for i in list.count - 1 47 if list[i] > list[i+1] 48 list.swap(i, i+1) 49 swapped = true 48 post while swapped 49 swapped = false 50 for i in list.count - 1 51 if list[i] > list[i+1] 52 list.swap(i, i+1) 53 swapped = true 50 54 return list 51 55 52 def heapSort(list) as dynamic 56 def heapSort(list) as dynamic? 53 57 """ 54 58 From http://en.wikipedia.org/wiki/Heapsort#Pseudocode … … 72 76 .testSort(ref .heapSort) 73 77 body 74 .heapify(list) 75 last = list.count - 1 76 while last > 0 77 list.swap(0, last) 78 last -= 1 79 .siftDown(list, 0, last) 78 if list 79 .heapify(list) 80 last = list.count - 1 81 while last > 0 82 list.swap(0, last) 83 last -= 1 84 .siftDown(list, 0, last) 80 85 return list 81 86 82 def heapify(list )87 def heapify(list as dynamic) 83 88 """ 84 89 From http://en.wikipedia.org/wiki/Heapsort#Pseudocode … … 129 134 return 130 135 131 def insertionSort(list) as dynamic 136 def insertionSort(list) as dynamic? 132 137 """ 133 138 From http://en.wikipedia.org/wiki/Insertion_sort … … 144 149 .testSort(ref .insertionSort) 145 150 body 146 for i in 1 : list.count 147 value = list[i] 148 j = i - 1 149 while j >= 0 and list[j] > value 150 list[j+1] = list[j] 151 j -= 1 152 list[j+1] = value 151 if list 152 for i in 1 : list.count 153 value = list[i] 154 j = i - 1 155 while j >= 0 and list[j] > value 156 list[j+1] = list[j] 157 j -= 1 158 list[j+1] = value 153 159 return list 154 160 155 161 156 def quickSort(list) as dynamic 162 def quickSort(list) as dynamic? 157 163 """ 158 164 From http://en.wikipedia.org/wiki/Quicksort … … 171 177 .testSort(ref .quickSort) 172 178 body 173 if list .count <= 1179 if list is nil or list.count <= 1 174 180 return list 175 181 else
