python Basic sorting algorithm summary (Basic Information Technology Engineer Examination)

This is a sorting algorithm I wrote about half a year ago. Please refer to those who choose python in the afternoon exam.

insert sort

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]

bubble sort

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]

selction sort

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

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

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]

merge sort

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]

Quick sort

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

python Basic sorting algorithm summary (Basic Information Technology Engineer Examination)
[Fundamental Information Technology Engineer Examination] I wrote the algorithm of Euclidean algorithm in Python.
[Fundamental Information Technology Engineer Examination] I wrote a linear search algorithm in Python.
[Fundamental Information Technology Engineer Examination] I wrote an algorithm for determining leap years in Python.
Fundamental Information Technology Engineer Examination (FE) Afternoon Exam Python Sample Question Explanation
Python basic grammar / algorithm
[Fundamental Information Technology Engineer Examination] I wrote an algorithm for the maximum value of an array in Python.
Basic sorting in Python
Fundamental Information Technology Engineer Examination Implemented Python sample questions without using external libraries
Basic information Write the 2018 fall algorithm problem in Python
Experience of taking the Applied Information Technology Engineer Examination
Sorting algorithm and implementation in Python
Algorithm learned with Python 19th: Sorting (heapsort)
Take the Python3 Engineer Certification Basic Exam
A story about downloading the past question PDF of the Fundamental Information Technology Engineer Examination in Python at once
I tried it with Wolfram Alpha and google, referring to "[Fundamental Information Technology Engineer Examination] I wrote an algorithm for determining leap years in Python."
Python Summary
Python algorithm
Python summary
[Examination Report] Python 3 Engineer Certified Data Analysis Exam
Python3 Engineer Certification Basic Exam-Notes and Problem Trends-
Algorithm learned with Python 16th: Sorting (insertion sort)
Algorithm learned with Python 15th: Sorting (selection sort)
Programming beginner Python3 engineer certification basic exam record
Algorithm learned with Python 17th: Sorting (bubble sort)