2016-06-11 4 views
-2

おはようございます。私はlistOneを並べ替えることができました。 ListTwoもソートする必要があります。すでにバブルソートにlistTwoを追加してソートされるようにする方法はありますか? 別のループを記述する必要はありますか?バブルソート2リストの単一機能

listOne = [3, 9, 2, 6, 1] 
    listTwo = [4, 8, 5, 7, 0] 

    def bubbleSort (inList): 

    moreSwaps = True 
while (moreSwaps): 
    moreSwaps = False 
    for element in range(len(listOne)-1): 
     if listOne[element]> listOne[element+1]: 
      moreSwaps = True 
      temp = listOne[element] 
      listOne[element]=listOne[element+1] 
      listOne[element+1]= temp 
return (inList) 

     print ("List One = ", listOne) 
     print ("List One Sorted = ", bubbleSort (listOne)) 
     print ("List Two = ", listTwo) 
     print ("List Two Sorted = ", bubbleSort (listTwo)) 
+0

あなたは 'メソッド' というパラダイムについて聞いたことが? ここで読むことはできません:[メソッド](https://en.wikipedia.org/wiki/Method_(computer_programming)) そして、Pythonの場合:[Pythonの "メソッド"とは何ですか?] http://stackoverflow.com/q/3786881/4907452) –

答えて

1

私はあなただけの一つの方法が必要だと思うし、その後、あなたはこれを試すことができる2つのリストにそれを呼び出す呼び出す: あなたのための2つのジョブを行うための一つの方法です。

listOne = [3, 9, 2, 6, 1] 
listTwo = [4, 8, 5, 7, 0] 

def bubblesort(array): 
    for i in range(len(array)): 
     for j in range(len(array) - 1): 
      if array[j] > array[j + 1]: 
       swap = array[j] 
       array[j] = array[j + 1] 
       array[j + 1] = swap 
    print(array) 


bubblesort(listOne) 
bubblesort(listTwo) 

[1、2、3、6、9]

[0、4、5、7、8]

+0

はい!ありがとうございました。よくやった。 ^^ – user662973

+0

@ user662973私は助けてうれしい –

+0

ありがとう! 2番目のリストのバブルソートを行う別のサブプログラムを書きました。 – user662973

関連する問題