每个循环中的选择排序设置值

时间:2017-05-16 10:19:07

标签: ruby selection-sort

我正在尝试找到数组中的最小元素。

我尝试使用finding_smallest方法执行此操作,如下所示:

def finding_smallest arr_arg
  # first time returns 3;
  # second time returns 3 again, even though arr_arg doesn't have it.
  p arr_arg    
  arr_arg.each do |el|
    if el < @min
      @min = el
    end
  end
  @min
end

def selection_sort array
  counter = 0
  sorting = ->(arr){
    arr_range = arr[counter..-1]
    smallest = finding_smallest(arr_range)
    p arr_range # first iteration - whole array; second iteration - [1..end of the array]
    p smallest # first iteration: 3, second iteration: 3;
    first_element_in_range = arr_range[0] # for switching indexes of smallest and first in array
    arr_range[arr_range.index(smallest)], arr_range[0] = arr_range[0], arr_range[arr_range.index(smallest)] #switching places
    counter += 1
    sorting.call(arr_range) unless counter == array.length || arr.nil?
  }
  sorting.call(array)
end

@array = [78, 42, 51, 49, 74, 53, 66, 39, 40, 3, 66, 100]
@min = @array[0]
selection_sort(@array)

它返回前一个数组中的最小元素。我认为问题是each循环没有设置第二次(或第一次)的值。我做错了什么?

1 个答案:

答案 0 :(得分:1)

main扮演[here]全局变量(def finding_smallest arr_arg @min = arr_arg.first arr_arg.each do |el| if el < @min @min = el end end @min end 的实例变量的角色。)一旦设置,它永远不会更新,因为永远不再触及最小值。

您可能希望在每次后续调用时更新它的值:

def finding_smallest arr_arg
  @min = arr_arg.reduce do |min, el|
    el < min ? el : min
  end
end

在ruby中,我们使用Enumerable#reduce

var a = {
   "status": "success",
   "reservations": [
      {
         "id": "26630",
         "subject": "Subject",
         "modifiedDate": "2017-05-16T06:05:12",
         "startDate": "2017-05-16T08:00:00",
         "endDate": "2017-05-16T09:45:00",
         "resources": [
            {
               "id": "2408",
               "type": "student_group",
               "code": "groupCode",
               "name": "groupName"
            },
            {
               "id": "3020",
               "type": "realization",
               "code": "realizationCode",
               "name": "realizationName"
            },
            {
               "id": "48",
               "type": "room",
               "code": "roomCode",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "buildingCode",
                  "name": "buildngName"
               },
               "name": "RoomName (PC)"
            }
         ],
         "description": ""
      },
      {
         "id": "21173",
         "subject": "subjectName",
         "modifiedDate": "2017-05-16T06:05:20",
         "startDate": "2017-05-16T08:00:00",
         "endDate": "2017-05-16T16:00:00",
         "resources": [
            {
               "id": "3115",
               "type": "realization",
               "code": "realizationCode",
               "name": "realizationName"
            },
            {
               "id": "2584",
               "type": "student_group",
               "code": "groupCode",
               "name": "groupName"
            },
            {
               "id": "52",
               "type": "room",
               "code": "roomCode",
               "parent": {
                  "id": "2",
                  "type": "building",
                  "code": "buildingCode",
                  "name": "buildingName"
               },
               "name": "roomName (classroom)"
            }
         ],
         "description": ""
      }
   ]
}

var b = [];
a.reservations.forEach(function(item){ 
    item.resources.forEach(function(obj){  
        if(obj.type == "room"){
            b.push(obj.name)
        }  
    })  
});
console.log(b); //outputs desired array