2013年3月3日日曜日

開発環境

『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 10章(章全部で復習), 10.2(通過儀礼としてのソート)再帰 を解いてみる。

その他参考書籍

通過儀礼としてのソート(再帰)

コード(BBEdit)

sample.rb

#!/usr/bin/env ruby1.9
#-*- coding: utf-8 -*-

def sort some_array
    recursive_sort some_array, []
end

def recursive_sort unsorted_array, sorted_array
    return sorted_array if unsorted_array.length == 0
    a = unsorted_array[0..unsorted_array.length]
    min = a.pop
    tmp = []
    a.each do |item|
        if min.to_s <= item.to_s
            tmp.push item
        else
            tmp.push min
            min = item
        end
    end
    sorted_array.push min
    recursive_sort tmp, sorted_array
end

a = ['e','a','d','b','c',5,1,4,2,3,'E','A','D','B','C']
sorted_a = sort a
puts "ソート前: #{a.join " "}"
puts "ソート後: #{sorted_a.join " "}"

入出力結果(Terminal)

$ ./sample.rb
ソート前: e a d b c 5 1 4 2 3 E A D B C
ソート後: 1 2 3 4 5 A B C D E a b c d e
$

ちなみにJavaScriptの場合。

コード(BBEdit)

var a = ['e','a','d','b','c',5,1,4,2,3,'E','A','D','B','C'],
    sort = function ( some_array ) {
        return recursiveSort(some_array, []);
    },
    recursiveSort = function ( unsorted_array, sorted_array ) {
        if (unsorted_array.length === 0) {
            return sorted_array;
        }
        var a = unsorted_array.slice(0),
            min = a.pop(),
            tmp = [],
            i, max;
        for (i = 0, max = a.length; i < max; i += 1) {
            if ( (min).toString() <= (a[i]).toString() ) {
                tmp.push( a[i] );
            } else {
                tmp.push( min );
                min = a[i];
            }
        }
        sorted_array.push(min);
        return recursiveSort(tmp, sorted_array);
    },
    sorted_a = sort( a ),
    result = "ソート前: " + a.join(" ") + "\n" +
              "ソート後: " + sorted_a.join(" ");
$('#pre0').text(result);

pythonの場合。

sample.py

コード(BBEdit)

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

def sort(a):
    return recursiveSort(a, [])

def recursiveSort(unsorted, a):
    if len(unsorted) == 0:
        return a
    b = unsorted[:]
    tmp = []
    min = b.pop()
    for x in b:
        if str(min) <= str(x):
            tmp.append(x)
        else:
            tmp.append(min)
            min = x
    a.append(min)
    return recursiveSort(tmp, a)

a = ['e','a','d','b','c',5,1,4,2,3,'E','A','D','B','C']
b = sort(a)
for x, y in [("ソート前", a), ("ソート後", b)]:
    print("{0}: {1}".format(x, y))

入出力結果(Terminal)

$ ./sample.py
ソート前: ['e', 'a', 'd', 'b', 'c', 5, 1, 4, 2, 3, 'E', 'A', 'D', 'B', 'C']
ソート後: [1, 2, 3, 4, 5, 'A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e']
$

perlの場合。

sample.pl

コード(BBEdit)

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.016;
binmode STDIN, ":utf8";
binmode STDOUT, ":utf8";

sub mySort{
    my $some_array = shift;
    my @empty = ();
    recursiveSort($some_array, \@empty);
}
sub recursiveSort {
    my $some_array = shift;
    my $sorted_array = shift;
    my $unsorted_array = [@$some_array];
    return @$sorted_array unless @$unsorted_array;
    my $min = pop @$unsorted_array;
    my @tmp = ();
    for  (@$unsorted_array) {
        if ($min le $_) {
            push @tmp, $_;
        } else {
            push @tmp, $min;
            $min = $_;
        }
    }
    push @$sorted_array, $min;
    recursiveSort(\@tmp, $sorted_array);
}
my @a = ('e','a','d','b','c',5,1,4,2,3,'E','A','D','B','C');
my @b = mySort \@a;
print "ソート前: @a\n";
print "ソート後: @b\n";

入出力結果(Terminal)

$ ./sample.pl
ソート前: e a d b c 5 1 4 2 3 E A D B C
ソート後: 1 2 3 4 5 A B C D E a b c d e
$

0 コメント:

コメントを投稿