0

次のJAVAコードをPythonに変換したいと思います(Pythonの初心者です)

public void selectionSort(int[] arr) {
      int i, j, minIndex, tmp;
      int n = arr.length;
      for (i = 0; i < n - 1; i++) {
            minIndex = i;
            for (j = i + 1; j < n; j++)
                  if (arr[j] < arr[minIndex])
                        minIndex = j;
            if (minIndex != i) {
                  tmp = arr[i];
                  arr[i] = arr[minIndex];
                  arr[minIndex] = tmp;
            }
      }
}

これは私が書いたものですが、機能せず、私が間違って何をしているのか理解できません

A = [86, 2,4 ,5, 6122, 87]

def selectionSort(a):
    n = len(a)
    print ("n = ", n)
    for i in range (0, n-1):
        minIndex = i
        j = i+1
        for j in range (0, n):
            if(a[j] < a[minIndex]):
                minIndex = j
        if minIndex != i:
            tmp = a[i]
            a[i] = a[minIndex]
            a[minIndex] = tmp



selectionSort(A)
print(A)

理由を理解するのを手伝ってください

4

3 に答える 3

2

2行を変更します

j = i+1
for j in range (0, n):

for j in range (i+1, n):

合わせる

for (j = i + 1; j < n; j++)

他のニッチ

Pythonでのスワッピングは、次のようにエレガントに行われます。

a[i], a[minIndex] = a[minIndex], a[i]

とブロック全体

    minIndex = i
    for j in range (i+1, n):
        if(a[j] < a[minIndex]):
            minIndex = j
    if minIndex != i:
        a[i], a[minIndex] = a[minIndex], a[i]

次のようにリファクタリングできます

    minIndex = min(range(i,n), key = partial(getitem, a))
    a[i], a[minIndex] = a[minIndex], a[i]

functoolsから部分的にインポートし、operatorからgetitemをインポートする場合

from functools import partial
from operator import getitem

したがって、最終バージョンは次のようになります。

from functools import partial
from operator import getitem
def selectionSort(a):
    n = len(a)
    for i in range (n-1):
        minIndex = min(range(i,n), key = partial(getitem, a))
        a[i], a[minIndex] = a[minIndex], a[i]

最後に、演習としてJavaコードをPythonに変換することが、Pythonを学ぶための良い方法であるかどうか疑問に思うでしょう。

于 2013-03-01T17:58:32.837 に答える
0
for j in range (0, n)

これは、i+1を使用して開始するJavaの場合と同じではありません。

選択ソートを実装するためのよりPython的な方法は、次のようになります。

A = [86, 2,4 ,5, 6122, 87]
def selectionSort(a):
    # Go through all positions except the last one 
    # (that one will automatically be correct)
    for index in range(len(a)-1):
        value = a[index]
        # enumerate all (index, value) pairs from the rest of the list 
        # and take the pair with the smallest value
        min_subindex, min_value = min(enumerate(a[index+1:]), key=lambda x: x[1])
        if min_value < value:
            a[index] = min_value
            a[min_subindex + index + 1] = value

selectionSort(A)
print(A) # [2, 4, 5, 86, 87, 6122]
于 2013-03-01T17:58:27.910 に答える
0

問題:

  1. これを行うと、の代わりにでj in range(0, n-1)ループが開始されます。0i+1

使用する

for j in range(i+1, n-1)

作業コード:

A = [86, 2,4 ,5, 6122, 87]

def selectionSort(a):
    n = len(a)
    print ("n = ", n)
    for i in range (0, n-1):
        minIndex = i
        for j in range (i+1, n):
            if(a[j] < a[minIndex]):
                minIndex = j

        if minIndex != i:
            a[i], a[minIndex] = a[minIndex], a[i]

selectionSort(A)
print(A)
于 2013-03-01T17:59:16.970 に答える