Problem:


Selection Sort

Write a program of the Selection Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:

SelectionSort(A)
1 for i = 0 to A.length-1
2     mini = i
3     for j = i to A.length-1
4         if A[j] N, the number of elements in the sequence.

In the second line, N elements of the sequence are given separated by space characters.

Output

The output consists of 2 lines. 

In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.

In the second line, please print the number of swap operations.

Constraints

1 ≤ N ≤ 100

Sample Input 1

6
5 6 4 2 1 3

Sample Output 1

1 2 3 4 5 6
4

Sample Input 2

6
5 2 4 6 1 3

Sample Output 2

1 2 3 4 5 6
3



Sample input:
6
5 6 4 2 1 3


Sample output:
1 2 3 4 5 6
4

Submission status: Accepted

Solution code:
def SelectionSort(A):
    count_swap = 0
    for i in range(len(A)):
        mini = i
        for j in range(i,len(A)):
            if A[j] < A[mini]:
                mini = j
        if i != mini:
            count_swap += 1
        A[i],A[mini] = A[mini],A[i]
    #print(count_swap)
    return A,count_swap #変更を保存 # 複数の戻り値を返せる
    

try:
    N = int(input())
    array = list(map(int,input().split()))
except ValueError:
    print('数値以外が入力されました')

result = SelectionSort(array)
print(*result[0])
#print(*array)

print(result[1])

Submission status: Accepted

Solution code:
N = int(input())
number = list(map(int,input().split()))

count = 0
for i in range(0,len(number)):
    mini = i
    for j in range(i,len(number)):
        if number[j] < number[mini]:
            mini = j
    if mini != i:
        count += 1
        temp = number[i]
        number[i] = number[mini]
        number[mini] = temp


print(" ".join(map(str,number)))
print (count)

Submission status: Accepted

Solution code:
def selectionSort(A, N):
    c = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if A[j] < A[minj]:
                minj = j
        if i != minj:
            A[i], A[minj] = A[minj], A[i]
            c += 1
    return c

N = int(input())
A = list(map(int, input().split()))
c = selectionSort(A, N)
for i in range(N):
    if i == N - 1:
        print(A[i])
    else:
        print(A[i], end=' ')
print(c)

Submission status: Accepted

Solution code:
from sys import stdin
N = int(input())
nums = list(map(lambda x: int(x), stdin.readline().split(' ')))
count = 0
for i in range(N-1):
    min = nums[i]
    min_id = i
    for j in range(i+1, N):
        if min > nums[j]:
            min = nums[j]
            min_id = j
    if i != min_id:
        nums[i], nums[min_id] = nums[min_id], nums[i]
        count += 1
output = ""
for i in nums:
    output += str(i) + ' '
print(output[:-1])
print(count)

Submission status: Accepted

Solution code:
length_list = int(input())
num_list = list(map(int, input().split()))

count = 0

for i in range(length_list):
    minj = i
    for j in range(i,length_list):
        if num_list[j] < num_list[minj]:
            minj = j
    if i != minj:
        num_list[i],num_list[minj] = num_list[minj],num_list[i]
        count += 1
print(" ".join(map(str,num_list)))
print(count)

Submission status: Accepted

Solution code:
# ALDS1_2_B.py
def selectionSort(A,N):
    cnt=0
    for i in range(N):
        minj = i
        for j in range(i+1,N):
            if A[j] < A[minj]:
                minj = j
        if minj != i:
            tmp = A[i] 
            A[i] = A[minj]
            A[minj] = tmp
            cnt += 1
    return A,cnt
N = int(input())
A = list(map(int,input().split()))
A,cnt = selectionSort(A,N)
print(*A)
print(cnt)

Submission status: Accepted

Solution code:
def selection_sort(A, N):
  sw = 0
  for i in range(0,N-1):
    minj = i
    for j in range(i,N):
      if A[j] < A[minj]:
        minj = j
    A[i], A[minj] = A[minj], A[i]
    if i != minj:
      sw += 1
  print(*A)
  print(sw)

N = int(input())
A = list(map(int, input().split()))
selection_sort(A, N)

Submission status: Accepted

Solution code:
n = int(input())
t = list(map(int, input().split(' ')))
x = 0
for i in range(n):
    kome = i
    for j in range(i,n):
        if t[j] < t[kome]:
            kome = j
    if t[i] > t[kome]:
        t[i],t[kome] = t[kome],t[i]
        x = x + 1
t = [str(i) for i in t]
print(' '.join(t))
print(x)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
cnt = 0
for i in range(n):
    minj = i
    for j in range(i, n):
        if A[j] < A[minj]:
            minj = j
    A[i], A[minj] = A[minj], A[i]
    if minj != i:
        cnt += 1
print(' '.join(map(str, A)))
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
lst=list(map(int,input().split()))

count=0
for i in range(n):
    minj=i
    for j in range(i,n):
        if lst[j]<lst[minj]:
            minj=j
    if i!=minj:
        lst[i],lst[minj]=lst[minj],lst[i]
        count+=1

print(" ".join(map(str,lst)))
print(count)

Submission status: Accepted

Solution code:
input()
A = [int(x) for x in list(input().split())]
n = len(A)
counter = 0

for i in range(0, n-1, 1):
    minj = i
    for j in range(i, n, 1):
        if A[j] < A[minj]:
            minj = j
    if i == minj:
        continue
    tmp = A[i]
    A[i] = A[minj]
    A[minj] = tmp
    counter += 1

print(*A)
print(counter)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
count = 0

for i in range(n):
    minj = i
    for j in range(i, n):
        if A[j] < A[minj]:
            minj = j
    if i != minj:
        a = A[i]
        A[i] = A[minj]
        A[minj] = a
        count += 1
        
print(' '.join(map(str, A)))
print(count)

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def selection_sort(A, N):
    cnt = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if A[j] < A[minj]:
                minj = j
        if i != minj:
            A[i], A[minj] = A[minj], A[i]
            cnt += 1
    return cnt


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = il()
    cnt = selection_sort(A, N)
    print(*A)
    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def selectionSort(A):
    counter = 0
    for i, _ in enumerate(A):
        min_j = i
        for j in range(i, len(A)):
            if A[j] < A[min_j]:
                min_j = j
        A[i], A[min_j] = A[min_j], A[i]
        if i != min_j:
            counter += 1
    return A, counter
    
N = int(input())
A = list(map(int, input().split()))

ans = selectionSort(A)
print(' '.join(str(a) for a in ans[0]))
print(ans[1])

Submission status: Accepted

Solution code:
n = int(input())
num = list(map(int, input().split()))
cnt = 0
for i in range(n):
    minj = i
    for j in range(i+1,n):
        if num[j] < num[minj]:
            minj = j
    if i != minj:
        num[i], num[minj] = num[minj], num[i]
        cnt+=1
print(" ".join(map(str, num)))
print(cnt)

Submission status: Accepted

Solution code:
def selection_sort(array):
    counter = 0
    for i in range(len(array)):
        # print(f"\n----- i: {i} -----")
        
        # iより後ろで最も小さい数のindexを探す
        j_min = i
        for j in range(i, len(array)):
            # print("j:", j)
            if array[j] < array[j_min]:
                j_min = j
            # print(f'j_min: {j_min} ({array[j_min]})')
        
        # print(f'arrray[i]: {array[i]}, array[j_min]: {array[j_min]}')
        if array[i] > array[j_min]:
            array[i], array[j_min] = array[j_min], array[i]
            # print(f'change to: {array}')
            counter += 1
            
    return array, counter

N = int(input())
A = list(map(int, input().split()))
# print(f'array: {A}')

A_new, num_ans = selection_sort(A)

print(" ".join(map(str, A_new)))
print(num_ans)

Submission status: Accepted

Solution code:
def bubbleSort(A, N):
    flag = 1
    while flag:
        flag = 0
        for j in range(N-1, 0, -1):
            if A[j] < A[j-1]:
                A[j], A[j-1] = A[j-1], A[j]
                flag = 1


def selectionSort(A, N):
    count =0
    for i in range(N):
        minidx = i
        for j in range(i,N):
            if A[j] < A[minidx]:
                minidx = j
        if minidx == i:
            continue
        A[i],A[minidx] = A[minidx],A[i]
        count += 1
    return count



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

count = selectionSort(A, N)
print(" ".join(map(str, A)))
print(count)

Submission status: Accepted

Solution code:
n = int(input())

l = list(map(int, input().split()))
count = 0

for i in range(n):
    mini = i
    for j in range(i,n):
        if l[j] < l[mini]:
            mini = j
    if i != mini:
        l[i], l[mini] = l[mini], l[i]
        count += 1

print(" ".join(map(str, l)))
print(count)

Submission status: Accepted

Solution code:
import math

N = input()
A = map(int, raw_input().split())
c=0
for i in range(0,N):
  minj = i
  for j in range(i,N):
    if A[j] < A[minj]:
      minj=j
  A[i],A[minj]=A[minj],A[i]
  if minj!=i:
    c+=1

print ' '.join(map(str, A))
print c

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

counter = 0
for i in range(N-1):
    min_index = i
    for j in range(i,N):
        v = A[j]
        min = A[min_index]
        if A[j] < A[min_index]:
            min_index = j
    if min_index != i:
        temp = A[min_index]
        A[min_index] = A[i]
        A[i] = temp
        counter = counter + 1

print(' '.join([str(a) for a in A]))
print(counter)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


def selection_sort(A):
    counter = 0
    for i in range(len(A)):
        _min = i
        for j in range(i, len(A)):
            if A[j] < A[_min]:
                _min = j
        A[i], A[_min] = A[_min], A[i]
        if i != _min:
            counter += 1
    print(" ".join([str(a) for a in A]))
    print(counter)
    return A


def main():
    _ = input()
    A = [int(i) for i in input().strip().split()]
    selection_sort(A)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n, *aa = map(int, open(0).read().split())

count = 0
for i in range(n):
    mini = i
    for j in range(i+1, n):
        if aa[j] < aa[mini]:
            mini = j
    if mini != i:
        aa[i], aa[mini] = aa[mini], aa[i]
        count += 1

print(*aa)
print(count)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int, input().split()))

count = 0
for idx1 in range(0, len(arr)):
    mins = idx1
    for idx2 in range(idx1, len(arr)):
        if arr[idx2] < arr[mins]:
            mins = idx2
    
    if mins != idx1:
        arr[idx1], arr[mins] = arr[mins], arr[idx1]
        count += 1

print(" ".join(map(str, arr)), count, sep="\n")

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
c = 0

for i in range(n-1):
    minj = i
    for j in range(i, n):
        if A[j] < A[minj]:
            minj = j
    if minj != i:
        A[i], A[minj] = A[minj], A[i]
        c += 1
print(*A, sep=' ')
print(c)

Submission status: Accepted

Solution code:
def selection_sort(A):
    cnt = 0
    for i in range(0, len(A)):
        min = i
        for j in range(i + 1, len(A)):
            if A[min] > A[j]:
                min = j
        if min != i:
            A[i], A[min] = A[min], A[i]
            cnt += 1
    print(' '.join(map(str, A)))
    print(cnt)


if __name__ == '__main__':
    n = int(input())
    l = list(map(int, input().split()))
    selection_sort(l)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]


while True:
    if len(A) == N:
        break
    else:
        print("Input again!")
        A = [int(x) for x in input().split()]


def SelectionSort(A,N):
    count = 0
    for i in range(0,N,1):
        #print(i)
        minj = i
        for j in range(i+1,N,1):
            if A[j] < A[minj]:
                minj = j
        A[i],A[minj] = A[minj],A[i]
        #count += 1
        if i != minj:
            count += 1
    return A,count

A,count = SelectionSort(A,N)
print(" ".join(map(str,A)))
print(count)

Submission status: Accepted

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

cnt = 0
for i in range(n):
    minj = i
    for j in range(i, n):
        if A[j] < A[minj]:
            minj = j
    if i != minj:
        cnt += 1
    tmp = A[i]
    A[i] = A[minj]
    A[minj] = tmp
    
for k in range(n-1):
    print(A[k], end=' ')
print(A[n-1])
print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
cnt=0
for i in range(N):
  minj = i
  for j in range(i,N):
    if A[j] < A[minj]:
      minj = j
  if i != minj:
    k=A[i]
    A[i]=A[minj]
    A[minj]=k
    cnt+=1
print(*A)
print(cnt)

Submission status: Accepted

Solution code:
def main():
    num = int(input())
    sort_nums = list(map(int, input().split()))
    
    min_num_index = 0
    temp_counter = 0
    for i in range(0, num):
        min_num_index = i
        for j in range(i, num):
            if sort_nums[j] < sort_nums[min_num_index]:
                min_num_index = j

        if not sort_nums[i] == sort_nums[min_num_index]:
            temp_counter += 1

            temp = sort_nums[i]
            sort_nums[i] = sort_nums[min_num_index]
            sort_nums[min_num_index] = temp
        
    print(" ".join([str(n) for n in sort_nums]))
    print(temp_counter)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = [int(x) for x in input().split()]

cnt = 0
for i in range(n - 1):
    k = i
    for j in range(i + 1, n):
        if A[j] < A[k]:
            k = j

    if i != k:
        A[i], A[k] = A[k], A[i]
        cnt += 1

print(*A)
print(cnt)

Submission status: Accepted

Solution code:
def selection_sort(A, N):
    change_cnt = 0
    for i in range(0, N-1):
        min_idx = i
        for j in range(i+1, N):
            if A[j] < A[min_idx]:
                min_idx = j
        A[i], A[min_idx] = A[min_idx], A[i]
        if i != min_idx:
            change_cnt += 1
    print(" ".join([str(a) for a in A]))
    print(change_cnt)
N = int(input().rstrip())
A = [int(a) for a in input().rstrip().split(" ")]
selection_sort(A, N)

Submission status: Accepted

Solution code:
def selectionSort(A,N):
    count = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if A[j] < A[minj]:
                minj = j
        
        if i != minj:
            A[i], A[minj] = A[minj], A[i]
            count += 1

    print(*A)
    print(count)

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

selectionSort(A, N)

Submission status: Accepted

Solution code:
# Selection Sort
N = int(input())
A = list(map(int, input().split()))
swapCnt = 0
for i in range(N):
    minNum = i
    for j in range(i, N):
        if A[j] < A[minNum]:
            minNum = j
    if minNum != i:
        A[minNum], A[i] = A[i], A[minNum]
        swapCnt += 1
print(' '.join(map(str, A)))
print(swapCnt)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

a = 0
for i in range(len(A)-1):
  mini = i
  minv = A[i]
  for j in range(i, len(A)):
    if minv > A[j]:
      mini = j
      minv = A[j]
  if i != mini:
    A[i], A[mini] = A[mini], A[i]
    a += 1

print(" ".join(list(map(str, A))))
print(a)

Submission status: Accepted

Solution code:
def selectionSort(a, n):
    cnt = 0
    for i in range(n-1):
        minj = i
        for j in range(i, n):
            if a[j] < a[minj]:
                minj = j
        a[i], a[minj] = a[minj], a[i]
        if i != minj:
            cnt += 1

    print(' '.join(map(str, a)))
    print(cnt)

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

selectionSort(a, n)

Submission status: Accepted

Solution code:
n=int(input())
num_list=list(map(int,input().split()))

count=0
for i in range(0,len(num_list)):
    minj=i
    for j in range(i+1,len(num_list)):
        if num_list[minj]>num_list[j]:
            minj=j
    tmp=num_list[minj]
    num_list[minj]=num_list[i]
    num_list[i]=tmp
    if minj!=i:
        count+=1

ans=str(num_list[0])
for i in range(1,len(num_list)):
    ans+=" "+str(num_list[i])
print(ans)
print(count)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = 0

for i in range(N):
    minj = i
    for j in range(i, N):
        if A[j] < A[minj]:
            minj = j
    if i != minj:
        A[i], A[minj] = A[minj], A[i]
        count += 1

print(*A)
print(count)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    cnt = 0

    for i in range(N):
        temp = i
        minj = A[i]
        for j in range(i+1,N):
            if A[j] < minj:
                minj = A[j]
                temp = j

        if temp != i:
            temp2 = A[i]
            A[i] = A[temp]
            A[temp] = temp2
            cnt += 1

    print(' '.join(map(str,A)))
    print(cnt)


main()

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def main():
    N = int(input())
    A = list(map(int, input().split()))

    ans = 0
    for i in range(N - 1):
        min_j = i
        for j in range(i, N):
            if A[j] < A[min_j]:
                min_j = j
        if min_j != i:
            A[i], A[min_j] = A[min_j], A[i]
            ans += 1

    print(" ".join(map(str, A)))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

def selectionSort(A,N):
    cnt=0
    for i in range(N):
        minj=i
        for j in range(i,N):
            if A[j]<A[minj]:
                minj=j
        if i != minj:
            cnt+=1
        A[i],A[minj]=A[minj],A[i]
    return(cnt)
a=int(input())
b=list(map(int,input().split(" ")))
c=selectionSort(b,a)
print(" ".join(list(map(str,b))),c,sep="\n")

Submission status: Accepted

Solution code:
from sys import stdin


def selection_sort(A, N):
    count = 0
    for i in range(N):
        minj = i
        for j in range(i,N):
            if A[minj] > A[j]:
                minj = j
        
        if A[minj] != A[i]:
            count += 1
            A[minj], A[i] = A[i], A[minj]
        
    return count
                
def main():
    read = stdin.readline
    N = int(read())
    data = list(map(int, read().split(' ')))
    count = selection_sort(data, N)
    for i in range(N-1):
        print(data[i], end=' ')
    print(data[N-1])
    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=0

for i in range(n):
  minj=i
  for j in range(i+1,n):
    if A[j]<A[minj]:
      minj=j
  if i==minj: continue
  tmp=A[i]
  A[i]=A[minj]
  A[minj]=tmp
  ans+=1
print(*A)
print(ans)

Submission status: Accepted

Solution code:
#ALDS1_2_B:   Selection Sort

def selectionSort(A, n):
    res = 0
    for i in range(n):
        minj = i
        for j in range(i, n):
            if A[j] < A[minj]:
                minj = j
                
        if A[i] != A[minj]:
            A[i], A[minj] = A[minj], A[i]        
            res += 1
        
    return A, res

n = int(input())
l = list(map(int, input().split()))
res_l, res = selectionSort(l, n)
print(*res_l)
print(res)

Submission status: Accepted

Solution code:
def selectionSort(table):
    swap_count = 0
    for nAchieve in range(len(table)):
        min_index = nAchieve
        for i in range(nAchieve, len(table)):
            if table[i] < table[min_index]:
                min_index = i
        if min_index != nAchieve:
            table[min_index], table[nAchieve] = table[nAchieve], table[min_index]
            swap_count += 1
    return swap_count


element = int(input())
table = list(map(int, input().split()))
swap_count = selectionSort(table)

print(' '.join(map(str, table)))
print(swap_count)

Submission status: Accepted

Solution code:
def SelectionSort(A, N):
    cnt = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if A[j] < A[minj]:
                minj = j
        if minj != i:
            tmp = A[i]
            A[i] = A[minj]
            A[minj] = tmp
            cnt += 1

    print(*A)
    print(cnt)


def main():
    N = int(input())
    A = list(map(int, input().split()))
    SelectionSort(A, N)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
c = 0

for i in range(n):
    min = i
    for j in range(i+1,n):
        if x[min] > x[j]:
            min = j
    if i != min:       
        x[i],x[min] = x[min],x[i]
        c += 1

print(*x)
print(c)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin

def selection_sort(A, N):  #N個の要素を含む0-オリジンの配列 A
  exchange_cnt=0
  for i in range(0, N):
    minj = i
    for j in range(i, N):
      if A[j] < A[minj]:
        minj = j
    else:
      if i != minj:
        A[i], A[minj] = A[minj], A[i]
        exchange_cnt += 1
  return A,exchange_cnt

n = int(stdin.readline().rstrip())
A = list(map(int, stdin.readline().split()))
ans = selection_sort(A, n)
print(' '.join(map(str,ans[0])))
print(ans[1])

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

cnt = 0
for i in range(N):
    index = i
    for j in range(i, N):
      if A[j] < A[index]:
        index = j
    if i != index:
      A[i], A[index] = A[index], A[i]
      cnt += 1
        
print(' '.join(map(str, A)))
print(cnt)

Submission status: Accepted

Solution code:
from sys import stdin
N = int(input())
A = [int(x) for x in stdin.readline().split(' ')]

cnt = 0
for i in range(0, N):
    minj = i
    for j in range(i, N):
        if (A[minj] > A[j]):
            minj = j
    if (minj != i):
        A[minj], A[i] = (A[i], A[minj])
        cnt += 1

print(' '.join(map(str, A)))
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
a = list(int(x) for x in input().split())
count = 0
for i in range(N):
  min_a = i
  for j in range(i,N):
    if a[j] < a[min_a]:
      min_a = j
  if min_a != i:
    count += 1
  tmp = a[i]
  a[i] = a[min_a]
  a[min_a] = tmp

for k in range(N):
  if k == N-1:
    print(a[k],end ="")
  else:
    print(a[k],end =" ")
    
print()
print(count)

Submission status: Accepted

Solution code:

# 出題: プロコンのためのアルゴリズムとデータ構造 3.4

def printList(array):
    print(' '.join(map(str, array)))

# N個の要素を持つ配列A

def selectionSort(A, N):
    swapCount = 0
    for index in range(0, N):
        minj = index
        for j in range(index, N):
            if A[j] < A[minj]:
                minj = j

        if index != minj:
            swapCount += 1

        A[index], A[minj] = A[minj], A[index]

    printList(A)
    print(str(swapCount))


if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    selectionSort(A, N)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int, input().split()))
change = 0

for i in range(n):
    minj = i
    for j in range(i, n):
        if arr[j] < arr[minj]:
            minj = j
    else:
        if i != minj:
            arr[i], arr[minj] = arr[minj], arr[i]
            change += 1

def print_space(li):
    for i in range(len(li)-1):
        print(li[i], end=' ')
    else:
        print(li[-1])

print_space(arr)
print(change)

Submission status: Accepted

Solution code:
def selectionSort(A, N):
    cnt = 0
    for i in range(N):
        minJ = i
        for j in range(i, N):
            if A[minJ] > A[j]:
                minJ = j
        if A[i] != A[minJ]:
            tmp = A[i]
            A[i] = A[minJ]
            A[minJ] = tmp
            cnt += 1
    return A, cnt


def resolve():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans, cnt = selectionSort(A, N)
    print(*ans)
    print(cnt)


resolve()

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
cnt = 0
for i in range(n):
    min_j = i
    for j in range(i, n):
        if A[j] < A[min_j]:
            min_j = j
    if i != min_j:
        A[i], A[min_j] = A[min_j], A[i]
        cnt += 1
print(*A)
print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
count=0
for i in range(N):
    minj = i
    for j in range(i,N):
        if A[j] < A[minj]:
            minj=j
    if i!=minj:
        count+=1
    R=A[i]
    A[i]=A[minj]
    A[minj]=R
Z=[str(a) for a in A]
Z=" ".join(Z)
print(Z)

print(count)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = 0

for i in range(N-1):
    minj = i
    for j in range(i, N):
        if A[j]<A[minj]:
            minj = j
    if A[i] != A[minj]:
        A[i], A[minj] = A[minj], A[i]
        count += 1
print(*A)
print(count)

Submission status: Accepted

Solution code:
def min_index(A):
    index = 0
    m = A[0]
    for i in range(1,len(A)):
        if A[i] < m:
            m = A[i]
            index = i
    return index

def selectionsort(A,N):
    count = 0
    for i in range(N-1):
        j = min_index(A[i+1:])
        if A[i] > A[i+j+1]:
            #A[i],A[i+j+1] = A[i+j+1],A[i]
            count += 1
            A[i],A[i + min_index(A[i+1:]) + 1] = A[i + min_index(A[i+1:]) + 1],A[i]
    return A,count

if __name__ == '__main__':
    N = int(input())
    A = [int(i) for i in input().split()]
    ans,count = selectionsort(A,N)
    for i in range(N):
        if i != N - 1:
            print(ans[i],end = " ")
        else:
            print(ans[i])
    print(count)

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_B

from sys import stdin
input = lambda: stdin.readline().rstrip()

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

cnt = 0
for i in range(0, N-1):
    minj = i
    for j in range(i+1, N):
        if A[j] < A[minj]:
            minj = j

    if i != minj:
        w = A[i]
        A[i] = A[minj]
        A[minj] = w
        cnt = cnt + 1

print(' '.join(map(str,A)))
print(cnt)

Submission status: Accepted

Solution code:
def selection_sort(A,N):
    cnt = 0
    for i in range(N):
        minj = i
        for j in range(i+1,N):
            if A[j] < A[minj]:
                minj = j
        if minj != i:
            A[i],A[minj] = A[minj],A[i]
            cnt += 1
    return A, cnt

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

A,cnt = selection_sort(A,N)
print(*A)
print(cnt)

Submission status: Accepted

Solution code:
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect
import functools

def SelectionSort(A):
    cnt = 0
    for i in range(len(A)):
        mini = i
        for j in range(i,len(A)):
            if A[j] < A[mini]:
                mini = j
        if mini != i:
            tmp = A[i]
            A[i] = A[mini]
            A[mini] = tmp
            cnt += 1
    return cnt
n = int(input())
A = list(map(int,input().split()))
cnt = SelectionSort(A)
print(*A)
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

cnt = 0
for i in range(n):
    minj = i
    for j in range(i, n):
        if a[j] < a[minj]:
            minj = j
    if i != minj:
        a[i], a[minj] = a[minj], a[i]
        cnt += 1

print(' '.join(map(str, a)))
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = 0
for i in range(N-1):
    minidx = i
    for j in range(i+1,N):
        if A[j] < A[minidx]:
            minidx = j
    if minidx != i:
        A[i], A[minidx] = A[minidx], A[i]
        count += 1
print(*A); print(count)

Submission status: Accepted

Solution code:
n = int(input())
l = []
for i in map(int, input().split()):
    l.append(i)

'''
n = 10
l = [9,5,10,3,6,7,4,1,2,8]
'''

start_index = 0
swap_cnt = 0

for j in range(start_index, n):
    if len(l[start_index:n]) == 1:
        break
    else:
        tmp = l[start_index]
        min_value = l[start_index]
        min_index = start_index

        for idx in range(start_index,n):
            if min_value > l[idx]:
                min_value = l[idx]
                min_index = idx

        l[start_index] = min_value
        l[min_index] = tmp

        if min_value != tmp:
            swap_cnt += 1

        start_index += 1

print(' '.join(map(str,l)))
print(swap_cnt)

Submission status: Accepted

Solution code:
from typing import List, Tuple


def selection_sort(A: List[int]) -> Tuple[List[int], int]:
    count: int = 0

    for i in range(len(A)):
        min_j: int = i
        for j in range(i, len(A)):
            if A[j] < A[min_j]:
                min_j = j
        if i != min_j:
            A[i], A[min_j] = A[min_j], A[i]
            count += 1

    return A, count


def main():
    _ = input()
    A: List[int] = list(map(int, input().split()))

    lst, count = selection_sort(A)
    print(*lst, sep=" ")
    print(count)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = LI()

    cnt = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if A[j]<A[minj]:
                minj = j
        if i!=minj:
            A[i], A[minj] = A[minj], A[i]
            cnt+=1

    print(*A)
    print(cnt)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(n-1) :
    minj = i
    for j in range(i+1, n) :
        if A[j] < A[minj] :
            minj = j
    if A[i] != A[minj] :
        A[i], A[minj] = A[minj], A[i]
        ans += 1
for i in range(n-1) :
    print(A[i], end=" ")
print(A[n-1])
print(ans)

Submission status: Accepted

Solution code:
def selectionSort(A,N):
    count = 0
    for i in range(N):
        minj = i
        for j in range(i,N):
            if A[j] < A[minj]:
                minj = j
        if minj != i:
            v = A[i]
            A[i] = A[minj]
            A[minj] = v
            count += 1
    print(" ".join(map(str,A)))
    print(count)
    
n = int(input())
numlist = [int(x) for x in input().split()]

selectionSort(numlist,n)