Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-

Introduction

Hello. It's chewy and chewy. We will solve the introduction to AOJ's algorithms and data structures. It's easy to keep a record of what you've learned.

It's been less than half a year since I started programming myself AtCoder is green, so I'm not a strong man. Let's work hard together.

Kyapee

table of contents

This time PART2: Elementary sort. I want to do my best and do it to the end.

ALDS1_2_A: Bubble sort ALDS1_2_B: Selection sort ALDS1_2_C: Stable sorting ALDS1_2_D: Shellsort

ALDS1_2_A: Bubble sort

Bubble sort

def bubbleSort(A, N):
    bit = True
    i = 0
    count = 0
    while bit:
        bit = False
        for j in range(n-1,i,-1):
            if A[j] < A[j-1]:
                A[j],A[j-1] = A[j-1],A[j]
                count += 1
                bit = True
    
    return A , count
        

n = int(input())
A = list(map(int,input().split()))

A,count = bubbleSort(A,n)
print(*A)
print(count)

ALDS1_2_B: Selection sort

 def SelectionSort(A,n):
    count = 0
    for i in range(n):
        minv = i
        for j in range(i,n):
            if A[j] < A[minv]:
                minv = j
        
        A[i],A[minv] = A[minv],A[i]
        if i!=minv:
            count += 1

    return A,count

n = int(input())
A = list(map(int,input().split()))

A1,count1 = SelectionSort(A,n)

print(*A1)
print(count1)

ALDS1_2_C: Stable sorting

Save as tuple so as not to change the original list Judgment of stability is an exploratory thinking

def bubbleSort(A, N):
    A = list(A)
    bit = True
    i = 0
    while bit:
        bit = False
        for j in range(n-1,i,-1):
            if int(A[j][1]) < int(A[j-1][1]):
                A[j],A[j-1] = A[j-1],A[j]
                bit = True
    
    return A 

def SelectionSort(A,n):
    A = list(A)
    for i in range(n):
        minv = i
        for j in range(i,n):
            if int(A[j][1]) < int(A[minv][1]):
                minv = j
        
        A[i],A[minv] = A[minv],A[i]

    return A

def is_stable(A_in, A_out):
    n = len(A_in)
    for i in range(n):
        for j in range(i+1,n):
            for a in range(n):
                for b in range(a+1,n):
                    if A_in[i][1] == A_in[j][1] and A_in[i]==A_out[b] and A_in[j]==A_out[a]:
                        return "Not stable"
    return "Stable"

n = int(input())
A = list(input().split())
A_ans = tuple(A)

A1 = bubbleSort(A_ans,n)
print(*A1)
print(is_stable(A_ans, A1))

A2 = SelectionSort(A_ans,n)
print(*A2)
print(is_stable(A_ans, A2))

ALDS1_2_D: Shellsort

It's difficult ...



def insertionSort(A,n,g):
    global count
    for i in range(g, n):
        v = A[i]
        j = i - g
        while j >= 0 and A[j]>v:
            A[j+g] = A[j]
            j -= g
            count += 1
        A[j+g] = v



def ShellSort(A, n):
    global count
    count = 0
    g = 1
    G = [g]
    while 3 * g + 1 < n:
        g = 3 * g + 1
        G.append(g)
    m = len(G)
    G = G[::-1]
    
    print(m)
    print(*G)

    for j in G:
        insertionSort(A,n,j)



n = int(input())
A = []
for i in range(n):
    a = int(input())
    A.append(a)

ShellSort(A, n)
print(count)

for i in A:
    print(i)

in conclusion

If you have a wrong answer, please contact Goto

p.s.p I've never received a Qitta like guy We are looking forward to the first memorable relatives. ↑ I wrote it last time, but I'm praising it

Recommended Posts

Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part1-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part2-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part4-
Solving AOJ's Algorithm and Introduction to Data Structures in Python -Part3-
[Introduction to cx_Oracle] (Part 6) DB and Python data type mapping
[Introduction to cx_Oracle] (Part 9) DB and Python data type mapping (version 8 or later)
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
Introduction to Effectiveness Verification Chapters 4 and 5 are written in Python
Introduction to Python scikit-learn, matplotlib, single-layer algorithm (~ towards B3 ~ part3)
[Introduction to Python] Combine Nikkei 225 and NY Dow csv data
[Introduction to Python3 Day 1] Programming and Python
Sorting algorithm and implementation in Python
Introduction to Python Hands On Part 1
Hashing data in R and Python
An introduction to statistical modeling for data analysis (Midorimoto) reading notes (in Python and Stan)
processing to use notMNIST data in Python (and tried to classify it)
[Introduction to Data Scientists] Basics of Python ♬ Conditional branching and loops
[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.
[Introduction to Python] How to use class in Python?
AM modulation and demodulation in Python Part 2
Ant book in python: Sec. 2-4, data structures
Web-WF Python Tornado Part 3 (Introduction to Openpyexcel)
[Introduction to Python3 Day 19] Chapter 8 Data Destinations (8.4-8.5)
[Introduction to Python3 Day 18] Chapter 8 Data Destinations (8.3.6.2 to 8.3.6.3)
Easily graph data in shell and Python
Compress python data and write to sqlite
How to use is and == in Python
"Introduction to data analysis by Bayesian statistical modeling starting with R and Stan" implemented in Python
Introduction to Vectors: Linear Algebra in Python <1>
Introduction to Effectiveness Verification Chapter 1 in Python
[Introduction to Data Scientists] Basics of Python ♬
[Python] [Supplement] Chapter 04-09 Various data structures (set theory and operations in sets)
Data analysis: Easily apply descriptive and inference statistics to CSV data in Python
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
How to generate permutations in Python and C ++
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
Introduction to effectiveness verification Chapter 3 written in Python
Receive and display HTML form data in Python
tse --Introduction to Text Stream Editor in Python
[Python] Swapping rows and columns in Numpy data
[Python] How to read data from CIFAR-10 and CIFAR-100
I wrote "Introduction to Effect Verification" in Python
[Introduction to Python3 Day 22] Chapter 11 Concurrency and Networking (11.1 to 11.3)
Send messages to Skype and Chatwork in Python
[Introduction to Python] How to handle JSON format data
[Introduction to Udemy Python3 + Application] 64. Namespace and Scope
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
[Introduction to Algorithm] Find the shortest path [Python3]
Introduction to Effectiveness Verification Chapter 2 Written in Python
To represent date, time, time, and seconds in Python
How to plot autocorrelation and partial autocorrelation in python
[Python] How to name table data and output it in csv (to_csv method)
Introduction to Time Series Analysis ~ Seasonal Adjustment Model ~ Implemented in R and Python
[Introduction to Udemy Python3 + Application] 35. Comparison operators and logical operators
Convert timezoned date and time to Unixtime in Python2.7
Summary of tools needed to analyze data in Python
Full-width and half-width processing of CSV data in Python
[Introduction to Reinforcement Learning] part.1-Epsilon-Greedy Algorithm in Bandit Game
[Introduction to element decomposition] Let's arrange time series analysis methods in R and python ♬
Differences in behavior between append () and "+ =" operators when adding data to a list in Python