This is a sorting algorithm I wrote about half a year ago. Please refer to those who choose python in the afternoon exam.
insertsort.py
#Insertion sort
M = [0] * 5
N = 0
J = 0
BUF = [4,2,5,1,3]
for i in range(0,5,1):
    I =0
    while I <=N and M[I] > BUF[i]:
        I = I+1
    J = N
    while J >= I:
        M[J] =M[J-1]
        J = J-1
    M[I] = BUF[i]
    N = N + 1
print(M)
#result==[5,4,3,2,1]
bubblesort.py
A = [4,2,5,1,3,6,8,7,6,]
N =len(A)
for i in range(N,1,-1):
    for k in range(0,i-1,+1):
        if A[k] > A[k+1]:
            work = A[k]
            A[k] = A[k+1]
            A[k+1] = work
print(A)
#result==[1,2,3,4,5,6,7,8]
selctionsort.py
A =[5,4,2,1,3]
N =4
for I in range(N,0,-1):
    MAX = I
    for J in range(I-1,-1,-1):
        if A[MAX] < A[J]:
            MAX = J
    work = A[I]
    A[I] = A[MAX]
    A[MAX] = work
print(A)
#result== [1,2,3,4,5]
shellsort.py
H  = [2,1,3,8,6]
k = (len(H))// 2
u =0
while k != 0:
    s = k
    while s <= len(H)-1:
        u = s - k #
        while u >=0:
            if H[u] > H[u+k]:
                print(H)
                work = H[u]
                H[u] = H[u+k]
                H[u+k] = work
                u = u - k
            else:
                break
        s = s + 1
    k = k // 2
print(H)
#result==[1,2,3,4,5]
heapsort.py
#Heapsort
A = {1:20,2:50,3:10,4:30,5:70,6:40,7:60,8:80}
def INSERT(i):
    num = N # 2
    o = 1
    while  o != 0:
        o = num // 2 # 1
        if A[num] > A[o]:
            work = A[num]
            A[num] = A[o]
            A[o] = work
            num = o
        if A[num] <= A[o]:
            o -=1
    return A
for N in range(2,9,1):
    hoge = INSERT(N)
print ('Heap creation result')
print(hoge)
def heap (hani):#7
    oya = 1
    kodo = oya * 2 #2
    while  kodo < hani: #2>7 Reconstruction loop
        if kodo+1 <= hani: #3<7
            if hoge[kodo+1] > hoge[kodo]:#60 > 70
                kodo+=1
        if hoge[kodo] > hoge[oya]: #3[60] > 2[70]
            work = hoge[kodo]
            hoge[kodo] = hoge[oya]
            hoge[oya] = work
            oya = kodo
            kodo = oya*2
        elif hoge[kodo] <= hoge[oya]:
             kodo = hani + 1
    return hoge
for s in range(len(hoge),2,-1):
    work = hoge[1]
    hoge[1] = hoge[s]
    hoge[s] = work
    #print(hoge)
    if s > 2:
        hoge= heap(s-1)
print ('Sort heapsort in ascending order')
print(hoge)
#result
#Heap creation result
#[1: 80, 2: 70, 3: 60, 4: 50, 5: 30, 6: 10, 7: 40, 8: 20]
#[1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60, 7: 70, 8: 80]
margesort.py
 #-*- coding: utf-8 -*-
output =[47,33,68,55,74,89,25,10]
span_size =2 #Set the division range
size = len(output)#Substitute the number of elements of output
size2 = size // 2
temp = [0]*size2    #Set the size of the array used for replacement
while span_size <= size: #Execute after sorting the division range,Specify the array range ➀
    span_idx = 0 #Initialize#Specify the position to divide
    write_idx = 0#Initialize#Swap value
    while span_idx < size: #Set the division position ②
        a_idx = span_idx #Compare the elements of the divided range
        b_idx = span_idx + span_size // 2 #Set the comparison target within the divided range
        for i in range(a_idx - span_idx,b_idx - a_idx,1): #Set the elements to be replaced ③
            temp[i] = output[i+span_idx]
            #print(temp)
            #print('Add to array temp')
            a_yet = True
            b_yet = True
        while a_yet == True or b_yet == True:#When sorting within the division range is completed, the loop ends ④
            if b_yet == False or (a_yet == True and b_yet == True and temp[a_idx - span_idx] <= output[b_idx]):#⑤
                #Short-circuit evaluation
                output[write_idx] = temp[a_idx - span_idx]
        
                a_idx +=1
                if a_idx >= span_idx + span_size // 2:
                    a_yet = False
 
            else:
                output[write_idx] = output[b_idx]
                b_idx +=1
                if b_idx >= span_idx + span_size:#When all the elements of the array temp are aligned ⑥
                    b_yet = False
            write_idx+=1  #Count the substitution position
        span_idx = span_idx + span_size#Move to the next scan range after sorting
    span_size = span_size * 2#
print(output)
#result==[10,25,33,47,55,68,74,89]
quicksoet.py
#Array sort using quicksort
#Sorting method that decides the reference value and divides it into more or less
#Recursive function
A = [2,4,221,5,8,2,9,10]
K = 0
J = 0
def Arrange(A,min,max,pivot):
    L = min
    R = max
    while L <= R:
        tmp = A[L]
        A[L] = A[R]
        A[R] = tmp
        while A[L]< pivot:
            L+=1
        while A[R]>= pivot:
            R -=1
    ret = L
    return ret
def findpivot(A,min,max):
    pivot = A[min]
    K  = min+1
    ret = -1
    found = False
    while K <= max and not found:
         if A[K] == pivot:
             K+=1
         else:
             found = True
             if A[K]> pivot:
                 ret = K
             else:
                 ret = min
    return ret
def quicksort(A,min,max):
    J=findpivot(A,min,max)
    if J > -1:
        pivot = A[J]
        K = Arrange(A,min,max,pivot)
        L = K - 1
        quicksort(A,min,L)
        quicksort(A,K,max)
        return(A)
num = quicksort(A,0,len(A)-1,)
print(num)
#result[2,2,4,5,8,9,10,221]
When I think about it now, I'm sorry if I can't refer to the spaghetti code.
Recommended Posts