アルゴリズムBasc-2-
nums = [35, 80, 21, 54, 11, 45, 92, 39]
def simpleSelect(idx, arr):
if idx == (len(arr) - 1):
# 정렬되지 않은 부분의 요소 수가 1이 되면 종료
return arr
else:
# arr 의 idx 값과 최소값 위치 교환
minNum = min(arr[idx:])
temp = arr[idx]
arr[arr.index(minNum)] = temp
arr[idx] = minNum
idx += 1
return simpleSelect(idx, arr)
ans = simpleSelect(0, nums)
arr = [19, 80, 77, 11, 54]
def BubbleSort(arr):
for i in range(len(arr) - 1, 1, -1):
for j in range(i):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
print(arr)
return arr
ans = BubbleSort(arr)
def InsertionSort(arr):
for i in range(1, len(arr)):
for j in range(a, 0, -1):
if arr[j] < arr[j-1]:
array[j], array[j-1] = array[j-1], array[j]
else:
break
return arr
ステップ1:グループ間隔SPANをN/2(シェアの整数部)に初期化する.
手順2:SPANが1より大きい場合は、次の手順3~5を繰り返します.
ステップ3:データ列をSPANと同じ長さに分割する.
手順4:単純挿入順にグループを並べ替えます.
def shellSort(arr):
N = len(arr)
gap = N // 2
while gap > 0:
for i in range(gap, N):
temp = arr[i]
j = i - gap
while j >= 0 and arr[j] > temp:
arr[j + gap] = arr[j]
j -= gap
arr[j + gap] = temp
gap = gap // 2
def mergeSort(arr):
if len(arr) > 1:
# Finding the mid of the array
mid = len(arr)//2
# Dividing the array elements
L = arr[:mid]
# into 2 halves
R = arr[mid:]
# Sorting the first half
mergeSort(L)
# Sorting the second half
mergeSort(R)
i = j = k = 0
# Copy data to temp arrays L[] and R[]
while i < len(L) and j < len(R):
if L[i] < R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
# Checking if any element was left
while i < len(L):
arr[k] = L[i]
i += 1
k += 1
while j < len(R):
arr[k] = R[j]
j += 1
k += 1
# 출처: https://www.geeksforgeeks.org/merge-sort/
def partition(array, start, end):
pivot = array[start]
low = start + 1
high = end
while True:
while low <= high and array[high] >= pivot:
high = high - 1
while low <= high and array[low] <= pivot:
low = low + 1
if low <= high:
array[low], array[high] = array[high], array[low]
# The loop continues
else:
break
array[start], array[high] = array[high], array[start]
return high
def quick_sort(array, start, end):
if start >= end:
return
p = partition(array, start, end)
quick_sort(array, start, p-1)
quick_sort(array, p+1, end)
# 출처: https://stackabuse.com/quicksort-in-python/
def heapify(arr, n, i):
# Find largest among root and children
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and arr[i] < arr[l]:
largest = l
if r < n and arr[largest] < arr[r]:
largest = r
# If root is not largest, swap with largest and continue heapifying
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)
def heapSort(arr):
n = len(arr)
# Build max heap
for i in range(n//2, -1, -1):
heapify(arr, n, i)
for i in range(n-1, 0, -1):
# Swap
arr[i], arr[0] = arr[0], arr[i]
# Heapify root element
heapify(arr, i, 0)
arr = [1, 12, 9, 5, 6, 10]
heapSort(arr)
n = len(arr)
print("Sorted array is")
for i in range(n):
print("%d " % arr[i], end='')
# 출처: https://www.programiz.com/dsa/heap-sort
Reference
この問題について(アルゴリズムBasc-2-), 我々は、より多くの情報をここで見つけました https://velog.io/@beemo/알고리즘-Basic-2-テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol