はかりの付箋がはがされ、こちらに正誤が発表されます |
言語選択: |
自動整列: |
|→選択されていません |
再生方法: |
|→選択されていません |
関数 バブルソート(Hairetsu):
n = 長さ(Hairetsu)
count = 0
count < n - 1 の間繰り返す:
| i を 0 から n - count - 2 まで 1 ずつ増やしながら繰り返す:
| | もし Hairetsu[i] > Hairetsu[i + 1] ならば:
| | | tmp = Hairetsu[i]
| | | Hairetsu[i] = Hairetsu[i + 1]
| ⎿ ⎿ Hairetsu[i + 1] = tmp
⎿ count += 1
バブルソート(Array)
関数 選択ソート(Hairetsu):
n = 長さ(Hairetsu)
count = 0
count < n - 1 の間繰り返す:
| max_index = 0
| i を 0 から n - count - 2 まで 1 ずつ増やしながら繰り返す:
| | もし Hairetsu[max_index] > Hairetsu[i + 1] ならば:
| ⎿ ⎿ max_index = i + 1
| tmp = Hairetsu[i + 1]
| Hairetsu[i + 1] = Hairetsu[max_index]
| Hairetsu[max_index] = tmp
⎿ count += 1
選択ソート(Array)
関数 挿入ソート(Hairetsu):
i を 1 から 長さ(Hairetsu) - 1 まで 1 ずつ増やしながら繰り返す:
| current_element = Hairetsu[i]
| k = i - 1
| k >= 0 and Hairetsu[k] > current_element の間繰り返す:
| | tmp = Hairetsu[k + 1]
| | Hairetsu[k + 1] = Hairetsu[k]
| | Hairetsu[k] = tmp
⎿ ⎿ k -= 1
挿入ソート(Array)
関数 クイックソート(Hairetsu):
もし 長さ(Hairetsu) <= 1 ならば:
⎿ return Hairetsu
pivot = Hairetsu[長さ(Hairetsu) // 2]
left = []
right = []
i を 0 から 長さ(Hairetsu) - 1 まで 1 ずつ増やしながら繰り返す:
| もし Hairetsu[i] == pivot ならば:
| | パスする
| そうでなければ:
| | もし Hairetsu[i] <= pivot ならば:
| | | left.追加(Hairetsu[i])
| | そうでなければ:
⎿ ⎿ ⎿ right.追加(Hairetsu[i])
返り値 クイックソート(left) + [pivot] + クイックソート(right)
クイックソート(Array)
def bubble_sort(Hairetsu):
n = len(Hairetsu)
count = 0
while count < n - 1:
for i in range(n - count - 1):
if Hairetsu[i] > Hairetsu[i + 1]:
tmp = Hairetsu[i]
Hairetsu[i] = Hairetsu[i + 1]
Hairetsu[i + 1] = tmp
count += 1
bubble_sort(Array)
def selection_sort(Hairetsu):
n = len(Hairetsu)
count = 0
while count < n - 1:
max_index = 0
for i in range(n - count - 1):
if Hairetsu[max_index] > Hairetsu[i + 1]:
max_index = i + 1
tmp = Hairetsu[i + 1]
Hairetsu[i + 1] = Hairetsu[max_index]
Hairetsu[max_index] = tmp
count += 1
selection_sort(Array)
def insertion_sort(Hairetsu):
for i in range(1, len(Hairetsu)):
current_element = Hairetsu[i]
k = i - 1
while k >= 0 and Hairetsu[k] > current_element:
tmp = Hairetsu[k + 1]
Hairetsu[k + 1] = Hairetsu[k]
Hairetsu[k] = tmp
k -= 1
insertion_sort(Array)
def quick_sort(Hairetsu):
if len(Hairetsu) <= 1:
return Hairetsu
pivot = Hairetsu[len(Hairetsu) // 2]
left = []
right = []
for i in range(len(Hairetsu)):
if Hairetsu[i] == pivot:
pass
else:
if Hairetsu[i] <= pivot:
left.append(Hairetsu[i])
else:
right.append(Hairetsu[i])
return quick_sort(left) + [pivot] + quick_sort(right)
quick_sort(Array)