Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes the threshold in Array#sort for determining whether to use in…
…sertion sort or quicksort. The threshold was 6, but it is now 13. Results and methodology below. ## Code def quicksort(v, tally=[]) return v if v.nil? or v.length <= 1 less, more = v[1..-1].partition { |i| tally << 1; i < v[0] } quicksort(less, tally) + [v[0]] + quicksort(more, tally) end def qsort(v) array = [] quicksort(v, array) array.size end def isort(list) tally = 0 for i in 1..(list.length - 1) value = list[i] j = i - 1 while j >= 0 and list[j] > value list[j + 1] = list[j] j -= 1 tally += 1 end list[j + 1] = value end tally end def random_array(size) array = [] size.times { array << rand(1_000_000) } array end class Array def average sum / size end def sum inject(0) {|s, v| s + v } end end ################################# q_results = Array.new(51) { [] } i_results = Array.new(51) { [] } 10_000.times do (1..50).map do |i| q_results[i] << qsort(random_array(i)) i_results[i] << isort(random_array(i)) end end puts "Size\t| Quicksort\t| Insertio[i].average}\t| #{i_results[i].average}" end ## Results Size | Quicksort | Insertion sort 1 | 0 | 0 2 | 1 | 0 3 | 2 | 1 4 | 4 | 2 5 | 7 | 5 6 | 10 | 7 7 | 13 | 10 8 | 16 | 14 9 | 20 | 17 10 | 24 | 22 11 | 28 | 27 12 | 32 | 32 13 | 37 | 38 14 | 41 | 45 15 | 46 | 52 16 | 51 | 59 17 | 55 | 68 18 | 60 | 76 19 | 65 | 85 20 | 71 | 94 21 | 76 | 104 22 | 81 | 115 23 | 87 | 126 24 | 92 | 138 25 | 98 | 150 26 | 104 | 162 27 | 110 | 175 28 | 115 | 188 29 | 121 | 203 30 | 127 | 217 31 | 134 | 232 32 | 139 | 247 33 | 145 | 264 34 | 152 | 280 35 | 158 | 297 36 | 164 | 314 37 | 171 | 333 38 | 177 | 351 39 | 184 | 370 40 | 191 | 390 41 | 197 | 410 42 43 | 211 | 452 44 | 216 | 472 45 | 224 | 494 46 | 231 | 517 47 | 238 | 541 48 | 244 | 563 49 | 251 | 588 50 | 258 | 612 real 1m57.527s user 1m56.007s sys 0m0.314s ## uname -a Darwin baits-190183.reshall.umich.edu 10.6.0 Darwin Kernel Version 10.6.0: Wed Nov 10 18:13:17 PST 2010; root:xnu-1504.9.26~3/RELEASE_I386 i386 i386
- Loading branch information