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:
n = int(input())
R = [int(x) for x in input().split()]#リスト内包表記
def selected_sort(a,n):
    count=0
    judge=False
    for i in range(0,n-1):#一個前までやれば全てソートが完了する
        minj=i#未ソート部の先頭をminとみなす
        for j in range(i,n):
            if a[j]<a[minj]:#
                minj=j#最小インデックスの更新
                judge=True#インデックスが更新されたかどうかの判定
        a[i],a[minj]=a[minj],a[i]#最小値と未ソートの先頭をswap
        if judge:
            count+=1
        judge=False
    print(*a)
    print(count)
        

selected_sort(R,n)

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
    else:
        if minj != i:
            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 = [int(x) for x 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 i != minj:
        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()))
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

print(*a)
print(cnt)

Submission status: Accepted

Solution code:
num = int(input(""))
seqs = list(map(int,input().split())) #แยกทุกตัวฝนบรรทัดแล้วเก็บค่าเป็น int 
count = 0
    #วน loop ตามจำนวนตัวเลขที่ input เข้ามาในบรรทัดแรก
for i in range(num): 
    #กำหนดให้ minindex = i ถ้ารอบแรกก็เป็น index ที่ 0 คือ เลขที่อยู่ตำแหน่งแรก
    minIndex = i 
    #วน loop ตามจำนวนตั้งแต่ 0-4 เพื่อใช้เป็นการดึง index
    #ของบรรทัดที่ input ข้อมูลตัวแรกเข้ามา
    for j in range(i,num):
    #ถ้าหากตำแหน่งที่ j น้อยกว่าตำแหน่งที่ minindex ให้เปลี่ยนค่า minindex เป็น j
        if seqs[j] < seqs[minIndex]:
            minIndex = j
            
    #ถ้าตำแหน่ง minindex ไม่เท่ากับ i และ  seqs[j] >= seqs[minIndex]
    #ให้ทำการ swap สลับตำแหน่ง        
    if minIndex != i:
        seqs[i],seqs[minIndex]=seqs[minIndex],seqs[i]
        count+=1
    #แสดงผล sequence ทั้งหมดออกมาเป็น string รวมถึงแสดงผลว่าทำการ swap ไปกี่รอบ
print(" ".join(map(str, seqs)))
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

indexMin = 0
count = 0


for start in range(n):
    indexMin = s[start:].index(min(s[start:])) + start
    s[start], s[indexMin] = s[indexMin], s[start]
    if start != indexMin:
        count+=1

print(*s)
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 minj != i:
        dummy = A[i]
        A[i] = A[minj]
        A[minj] = dummy
        count +=1
for i in range(N-1):
    print(f"{A[i]}",end=" ")
print(f"{A[N-1]}\n{count}")

Submission status: Accepted

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

count = 0
for a in range(len(A)-1):
    index_min = A[(a+1):].index(min(A[(a+1):]))+a+1
    # print(f'{min(A[a+1:])} {index_min}')
    if A[a] > A[index_min]:
        A[a],A[index_min] = A[index_min],A[a]
        count += 1
        # print(*A)
print(*A)
print(count)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
cnt=0
for i in range(n):
    mini=i
    for j in range(i,n):
        if a[mini]>a[j]:
            mini=j
    if mini!=i:
        a[i],a[mini]=a[mini],a[i]
        cnt+=1
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:
import math
import itertools

def main():
  n = int(input())
  A = list(map(int, input().split()))
  len_A = len(A)
  times = 0

  for i in range(len_A):
    min = i
    for j in range(i+1, len_A):
      if A[j] < A[min]:
        min = j
    
    if min != i:
      tmp = A[min]
      A[min] = A[i]
      A[i] = tmp
      times += 1

  print(*A)
  print(times)

if __name__ == '__main__':
  main()

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 A[i] != A[minj]:
            cnt += 1
        A[i], A[minj] = A[minj], A[i]
    print(*A)
    print(cnt)


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

selectionSort(A, N)

Submission status: Accepted

Solution code:
def selection_sort(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
    if a[i] != a[minj]:
      temp = a[i]
      a[i] = a[minj]
      a[minj] = temp
      cnt += 1
  print(' '.join(map(str,a)))
  print(cnt)
  
num = int(input())
array = list(map(int,input().split()))

selection_sort(array,num)

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:        
            ho = A[i]
            A[i] = A[minj]
            A[minj] = ho
            count += 1
    return A, count

n = int(input())
a = [ int(i) for i in input().split()]
b , count = selectionSort(a,n)
for i in range(len(b)):
    if i == len(b)-1:
        print(b[i])
    else:
        print(b[i],end=' ')            
print(count)

Submission status: Accepted

Solution code:
n = int(input())
As = input().split()
A = [int(i) for i in As]
c = 0

for i in range(n-1):
    mini = i
    j = i
    while n > j:
        if A[j] < A[mini]:
            mini = j
        j += 1
    if i != mini:
        w = A[i]
        A[i] = A[mini]
        A[mini] = w
        c += 1

for i in range(n-1):
    print("{} ".format(A[i]), end="")
print(A[n-1])
print(c)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, 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 mini != i:
        tmp = A[i]
        A[i] = A[mini]
        A[mini] = tmp
        cnt += 1

result = [str(i) for i in A]
print(' '.join(result))
print(cnt)

Submission status: Accepted

Solution code:
num = int(input())
seqs = list(map(int,input().split()))
count = 0
for i in range(num):
    minIndex = i
    for j in range(i,num):
        if seqs[j] < seqs[minIndex]:
            minIndex = j
    if minIndex != i:
        seqs[i],seqs[minIndex]=seqs[minIndex],seqs[i]
        count+=1

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

Submission status: Accepted

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

Submission status: Accepted

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

icnt = 0
for i in range(0, len(a)):
	imin = i
	for j in range(i + 1, len(a)):
		if a[j] < a[imin]:
			imin = j
	if imin != i:
		a[i], a[imin] = a[imin], a[i]
		icnt = icnt + 1

print(' '.join([str(n) for n in a]))
print(icnt)

Submission status: Accepted

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

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

print(" ".join([str(i) for i in a]))
print(cnt)

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

Submission status: Accepted

Solution code:
def selectionSort(A, N):
    count = 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:
            temp = A[i]
            A[i] = A[minj]
            A[minj] = temp
            count += 1
    return count

N = int(input())
A = list(map(int, input().split()))
c = selectionSort(A, N)
print(" ".join(map(str, A)))
print(c)

Submission status: Accepted

Solution code:
def selectionSort(A, n):
    k = 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:
            k += 1

    L=[str(a) for a in A]
    L=" ".join(L)
    print(L)
    print(k)



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

selectionSort(A, n)

Submission status: Accepted

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

for i in range(0, N):
    min_A = A[i]
    for j in range(i, N):
        if A[j] < min_A:
            min_A = A[j]
            num = j
    if min_A < A[i]:
        v = A[i]
        A[i] = min_A
        A[num] = v
        count += 1

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

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

Submission status: Accepted

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

swapcnt = 0

def bubbleSort(A):
  global N,swapcnt
  for i in range(N):
    for j in  range(N-1,i,-1):
      if A[j] < A[j-1]:
        x = A[j]
        A[j] = A[j-1]
        A[j-1] = x
        swapcnt += 1

def selectionSort(A):
  global N,swapcnt
  for i in range(N):
    minj = i
    for j in range(i,N):
      if A[j] < A[minj]:
        minj = j
    if minj != i :
      x = A[minj]
      A[minj] = A[i]
      A[i] = x
      swapcnt += 1

#bubbleSort(A)
selectionSort(A)
strA = [str(a) for a in A]
print(" ".join(strA))
print(swapcnt)

Submission status: Accepted

Solution code:
def selection_sort(lst, n):
    count = 0
    for i in range(n - 1):
        minj = i
        for j in range(n - i):
            if lst[minj] > lst[j + i]:
                minj = j + i
        if minj != i:        
            tmp = lst[i]
            lst[i] = lst[minj]
            lst[minj] = tmp
            count += 1
    map_lst = map(str,lst)
    print(" ".join(map_lst))
    print(count)

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

selection_sort(num_list, n)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = []
a = input().split()
a = list(map(int,a))
num = 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]
        num = num + 1

print(*a)
print(num)

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
    if minj != i:
        A[i],A[minj] = A[minj],A[i]
        count+=1
print(*A)
print(count)

Submission status: Accepted

Solution code:
def selectionSort(sort_list):
    count = 0

    for i in range(0, len(sort_list)):
        minj = i

        for j in range(i, len(sort_list)):
            if sort_list[j] < sort_list[minj]:
                minj = j

        if minj != i:
            tmp = sort_list[i]
            sort_list[i] = sort_list[minj]
            sort_list[minj] = tmp
            count += 1

    print(' '.join([str(i) for i in sort_list]))
    print(count)


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

    selectionSort(sort_list)

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 minj != i:
        count += 1
        a[i],a[minj] = a[minj],a[i]
print(*a)
print(count)

Submission status: Accepted

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

def trace():
    for i in range(len(R)):
        if i != len(R) - 1:
            print(R[i],end=' ')
        else:
            print(R[i])

count = 0
for i in range(0,n):
    minj = i
    for j in range(i,n):
        if R[j] < R[minj]:
            minj = j

    R[i],R[minj] = R[minj],R[i]
    if i != minj:
        count += 1
    


trace()
print(count)

Submission status: Accepted

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

for i in range(N):
    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]
        times += 1

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

Submission status: Accepted

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

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

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

    return(A, cnt)

out_A, cnt_sort = selectionSort(A,N)

out_A_str = list(map(str, out_A))
print(" ".join(out_A_str))
print(str(cnt_sort))

Submission status: Accepted

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

    nums[minj],nums[minv]=nums[minv],nums[minj]
    

def show(nums):
    for j in range(len(nums)):
        if j!=len(nums)-1:
            print(nums[j],end=' ')

        else:
            print(nums[j])

show(nums)
print(count)

Submission status: Accepted

Solution code:
# Problem - 選択ソート

def selection_sort(A, N):
    sort_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
            sort_count += 1
    return A, sort_count

# input
N = int(input())
a_nums = list(map(int, input().split()))

# initialization
ans_list, ans_count = selection_sort(a_nums, N)
print(" ".join(list(map(str, ans_list))))
print(ans_count)

Submission status: Accepted

Solution code:
# Selection Sort

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

def selectionSort(A):
	n = len(A)
	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
	return cnt

cnt = selectionSort(A)

print(*A)
print(cnt)

Submission status: Accepted

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

counter=0
for i in range(0,N-1):
    min_,min_index=min(S[i:]),S[i:].index(min(S[i:]))+i
    first_element,first_element_index=S[i],i
    
    if min_index!=first_element_index:
        counter+=1
        S[first_element_index],S[min_index]=S[min_index],S[first_element_index]
        
print(*S)
print(counter)

Submission status: Accepted

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

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

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:
        cnt += 1
    a[i],a[minj] = a[minj],a[i]
print(*a)
print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def selectionSort(A,N):
    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):
            t = A[i]
            A[i] = A[minj]
            A[minj] = t
            c+=1

    a = [str(i) for i in A]
    print(" ".join(a))
    print(c)

if __name__=="__main__":
    n = int(input())
    l = [int(x) for x in input().split()]
    selectionSort(l,n)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from copy import deepcopy

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

A = deepcopy(a)
j = 0
cnt = 0
for i in range(n-1):
    minj = i
    for j in range(i, n):
        if A[j] < A[minj]: # 未ソート部からargminを持ってくる
            minj = j
    A[i], A[minj] = A[minj], A[i] # 未ソート先頭と最小値を交換する
    if i != minj: # ソートした場合はカウント+1する
        cnt += 1

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-1):
    min = i+1
    for j in range(i+1,n):
        if a[min] > a[j]:
            min = j
    if a[i] > a[min]:
        a[i],a[min] = a[min],a[i]
        cnt += 1
for i in range(n):
    if i != n-1:
        print(a[i], end=' ')
    else:
        print(a[i])
print(cnt)

Submission status: Accepted

Solution code:
def selection(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 i != minj:
            A[i],A[minj]=A[minj],A[i]
            count += 1
    return count 

n = int(input())
a = list(map(int,input().split()))
count=selection(a,n)
print(' '.join(map(str,a)))
print(count)

Submission status: Accepted

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

for i in range(n - 1):
    minv = A[i]
    minv_idx = i
    for k in range(i + 1, n):
        if minv > A[k]:
            minv = A[k]
            minv_idx = k
    if i != minv_idx:
        A[i], A[minv_idx] = minv, A[i]
        c += 1

print(*A)
print(c)

Submission status: Accepted

Solution code:
def selectionsort(a, n):
    n_exchange = 0
    for i in range(0, n):
        min_j = i
        for j in range(i, n):
            if a[j] < a[min_j]:
                min_j = j

        if a[i] != a[min_j]:
            n_exchange += 1

        a[i], a[min_j] = a[min_j], a[i]

    return a, n_exchange


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

sorted_a, n_exchange = selectionsort(a, n)

print(' '.join([str(x) for x in sorted_a]))
print(n_exchange)

Submission status: Accepted

Solution code:
def SelectionSort(l):
  countSwap = 0
  for i in range(len(l)):
    minj = i
    for j in range(i, len(l)):
      if l[j] < l[minj]:
        minj = j
    if minj == i:
      continue
    else:
      temp = l[i]
      l[i] = l[minj]
      l[minj] = temp
      countSwap += 1
  print(" ".join(map(str, l)))
  print(countSwap)

N = input()
l = list(map(int, input().split()))
SelectionSort(l)

Submission status: Accepted

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

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


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


ans = 0

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

p(A)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding:utf8 -*-

n = int(input())
l = list(map(int, input().split()))
c = 0
for i in range(n):
    minj = i + l[i:].index(min(l[i:]))
    if minj != i:
        l[i], l[minj] = l[minj], l[i]
        c += 1
print(' '.join(map(str, l)))
print(c)

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


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

cnt = selectionSort(A, N)
print(' '.join(map(str, A)))
print(cnt)

Submission status: Accepted

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

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

print(' '.join(map(str,lst)))
print(swap)

Submission status: Accepted

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

c = 0
for i in range(n):
    minj = i
    for j in range(i+1, n):
        if A[j] < A[minj]:
            minj = j
    t = A[i]
    A[i] = A[minj]
    A[minj] = t
    if i != minj:
        c += 1
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):
    m = i
    for j in range(i+1, n):
        if a[j] < a[m]:
            m = j
    a[i], a[m] = a[m], a[i]
    if i != m:
        count += 1

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):
    min_index = i
    for j in range(i, n):
        if l[min_index] > l[j]:
            min_index = j
    if min_index != i:
        l[i], l[min_index] = l[min_index], l[i]
        count += 1

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

Submission status: Accepted

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

def selectionSort(A,N):
    num=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:
            tmp=A[i]
            A[i]=A[minj]
            A[minj]=tmp
            num += 1
    return A,num

A,N=selectionSort(A,N)
print(*A)
print(N)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x 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 not i == minj:
        A[i], A[minj] = A[minj], A[i]
        count += 1
        
print(" ".join(map(str, A)))
print(count)

Submission status: Accepted

Solution code:
def selection_sort(lst):
    cnt = 0
    for i in range(len(lst)-1):
        minj = i
        for j in range(i+1, len(lst)):
            if lst[j] < lst[minj]:
                minj = j
        if lst[i] > lst[minj]:
            lst[i], lst[minj] = lst[minj], lst[i]
            cnt += 1
    return cnt

n = int(input())
lst = list(map(int, input().split()))
cnt = selection_sort(lst)
print(*lst)
print(cnt)

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
        if minj > i:
            A[i],A[minj] = A[minj],A[i]
            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())
*L, = map(int, input().split())
#L = [int(i) for i in input().split()]

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

cnt = SelectionSort(L,n)

print(*L)
print(cnt)

Submission status: Accepted

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

perm = 0
for i in range(N):
    minj = i
    for j in range(i, N):
        if A[j] < A[minj]:
            minj = j
    if minj != i:
        perm += 1
    temp = A[i]
    temp2 = A[minj]
    A[i] = temp2
    A[minj] = temp
print(" ".join(list(map(str, A))))
print(perm)

Submission status: Accepted

Solution code:
def selectionSort(matrix, N):
  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
  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:
def select_sort(A, N):
    cnt = 0
    for i in range(N):
        mini_ix = i
        for j in range(i, N):
            if A[mini_ix] > A[j]:
                mini_ix = j

        if mini_ix != i:  # ここでswap
            A[i], A[mini_ix] = A[mini_ix], A[i]
            cnt += 1

    print(*A, sep=' ')
    print(cnt)


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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ALDS1_2_B: Selection Sort 
#N = 6
#A = [int(x) for x in "5 6 4 2 1 3".split()]

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


def selectionSort(A, N):
    e = 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]
            e += 1
            #print(A)
    print(" ".join(map(str, A)))
    print(e)
    
selectionSort(A, N)

Submission status: Accepted

Solution code:
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
    if i!=minj:
        tmp=A[i]
        A[i]=A[minj]
        A[minj]=tmp
        counter+=1
for k in range(len(A)):
    if k != len(A)-1:
        print(A[k],end=' ')
    else:
        print(A[k])
print(counter)

Submission status: Accepted

Solution code:
N = int(input())

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

cnt = 0

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

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 i != minj:
           cnt += 1
           tmp = A[i]
           A[i] = A[minj]
           A[minj] = tmp
    print(*A)
    print(cnt)
    
n = int(input())
a = list(map(int, input().split()))

selectionSort(a, n)

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
    v=A[minj]
    A[minj]=A[i]
    A[i]=v
    if(minj!=i):
        count+=1
s=" ".join(map(str,A))
print(s)
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):
    min=i
    for j in range(i,N):
        if A[j] < A[min]:
            min=j
    if i<min:
        count+=1
        A[i],A[min]=A[min],A[i]
for k in range(N):print(A[k],end=' \n'[k+1==N])
print(count)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def select(a,n):
	ct=0
	for i in range(n-1):
		minv=i
		for j in range(i+1,n):
			if a[j]<a[minv]:
				minv=j
		t=a[i]
		a[i]=a[minv]
		a[minv]=t
		if a[i]!=a[minv]:
			ct+=1
	return a,ct

n=int(input())
table=list(map(int,input().split()))
b,c=select(table,n)
print(*b)
print(c)