There is a function to create a square array:

def create_array array = [] print "Пожалуйста введите размерность квадратного массива: " n = gets.chomp.to_i puts (0...n).each do |i| array[i] = [] (0...n).each do |j| print "[#{i}, #{j}] = #{array[i] << rand(-30..30)}" puts end end puts "Массив: #{array.inspect}" end 

There is an array sorting function:

 def sort_dimensional_array_descending_optimized(array) swap = true rhythm = array.size - 1 while swap swap = false for i in 0...rhythm swap |= array[i] > array[i + 1] array[i], array[i + 1] = array[i + 1], array[i] if array[i] > array [i + 1] end rhythm -= 1 end puts array.join(', ') end 

Actually the problem: the sort function does not sort an array created from numbers or using the function of creating an array. It sorts only text arrays. Please help me fix it. (I need to understand more, so I will be very happy with detailed comments!)

  • I need an example of how you are trying to sort. From the given example it is not clear. In the method that creates the array, you work with a two-dimensional array, and when sorting with a one-dimensional array. Plus, is it necessary to write your own sorting implementation? Native methods like sort or sort! unsuitable? - anoam
  • So, what kind of array do you want to sort? Numeric or string or mixed? - Mal Skrylev
  • Numeric, string function sorts normally. - ToshiDono
  • @anoam I'm trying to figure out how multidimensional arrays are sorted in Ruby, so I wrote my own function (apparently I didn’t write correctly). The above sort function should sort the two-dimensional array in ascending order (bubble). - ToshiDono
  • Explain please the principle of sorting multidimensional arrays in Ruby. (Before dealing with C) - ToshiDono

2 answers 2

 class MatrixSorter attr_reader :matrix def initialize(matrix) @matrix = matrix end def sort! @matrix = matrix.flatten.sort.each_slice(matrix.size).to_a end end array = [[1, 2, 3], [4, 1, 0], [7, 7, 6]] matrix_sorter = MatrixSorter.new(array) matrix_sorter.sort! p matrix_sorter.matrix.inspect 

I absolutely do not remember how to properly sort the two-dimensional array. In this I did as I realized: we sort all the lines, and then we compare the lines to each other by the first element.

Since ruby ​​is, after all, an object-oriented language, sorting is performed as a class. Input also missed, because there were no questions about him. Sort by manual rearrangement of elements in ruby ​​is not worth it. All this language is able itself, and native sortings are optimized.

Honestly, I have very little idea what needs to be further explained here. So ask in comments - I will add the answer. And if you need a manual sorting for some reason, just write in the comments - I will add a solution.

UPD .:

  1. I'm not quite sure that the two-dimensional array is sorted in this way, but I changed the example so that it is sorted in this way. The principle of such a flatten - converts an array into a one-dimensional, sort - sorts it, each_slice(matrix.size) splits into groups, each by matrix.size (we assume that the array is "square"). And since each_slice returns an object of the Enumerator class, we convert it back to an array.

  2. Unfortunately, the code is difficult to read. But I think the problem is this:

    array3 [i] [j], array3 [i + 1] [j + 1] = array3 [j + 1] [i + 1]

On the left side there are two variables, on the right there is only one.

  1. matrix.sort! {| arr1, arr2 | arr1.first <=> arr2.first} - the sort method can take as its argument a block that is executed during the sorting process.

Since a two-dimensional array is, in fact, an array of arrays, we first sorted each array. (&:sort!) is an abbreviated (and preferred) form. Completely, it would look like matrix.each {| array | array.sort! }

four.

And in the first passage of your code, too, the function does not understand itself, or the sort method is called! for class Array?

This is not understood at all. First, create an object of the MatrixSorter class and then call the method defined in this sort! class sort! . Which, inside, calls the standard array methods.

  • I can not understand how sorting happens. That is, if you take an array: array3 = [[14, 12, 3, 4], [13, 1, 15, 6], [9, 7, 11, 2], [5, 16, 10, 8]] , then after sorting we get the following array: [[1, 6, 13, 15], [2, 7, 9, 11], [3, 4, 12, 14], [5, 8, 10, 16]]. It seems to be sorted, but not completely. in general, how to make the numbers in it go in order (that is, [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, , 14, 15, 16]]) - ToshiDono
  • Here is the sorting: 'while swap swap = false for i in 0..size for j in 0..size swap = true if array3 [i] [j]> array3 [i + 1] [j + 1] array3 [i ] [j], array3 [i + 1] [j + 1] = array3 [j + 1] [i + 1] end end end 'Should work, but does not work ((It gives a lot of errors and I don’t understand what’s wrong ( - ToshiDono
  • I also cannot understand this code in your code: (& & sort!) - & and ":" - what are we for? and also: matrix.sort! {| arr1, arr2 | arr1.first <=> arr2.first} - the resulting function calls itself, but how is the exchange going? And in the first passage of your code, too, the function does not understand itself, or the sort method is called! for class Array? - ToshiDono

Use the Enumerable module's #sort function to sort:

 array.sort { |x, y| x <=> y } 
  • How can I use this construction to sort a two-dimensional array? She does nothing. Example: source array a = [[-2, -16, -6], [-12, -30, 3], [6, -24, -11]]; p a.sort {| x, y | x <=> y} # displays: [[-12, -30, 3], [-2, -16, -6], [6, -24, -11]] - ToshiDono
  • @ToshiDono is like an example, it depends on the task that you want to get at the output))) update the post with output and output array - Mal Skrylev