開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
初めてのコンピュータサイエンス(Jennifer Campbell、Paul Gries、Jason Montojo、Greg Wilson(著)長尾 高弘(翻訳))の11章(探索とソート)、11.7(練習問題)、11-2-a.を解いてみる。
11.7(練習問題)、11-2-a.
書くイテレーション終了後のリストの内容の変化。
- [6, 5, 4, 3, 7, 1, 2]
- [1, 5, 4, 3, 7, 6, 2]
- [1, 2, 4, 3, 7, 6, 5]
- [1, 2, 3, 4, 7, 6, 5]
- [1, 2, 3, 4, 7, 6, 5]
- [1, 2, 3, 4, 5, 6, 7]
- [1, 2, 3, 4, 5, 6, 7]
- [1, 2, 3, 4, 5, 6, 7]
確認。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
def selectionSort(l):
i = 0
print(l)
while i != len(l):
smallest = findMin(l, i)
l[i], l[smallest] = l[smallest], l[i]
i += 1
print(l)
def findMin(l, i):
smallest = i
i += 1
while i != len(l):
if l[i] < l[smallest]:
smallest = i
i += 1
return smallest
l = [6, 5, 4, 3, 7, 1, 2]
if __name__ == '__main__':
selectionSort(l)
入出力結果(Terminal)
$ ./sample.py [6, 5, 4, 3, 7, 1, 2] [1, 5, 4, 3, 7, 6, 2] [1, 2, 4, 3, 7, 6, 5] [1, 2, 3, 4, 7, 6, 5] [1, 2, 3, 4, 7, 6, 5] [1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5, 6, 7] $
0 コメント:
コメントを投稿