fork download
  1. #Chris Nixon
  2. #Algorithms II
  3. #Fri. Jan. 27th, 2011
  4. #Homework 2
  5.  
  6. class Sort2DMesh
  7. def initialize
  8. #creates our initial mesh as an instance variable to be used throughout our class
  9. @mesh = [[6, 1, 5, 7, 4, 3, 2, 9],[4, 3, 4, 5, 3, 9, 5, 8], [3, 7, 8, 6, 4, 2, 9, 3],[1, 7, 5, 3, 9, 6, 3, 8], [1, 6, 9, 3, 7, 3, 3, 1], [1, 6, 4, 3, 6, 6, 3, 9], [9, 8, 4, 2, 1, 8, 3, 8], [1, 2, 5, 7, 9, 4, 4, 9]]
  10. end
  11.  
  12. def sort_mesh
  13. print_mesh("Initial mesh:") #prints out the intial mesh
  14. 0.upto(Math.log2(@mesh.length**2)) do |x| #goes from 0 upto log_2_n: where n is 64, so upto 8 in this case #Shikha - The ceil of log2 should be taken. And also, you can directly pass @mesh.length to the functions/
  15. if x % 2 == 0 #if even value of x do the even odd row sort and print out the resulting mesh
  16. even_odd_row_sort(@mesh.length**2)
  17. print_mesh("Even odd row sort:")
  18. else #if odd value of x do the even odd column sort and print out the resulting mesh
  19. even_odd_column_sort(@mesh.length**2)
  20. print_mesh("Even odd column sort:")
  21. end
  22. end
  23. print_mesh("Final mesh:") #after performing the even odd row/column sorts print out the final mesh
  24. print_snake(@mesh.length**2) #prints out a string in snake order
  25. discussion #prints out a discussion of the complexity of the sort2dmesh
  26. end
  27.  
  28. def even_odd_row_sort(n)
  29. Math.sqrt(n).to_i.times do #q times we iterate through our rows in parallel to sort
  30. 0.upto(Math.sqrt(n)-1) do |row| #do in parallel
  31. if row % 2 == 0
  32. 0.upto(Math.sqrt(n)-2) do |col|
  33. if @mesh[row][col] > @mesh[row][col+1]
  34. temp = @mesh[row][col+1] #holds a temp value to be exchanged
  35. @mesh[row][col+1] = @mesh[row][col]
  36. @mesh[row][col] = temp
  37. end
  38. end
  39. else
  40. 0.upto(Math.sqrt(n)-2) do |col|
  41. if @mesh[row][col] < @mesh[row][col+1]
  42. temp = @mesh[row][col+1] #holds a temp value to be exchanged
  43. @mesh[row][col+1]= @mesh[row][col]
  44. @mesh[row][col] = temp
  45. end
  46. end
  47. end
  48. end
  49. end
  50. end
  51.  
  52. def even_odd_column_sort(n)
  53. Math.sqrt(n).to_i.times do #q times we iterate through our columns in parallel to sort
  54. 0.upto(Math.sqrt(n)-1) do |col| #do in parallel
  55. if col % 2 == 0
  56. 0.upto(Math.sqrt(n)-2) do |row|
  57. if @mesh[row][col] > @mesh[row+1][col]
  58. temp = @mesh[row+1][col] #holds a temp value to be exchanged
  59. @mesh[row+1][col] = @mesh[row][col]
  60. @mesh[row][col] = temp
  61. end
  62. end
  63. else
  64. 0.upto(Math.sqrt(n)-2) do |row|
  65. if @mesh[row][col] > @mesh[row+1][col]
  66. temp = @mesh[row+1][col] #holds a temp value to be exchanged
  67. @mesh[row+1][col]= @mesh[row][col]
  68. @mesh[row][col] = temp
  69. end
  70. end
  71. end
  72. end
  73. end
  74. end
  75.  
  76. #prints out the mesh and a given message
  77. def print_mesh(message)
  78. puts "#{message}"
  79. @mesh.each_index do |row|
  80. @mesh.each_index do |column|
  81. print "#{@mesh[row][column]}"
  82. end
  83. puts
  84. end
  85. end
  86.  
  87. #prints out a string of the snake order of the final mesh
  88. def print_snake(n)
  89. puts "Snake order:"
  90. snake_string = ""
  91. 0.upto(Math.sqrt(n)-1) do |x|
  92. if x % 2 == 0
  93. 0.upto(Math.sqrt(n).to_i-1) do |col|
  94. snake_string << "#{@mesh[x][col]}, "
  95. end
  96. else
  97. from = Math.sqrt(n).to_i - 1
  98. from.downto(0) do |col|
  99. snake_string << "#{@mesh[x][col]}, "
  100. end
  101. end
  102. end
  103. puts "#{snake_string[0..-3]}"
  104. end
  105.  
  106. #discusses the sort2dmesh function and the complexity
  107. def discussion
  108. puts "For the sorting a 2D mesh the W(n) = sqrt(n)*log(n) + sqrt(n). EvenOddRowSort (line 23) and\nEvenOddColumnSort (line 47) each perform sqrt(n) parallel comparison steps."
  109. puts "The Sort2dMesh or in our case sort_mesh (line 7) function itself involes log_2_(n) + 1 steps.\nThis is how we get the worst case complexity as mentioned before."
  110. end
  111. end
  112.  
  113. Sort2DMesh.new.sort_mesh
  114.  
Success #stdin #stdout 0s 4892KB
stdin
Standard input is empty
stdout
Initial mesh:
61574329
43453958
37864293
17539638
16937331
16436639
98421838
12579449
Even odd row sort:
12345679
98554433
23346789
98765331
11333679
96664331
12348889
99754421
Even odd column sort:
11333321
12344331
12344431
23344433
96555679
98655679
98766789
99768889
Even odd row sort:
11123333
44333211
11233444
44433332
55566799
99876655
66778899
99988876
Even odd column sort:
11123211
11233332
44333333
44433444
55566655
66776776
99878899
99988899
Even odd row sort:
11111223
33332211
33333344
44444433
55555666
77776666
78889999
99999888
Even odd column sort:
11111211
33332223
33333333
44444444
55555666
77776666
78889888
99999999
Even odd row sort:
11111112
33333222
33333333
44444444
55555666
77776666
78888889
99999999
Final mesh:
11111112
33333222
33333333
44444444
55555666
77776666
78888889
99999999
Snake order:
1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9
For the sorting a 2D mesh the W(n) = sqrt(n)*log(n) + sqrt(n). EvenOddRowSort (line 23) and
EvenOddColumnSort (line 47) each perform sqrt(n) parallel comparison steps.
The Sort2dMesh or in our case sort_mesh (line 7) function itself involes log_2_(n) + 1 steps.
This is how we get the worst case complexity as mentioned before.