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:
import sys
input = sys.stdin.readline


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


def selectionSort(N, P):
    change_count = 0
    for i in range(0, N):
        minj = i

        # print(P[i:N])
        # print(min(P[i:N]))

        for j in range(i, N):
            if P[j] < P[minj]:
                minj = j

        if i != minj:
            tmp = P[i]
            P[i] = P[minj]
            P[minj] = tmp
            change_count += 1

        # print(' '.join(map(str, P)))

    print(' '.join(map(str, P)))
    print(change_count)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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, n):
    if a[j] < a[minj]:
      minj = j
  if i != minj:
    a[minj], a[i] = a[i], a[minj]
    ans += 1

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

Submission status: Accepted

Solution code:
num_counts = int(input())
nums = list(map(int, input().split()))
swap_counts = 0

for i in range(num_counts):
    mini = i
    for j in range(i, num_counts):
        if nums[j] < nums[mini]:
            mini = j
    if i == mini:
        continue
    nums[i], nums[mini] = nums[mini], nums[i]
    swap_counts += 1
    
for num in nums[:-1]:
    print(num, end=' ')
print(nums[-1])
print(swap_counts)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
count = 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 A[i] != A[minj]:
        count += 1
print(' '.join([str(x) for x in A]))
print(count)

Submission status: Accepted

Solution code:
#選択ソート
n=int(input())
a=[int(i) for i in input().split()]
k=0
for i in range(n):
    l=0
    min=i
    for j in range(n-i-1):
        if a[i+j+1]<a[min]:
            min=i+j+1
            l=1
    a[i],a[min]=a[min],a[i]
    if l==1:
        k+=1

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

Submission status: Accepted

Solution code:
def ss(A,n):
    cnt=0
    for k in range(n):
        minj=k
        for i in range(k,n):
            if A[i] < A[minj]:
                minj=i
        if minj !=k:
            tmp=A[k]
            A[k]=A[minj]
            A[minj]=tmp
            cnt=cnt+1
    return A,cnt

n=int(input())
A=list(map(int,input().split()))
A,cnt=ss(A,n)
As=[str(i)for i in A]
As = ' '.join(As)
print(As)
print(cnt)

Submission status: Accepted

Solution code:
def pintarr(arr):
    arr = [str(c) for c in arr]
    print(' '.join(arr))


def selecsort(arr, total):
    count = 0
    for i in range(total):
        minj = i
        for j in range(i, total):
            if arr[j] < arr[minj]:
                minj = j
        arr[i], arr[minj] = arr[minj], arr[i]
        if i != minj:
            count += 1
    pintarr(arr)
    print(count)


if __name__ == '__main__':
    t = int(input())
    a = input()
    a = [int(c) for c in a.split(' ')]
    selecsort(a, t)

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 A[i] != A[minj]:
        cnt += 1

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

Submission status: Accepted

Solution code:
#Selection sort

N=int(input())
A=list(map(int,input().split()))
counter=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:
        counter+=1

A_str=map(str,A)
string=" ".join(A_str)
print(string)
print(counter)

Submission status: Accepted

Solution code:
def selection_sort(a, n):
    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(" ".join([str(x) for x in a]))
    print(cnt)


def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    selection_sort(a, n)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
input()
A = [int(v) for v in input().split(" ")]

def swap(A, i, j):
    t = A[i]
    A[i] = A[j]
    A[j] = t

swap_count = 0
for i in range(0, len(A) - 1):
    minJ = i
    for j in range(i, len(A)):
        if A[j] < A[minJ]:
            minJ = j

    if i != minJ:
        swap(A, i, minJ)
        swap_count += 1

print(" ".join([str(v) for v in A]))
print(swap_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 A[i] != A[minj]:
        A[i], A[minj] = A[minj], A[i]
        count += 1
print(*A)
print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]

cnt = 0

for i in range(n):
    m = i
    for j in range(i + 1, n):
        if a[j] < a[m]:
            m = j

    if not m == i:
        a[m], a[i] = a[i], a[m]
        cnt += 1


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

Submission status: Accepted

Solution code:
#coding: utf-8

swap = 0
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
    minj = i
    for j in range(i, n):
        if a[j] < a[minj]:
            minj = j
    if minj > i:
	    temp = a[i]
	    a[i] = a[minj]
	    a[minj] = temp
	    swap += 1
print(' '.join([str(i) for i in a]))
print(swap)

Submission status: Accepted

Solution code:
def selectionSort(a, n):
    sw = 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 i != minj:
            sw += 1
    return sw


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

sw = selectionSort(a, n)
print(*a)
print(sw)

Submission status: Accepted

Solution code:
n=int(input())
box=list(map(int,input().split()))
count=0
for i in range(n):
    j=box[i:].index(min(box[i:]))+i
    box[i],box[j]=box[j],box[i]
    if i!=j:
        count +=1
print(*box)
print(count)

"""n=int(input())
box=list(map(int,input().split()))
count=0
for i in range(n):
    mini=i
    for j in range(i,n):
        if box[j]<box[mini]:
            mini=j
    if box[i]>box[mini]:
        box[i],box[mini]=box[mini],box[i]
        count +=1
print(*box)
print(count)"""

Submission status: Accepted

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


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
        A[i],A[minj]=A[minj],A[i]
        count = count + int( i != minj)
    return count

count=selectionSort(l,n)

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

Submission status: Accepted

Solution code:
def main(n):
	a = list(map(int,input().split()))
	ans = 0
	for i in range(n):
		minj = i
		for j in range(i, n):
			if a[j] < a[minj]:
				minj = j
		if minj == i:
			continue
		a[i], a[minj] = a[minj], a[i]
		ans += 1
	print(" ".join(map(str, a)))
	print(ans)

main(int(input()))

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+1,N):
        if A[minj]>A[j]:
            minj=j
    if i<minj:
        A[minj],A[i]=A[i],A[minj]
        count+=1
for i in range(N):
    if i<N-1:
        print(A[i],end=' ')
    else:
        print(A[i])
print(count)

Submission status: Accepted

Solution code:
# coding: utf-8

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:
            c += 1
            A[i], A[minj] = A[minj], A[i]
    print(' '.join(list(map(str, A))))
    print(c)


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

Submission status: Accepted

Solution code:

def selection_sort(arr, n):
    mini = 0
    sw = 0
    for i in range(n-1):
        mini = i
        for j in range(i, n):
            if arr[j] < arr[mini]:
                mini = j
        arr[i], arr[mini] = arr[mini], arr[i]

        if i != mini:
            sw += 1

    return sw



def print_result(arr, n):
    for i in range(n):
        if i > 0:
            print(' ', end='')
        print(arr[i], end='')
    print('\n', end='')



n = eval(input())
arr = [eval(x) for x in input().split()]
sw = selection_sort(arr, n)
print_result(arr, n)
print(sw)

Submission status: Accepted

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

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

print(' '.join([str(x) for x in a]))
print(count)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in 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

for i in range(N):
    print(A[i], end='')
    if i != N - 1:
        print(' ', end='')
print('')

print(count)

Submission status: Accepted

Solution code:
m=int(input())
n=list(map(int,input().split()))
cnt=0
for i in range(m-1):
  minj=i
  irekae=0
  for j in range(i+1,m):
    if n[j]<n[minj]:
      minj=j
      irekae=1
  if irekae:
    n[i],n[minj]=n[minj],n[i]
    cnt+=1

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

Submission status: Accepted

Solution code:
# ALDS1_2_B - Selection Sort
def main():
    N, *A = map(int, open(0).read().split())
    swap = 0
    for i in range(N):
        mi = i
        for j in range(i, N):
            if A[j] < A[mi]:
                mi = j
        if i != mi:
            A[i], A[mi] = A[mi], A[i]
            swap += 1
    print(*A)
    print(swap)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    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:
N = int(input())
A = [int(i) for i in 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 minj != i:
		cnt += 1
	v = A[i]
	A[i] = A[minj]
	A[minj] = v

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

Submission status: Accepted

Solution code:
num = int(input())

lst = [int(x) for x in input().split()]

def show():
    for i in range(len(lst)):
        print(lst[i], end="")
        if i < len(lst)-1:
            print(" ", end="")
        else:
            print()

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

show()
print(count)

Submission status: Accepted

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

print(*a)
print(cnt)

Submission status: Accepted

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

for i in range(n):
    minv = a[i]
    mink = i
    for j in range(i, n):
        if a[j] < minv:
            minv = a[j]
            mink = j
    if a[i] != minv:
        a[i], a[mink] = a[mink], a[i]
        co += 1

print(*a)
print(co)

Submission status: Accepted

Solution code:
def test():

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

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

	for i in range(n-1):
		print(f"{A[i]} ",end = '')
	print(f"{A[n-1]}")
	print(swap_count)


if __name__ == "__main__":
	test()

Submission status: Accepted

Solution code:
#! /usr/bin/python
# -*- coding: utf-8 -*-

N=int(input()) # 数列の長さ
A=list(map(int, input().split())) # 空白を区切文字として配列に格納

minj = 0
tmp = 0
cnt = 0

for i in range(0, N):
    minj = i
    for j in range(i, N):
        if A[j] < A[minj]:  #一番小さい要素よりも小さいものを記憶する
            minj = j
    if i != minj:
        cnt += 1
    tmp = A[i]  #18行目までで、一番小さい要素とi番目の要素を入れ替え
    A[i] = A[minj]
    A[minj] = tmp
    
print(*A)
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,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
    if i != minj:
        c+=1
    A[i],A[minj]=A[minj],A[i]
print(" ".join(map(str,A)))
print(c)

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+1,n):
		if A[j]<A[minj]:
			minj=j
	A[i],A[minj]=A[minj],A[i]
	if minj != i:
		count+=1
print(' '.join(list(map(str,A))))
print(count)

Submission status: Accepted

Solution code:
N = int(input())
*A, = 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 not minj == i:
        tmp = A[i]
        A[i] = A[minj]
        A[minj] = tmp
        count += 1
    
print(*A)
print(count)

Submission status: Accepted

Solution code:
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 i != mini:
            A[i], A[mini] = A[mini], A[i]
            cnt += 1
    return A, cnt

N = int(input())
A = [int(_) for _ in input().split()]

A, cnt = SelectionSort(A)
print(" ".join([str(i) for i in 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+1, N):
        if(A[j] <  A[minj]):
            minj=j

    if(minj !=i):
        tmp=A[i]
        A[i]=A[minj]
        A[minj]=tmp
        count=count+1

print(' '.join([str(a) for a in A]))
print("{}".format(count))

Submission status: Accepted

Solution code:

N = int(input())

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

num = 0

for i in range(N):

    minj = i

    for j in range(N-i):

        j += i

        if A[j] < A[minj]:

            minj = j

    if minj != i:
        num += 1

    oai = A[i]
    A[i] = A[minj]
    A[minj] = oai

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

Submission status: Accepted

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

def SelectionSort(A):
    N = len(A)
    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
    for k in range(N-1):
        print(A[k], end=" ")
    print(A[N-1])
    print(count)

SelectionSort(A)

Submission status: Accepted

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

cnt = 0
for i in range(r):
    min_j = i
    for j in range(i+1, r):
        if A[j] < A[min_j]:
            min_j = j
    if min_j > i:
        A[min_j], A[i] = A[i], A[min_j]
        cnt+=1
print(" ".join(map(str, A)))
print(cnt)

Submission status: Accepted

Solution code:
#selection sort


def printout(array,n):
    output = ""
    for i in range(n-1):
        output += str(array[i]) + " "
    output += str(array[N-1])
    print(output)


def linearsearch(subarray):
    # return index of minimum array element
    min = subarray[0]
    min_index = 0;
    for i in range(1,len(subarray)):
        if min > subarray[i]:
            min = subarray[i]
            min_index = i
    return min_index


N = int(input())
array = [int(i) for i in input().split()]

count,index,tmp = 0,0,0

for i in range(N):
    index = linearsearch(array[i:]) + i

    if index != i:
        #swap
        tmp = array[index]
        array[index] = array[i]
        array[i] = tmp
        count += 1

printout(array,N)
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 minj!=i:
        ab=a[i]
        a[i]=a[minj]
        a[minj]=ab
        cnt+=1

def convert(list):
    res=" ".join(map(str, list))
    return res
print(convert(a))
print(cnt)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split(' ')))
    c = 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]
            c += 1
    print(' '.join(list(map(str, a))))
    print(c)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
valuenum = int(input())
values = [int(value) for value in input().strip().split()]

exchange_count = 0

for i in range(valuenum):
  min_num = i
  for j in range(i + 1, valuenum):
    if values[j] < values[min_num]:
      min_num = j
  if values[i] > values[min_num]:
    values[i], values[min_num] = values[min_num], values[i]
    exchange_count += 1

ans = map(lambda value:str(value), values)

print(' '.join(ans).strip())
print(str(exchange_count))

Submission status: Accepted

Solution code:
def getInput():
    n = int(input())
    a = [int(i) for i in input().split(" ")]
    return n, a

def swap(x, y):
    v = x
    x = y
    y = v
    return x, y

def selectionSort(n, a):
    cnt = 0
    for i in range(n):
        id_min = i
        for j in range(i+1, n):
            if a[j] < a[id_min]:
                id_min = j
        if id_min != i:
            a[i], a[id_min] = swap(a[i], a[id_min])
            cnt += 1
    print(" ".join([str(i) for i in a]))
    print(cnt)
    return

n, a = getInput()
selectionSort(n, a)

Submission status: Accepted

Solution code:
def selectionSort(A, N):
    count = 0
    # preA = copy.deepcopy(A)
    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 i != minj:
            count += 1
        # if A != preA:
        #     count += 1
        # preA = copy.deepcopy(A)
        
    return count

def main():
    N = int(input())
    list = input().split(" ")
    list = [int(s) for s in list]
    
    count = selectionSort(list, N)

    for i in range(len(list)):
        if i == len(list)-1:
            print(list[i])
        else:
            print(list[i], end=" ")

    print(count)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
num_inputs = int(input())
array = input().split(' ')
count = 0

for i in range(num_inputs):
    minj = i
    for j in range(i, num_inputs):
        if int(array[j]) < int(array[minj]):
            minj = j
    
    if minj != i:
        buf = array[i]
        array[i] = array[minj]
        array[minj] = buf
        count += 1

print(' '.join(map(str, array)))
print(str(count))

Submission status: Accepted

Solution code:

p = int(input())
y = list(map(int, input().split()))
t = 0
for h in range(p):
    o = h
    for n in range(h, p):
        if y[n] < y[o]: o = n
    if h != o:
        y[h], y[o] = y[o], y[h]
        t += 1
print(*y)
print(t)

Submission status: Accepted

Solution code:
def selectionSort(A, N):
    cnt = 0
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j] < A[minj]:
                minj = j
        cnt = cnt + int(i != minj)
        A[i], A[minj] = A[minj], A[i]
    return cnt
N = int(input())
A = [int(i) for i in input().split()]
cnt = selectionSort(A, N)
  
for i in range(N):
    if i != 0:
        print(" ", end = "")
    print(A[i], end = "")
      
print()
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
ns=list(map(int,input().split()))
ans=0
for i in range(n):
    minj=i
    for j in range(i,n):
        if(ns[j]<ns[minj]):
            minj=j
    if i!=minj:
        ans+=1
        k=ns[i]
        ns[i]=ns[minj]
        ns[minj]=k
 
for i in range(n):
    if(i!=n-1):
        print(ns[i],end=' ')
print(ns[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 i!=minj:
            m=a[i]
            a[i]=a[minj]
            a[minj]=m
            count+=1
    return a,count

n=int(input())
a=list(map(int,input().split()))
ans=selectionsort(a,n)
print(" ".join(map(str,ans[0])))
print(ans[1])

Submission status: Accepted

Solution code:
def bubbleSort(A,N):
    global cnt
    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]
N = int(input())
A = list(map(int,input().split()))
cnt = 0
bubbleSort(A,N)
print(*A)
print(cnt)

Submission status: Accepted

Solution code:
def selectionSort(A, N):
    cnt = 0
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j] < A[minj]:
                minj = j
        cnt = cnt + int(i != minj)
        A[i], A[minj] = A[minj], A[i]
    return cnt
N = int(input())
A = [int(i) for i in input().split()]
cnt = selectionSort(A, N)
 
for i in range(N):
    if i != 0:
        print(" ", end = "")
    print(A[i], end = "")
     
print()
print(cnt)

Submission status: Accepted

Solution code:
def selectionSort(A, N):
    cnt = 0
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j] < A[minj]:
                minj = j
        cnt = cnt + int(i != minj)
        A[i], A[minj] = A[minj], A[i]
    return cnt
 
N = int(input())
A = [int(i) for i in input().split()]
cnt = selectionSort(A, N)
 
for i in range(N):
    if i != 0:
        print(" ", end = "")
    print(A[i], end = "")
     
 
print()
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = []
flag = 0
m = 0

for i in range(n):
    x = a.index(min(a))
    if a[0] > a[x]:
      a[0], a[x] = a[x], a[0]
      flag = flag + 1
    b.append(a[0]) 
    del a[0]
      
for k in range(n):
    print(b[k], end="")
    if k == n-1:
        print("")
    else:
        print(" ", end="")
print(flag)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

print(' '.join(map(str, nums)))
print(count)

Submission status: Accepted

Solution code:
n = int(input())

A = [int(s) for s in input().split()]

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

print(*A)
print(cnt)

Submission status: Accepted

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

def sort(a):
	count = 0
	for n in range(len(a)):
		min_n = n
		for m in range(n, len(a)):
			if a[m] < a[min_n]:
				min_n = m
		if n != min_n:
			count += 1
			a[n], a[min_n] = a[min_n], a[n]
	return count

count = sort(A)
print(*A)
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
c=0
a=list(map(int,input().split()))
for i in range(n):
    m=i
    for j in range(i,n):
        if a[m]>a[j]:m=j
    if i!=m:a[m],a[i]=a[i],a[m];c+=1
print(*a)
print(c)

Submission status: Accepted

Solution code:
def selectionSort(A, N):
    swap_count = 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 i != minj:
            swap_count += 1

    return swap_count


if __name__ == "__main__":
    N = int(input())
    numbers = input().split(" ")
    for i in range(len(numbers)):
        numbers[i] = int(numbers[i])

    swap_count = selectionSort(numbers, N)

    print(*numbers)
    print(swap_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[minj]>A[j]:
      minj = j
  _=A[minj]
  if minj != i:
    A[minj]=A[i]
    A[i]=_
    cnt+=1
print(*A)
print(cnt)

Submission status: Accepted

Solution code:
"""
1 selectionSort(A, N) // N個の要素を含む0-オリジンの配列A
2   for i が 0 から N-1 まで
3     minj = i
4     for j が i から N-1 まで
5       if A[j] < A[minj]
6         minj = j
7     A[i] と A[minj] を交換
"""
def selectionSort(A, N):
    count = 0
    B = []
    for i in range(N):
        minj = i
        for j in range(i, N):
            if A[j] < A[minj]:
                minj = j
        out = A[i]
        A[i] = A[minj]
        A[minj] = out
        if(A[i]!=A[minj]):
            count += 1
            
    print(' '.join(map(str, A)))
    print(str(count))
    
if __name__ == '__main__':
    N = int(input()) #1行目のNを取得する
    M = list(map(int,input().split()))
    selectionSort(M, N)

Submission status: Accepted

Solution code:
def selection_sort(elems: list) -> list:
    swap_num = 0
    for i in range(0, len(elems)):
        mini = i
        for j in range(i + 1, len(elems)):
            if elems[j] < elems[mini]:
                mini = j
        if i != mini:
            elems[i], elems[mini] = elems[mini], elems[i]
            swap_num += 1
    return [elems, swap_num]


if __name__ == "__main__":
    elem_num = int(input())
    elems = list(map(lambda x: int(x), input().split()))
    elems, swap_num = selection_sort(elems)
    print(" ".join([str(elem) for elem in elems]))
    print(f"{swap_num}")

Submission status: Accepted

Solution code:
def show(A):
    L = list(map(str, A))
    print(" ".join(L))

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

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

Submission status: Accepted

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

selection_sort = A[:]

counter = 0
for i in range(len(selection_sort)):
    v = selection_sort[i]
    min_v = v
    min_i = i
    for j in range(i, len(selection_sort)):
        if min_v > selection_sort[j]:
            min_v = selection_sort[j]
            min_i = j
    if v != min_v:
        counter += 1
        selection_sort[i], selection_sort[min_i] = selection_sort[min_i], selection_sort[i]
        
print(' '.join(map(str, selection_sort)))
print(counter)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    array = [int(i) for i in input().split(' ')]

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 选择排序
def selection_sort(arr):
    swap_cnt = 0
    for i in range(0, len(arr)):
        min_index = i
        for j in range(i + 1, len(arr)):
            if arr[min_index] > arr[j]:
                min_index = j
        if min_index != i:
            arr[i], arr[min_index] = arr[min_index], arr[i]
            swap_cnt += 1
    return swap_cnt

if __name__ == '__main__':
    N = int(input())
    arr = list(map(int, input().split()))
    cnt = selection_sort(arr)
    print(' '.join(map(str, arr)))
    print(cnt)

Submission status: Accepted

Solution code:
n= int(input())
a=list(map(int,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
	if i != minj:
		c+=1
	a[i],a[minj]=a[minj],a[i]

print(*a)
print(c)

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
nums = [int(_) for _ in stdin.readline().rstrip().split()]

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

ret, count = select_sort(nums, N)
print(*ret)
print(count)