我的选择排序算法有什么问题?

时间:2016-04-08 20:41:44

标签: ruby algorithm sorting

对于受过训练的人来说,答案可能是显而易见的,但我现在已经在书上打了几个小时,我的眼睛很紧张,我似乎无法看到这个错误。

以下是我编写的两种选择排序实现,也没有正确排序输入。您可以play with this code在线翻译。

def selection_sort_enum(array)
  n = array.length - 1

  0.upto(n - 1) do |i|
    smallest = i

    (i + 1).upto(n) do |j|
      smallest = j if array[j] < array[i]
    end

    array[i], array[smallest] = array[smallest], array[i] if i != smallest
  end
end

def selection_sort_loop(array)
  n = array.length - 1
  i = 0
  while i <= n - 1
    smallest = i
    j = i + 1
    while j <= n
      smallest = j if array[j] < array[i]
      j += 1
    end
    array[i], array[smallest] = array[smallest], array[i] if i != smallest
    i += 1
  end
end

这是第一个实施的测试,selection_sort_enum

puts "Using enum:"
a1 = [*1..10].shuffle
puts "Before sort: #{a1.inspect}"
selection_sort_enum(a1)
puts "After sort: #{a1.inspect}"

这是第二个实施的测试,selection_sort_loop

puts "Using while:"
a2 = [*1..10].shuffle
puts "Before sort: #{a2.inspect}"
selection_sort_enum(a2)
puts "After sort: #{a2.inspect}"

这是第一个实现的输出selection_sort_enum

Using enum:
Before sort: [7, 5, 2, 10, 6, 1, 3, 4, 8, 9]
After sort:  [4, 3, 1, 9, 5, 2, 6, 7, 8, 10]

这是第二个实现的输出,selection_sort_loop

Using while:
Before sort: [1, 10, 5, 3, 7, 4, 8, 9, 6, 2]
After sort:  [1, 2, 4, 3, 6, 5, 7, 8, 9, 10]

3 个答案:

答案 0 :(得分:3)

您要与code snippets进行比较,而不是索引i

这应该有效:

smallest

输出:

def selection_sort_enum(array)
  n = array.length - 1

  0.upto(n - 1) do |i|
    smallest = i

    (i + 1).upto(n) do |j|
      smallest = j if array[j] < array[smallest]
    end
    array[i], array[smallest] = array[smallest], array[i] if i != smallest
  end
end

def selection_sort_loop(array)
  n = array.length - 1
  i = 0
  while i <= n - 1
    smallest = i
    j = i + 1
    while j <= n
      smallest = j if array[j] < array[smallest]
      j += 1
    end
    array[i], array[smallest] = array[smallest], array[i] if i != smallest
    i += 1
  end
end

解决方案链接:http://ideone.com/pKLriY

答案 1 :(得分:1)

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public static $login_validation_rules = [
        'email' => 'required|email|exists:users',
        'password' => 'required'
    ];
}

答案 2 :(得分:1)

您可能对此感兴趣:

def selection_sort_enum(array)
  n = array.length - 1

  0.upto(n - 1) do |i|
    smallest = i

    (i + 1).upto(n) do |j|
      smallest = j if array[j] < array[i]
    end

    array[i], array[smallest] = array[smallest], array[i] if i != smallest
  end
  array # <-- added to return the modified array
end

def selection_sort_loop(array)
  n = array.length - 1
  i = 0
  while i <= n - 1
    smallest = i
    j = i + 1
    while j <= n
      smallest = j if array[j] < array[i]
      j += 1
    end
    array[i], array[smallest] = array[smallest], array[i] if i != smallest
    i += 1
  end
  array # <-- added to return the modified array
end

require 'fruity'

ARY = (1 .. 100).to_a.shuffle
compare do
  _enum { selection_sort_enum(ARY.dup) }
  _loop { selection_sort_loop(ARY.dup) }
end

结果是:

# >> Running each test once. Test will take about 1 second.
# >> _enum is faster than _loop by 3x ± 1.0