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 s_sort(A,N):
   global count

   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:
           temp = A[i]
           A[i] = A[min_j]
           A[min_j] = temp
           count += 1

   return A
  
if __name__=='__main__':
    n = int(input())
    R = list(map(int,input().split()))
    count = 0
    R = s_sort(R,n)
    print(" ".join(map(str,R)))
    print(count)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in 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 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(0, n):
    minj = i
    for j in range(i+1, n):
        if a[j] < a[minj]:
            minj = j
    if i != minj:
        x = a[i]
        a[i] = a[minj]
        a[minj] = x
        count = count + 1

print(*a)
print(count)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=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 a[i]!=tmp:
        b+=1
print(" ".join(map(str,a)))
print(b)

Submission status: Accepted

Solution code:
def print_list(a):
    out = ''
    for ai in a:
        out += str(ai) + ' '
    else:
        out = out[:-1]
    print(out)

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

    print_list(a)
    print(str(cnt))
    return a

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

a = selection_sort(a, n)

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

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

ans, cnt = SelectionSort(A, N)

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

Submission status: Accepted

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

print(*working_list)
print(exchange_count)

Submission status: Accepted

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

    print(*A)
    print(cnt)

n = int(input())
ls = list(map(int,input().split()))
selectionSort(ls,n)

Submission status: Accepted

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

def SelectionSort(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 A[i] > A[mini]:
            A[i], A[mini] = A[mini], A[i]
            count += 1
    print(*A)
    print(count)

main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = 0
minj = 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      
    c = a[i]
    a[i] = a[minj]
    a[minj] = c
    

b = [str(i) for i in a]
print(" ".join(b))
print(count)

Submission status: Accepted

Solution code:
input()
A = [int(i) for i in input().split()]
n = 0
for i in range(0, len(A)):
    minj = i
    for j in range(i, len(A)):
        if A[j] < A[minj]:
            minj = j
    A[i], A[minj] = A[minj], A[i]
    if i != minj:
        n += 1

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

Submission status: Accepted

Solution code:
#選択ソート
#最小値を毎回計算して一番左にぶち込む直感的なソート

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

count = 0
for i in range(length):
    minpos = i
    for j in range(i+1, length):
        if list[minpos] > list[j]:
            minpos = j

    if i != minpos:
        count += 1
    list[i], list[minpos] = list[minpos], list[i]

result = ''
for l in list:
    result += str(l) + ' '

print(result[:-1])
print(count)

Submission status: Accepted

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

print(*A)
print(count)

Submission status: Accepted

Solution code:
def selection_sort(A, N):
    swap_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:
            tmp = A[i]
            A[i] = A[minj]
            A[minj] = tmp
            swap_count += 1
    return A, swap_count
N = int(input())
A = list(map(int, input().split(' ')))
sorted_A, swap_count = selection_sort(A, N)
print(" ".join(map(str, sorted_A)))
print(swap_count)

Submission status: Accepted

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

def selectsort(A,n):
    count=0
    for i in range(len(A)):
        minj=i
        flag=0
        for j in range(i,len(A)):
            if A[j]<A[minj]:
                minj=j
                flag=1
        if flag==1:
            tmp=A[minj]
            A[minj]=A[i]
            A[i]=tmp
            count+=1
    return(A,count)

result,count=selectsort(A,n)
print(" ".join([str(i) for i in result]))
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def show (a,n):
  for i in range(n):
    if i > 0:
      print("",end=" ")
    print(a[i],end="")
  print("")

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

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

selectionSort(a,n)

Submission status: Accepted

Solution code:
def SelectionSort(N, A):
    count = 0
    if N == 1:
        print(str(A[0]) + "\n" + str(0))
    else:
        for i in range(N - 1):
            A_r = A[i:] #確定してないAの右側の領域
            mini = min(A_r)
            mini_index = i + A_r.index(mini) #+iの補正が必要
            if A[i] > mini:
                count += 1
                A[i], A[mini_index] = A[mini_index], A[i] 

        str_A = map(str, A)
        print(" ".join(str_A) + "\n" + str(count))


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

Submission status: Accepted

Solution code:

def ALDS1_2_B():
    """
    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] を交換

    ＜問題＞
    数列Aを読み込み、選択ソートのアルゴリズムで昇順に並び替え出力するプログラムを作成してください。
    上の疑似コードに従いアルゴリズムを実装してください。
    疑似コード 7 行目で、i と minj が異なり実際に交換が行われた回数も出力してください。

    ＜入力＞
    入力の最初の行に、数列の長さを表す整数 N が与えられます。２行目に、N 個の整数が空白区切りで与えられます。

    ＜出力＞
    出力は 2 行からなります。１行目に整列された数列を 1 行に出力してください。
    数列の連続する要素は１つの空白で区切って出力してください。2 行目に交換回数を出力してください。

    ＜制約＞
    1 ≤ N ≤ 100
    0 ≤ A の要素 ≤ 100
    """
    N = int(input())
    A = [int(e) for e in input().split(" ")]
    switch_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]
            switch_cnt += 1
    print(" ".join(map(str, A)))
    print(switch_cnt)


def main():
    ALDS1_2_B()


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):
	mini=i
	for j in range(i,n):
		if a[j]<a[mini]:
			mini=j
	if a[i]>a[mini]:
		ans+=1
	a[i],a[mini]=a[mini],a[i]
print(*a)
print(ans)

Submission status: Accepted

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


def main():
    sc = Scan()
    N = sc.intarr()[0]
    A = sc.intarr()

    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:
            tmp = A[i]
            A[i] = A[minj]
            A[minj] = tmp
            count += 1
    display(A)
    print(count)


class Scan():

    def intarr(self):
        line = input()
        array = line.split(' ')
        num_array = [int(n) for n in array]
        return num_array

    def intarr_ver(self, n):
        return [int(input()) for _ in range(n)]

    def strarr(self):
        line = input()
        array = line.split(' ')
        array[-1] = array[-1].strip('\n')
        return array


def display(array):
    for a in range(len(array)):
        if len(array) - a != 1:
            print(array[a], end=' ')
        else:
            print(array[a])


main()

Submission status: Accepted

Solution code:
def selection_sort(a, n):

    swap_count = 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
        a[i], a[min_j] = a[min_j], a[i]
        if i != min_j:
            swap_count += 1

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


def main():
    n = int(input())
    a = list(map(int, input().split(' ')))
    selection_sort(a, n)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())
ans = 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]
        ans += 1
print(' '.join(map(str, A)))
print(ans)

Submission status: Accepted

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

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

                
    showResult(A, cnt)

def showResult(A, cnt):
    for i in range(N-1):
        print(A[i], end=" ")
    print(A[N - 1])
    print(cnt)
        
#A = [6, 5, 4, 3,2,1]
#N = len(A)
N = int(input())
A = list(map(int, input().split())) 
selectionSort(A,N)

Submission status: Accepted

Solution code:
cou = 0

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

for i in range(N):
  minj = i
  for j in range(i,N):
    if l[j] < l[minj]:
      minj = j
  if not l[i] == l[minj]:
    a = l[i]
    b = l[minj]
    l[i] = b
    l[minj] = a
    cou += 1

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

Submission status: Accepted

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

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()))
cnt = 0

for i in range(n):
    min_j = i
    for j in range(i+1,n):
        if A[j] < A[min_j]:
            min_j = j
    if min_j != i:
        buf = A[i]
        A[i] = A[min_j]
        A[min_j] = buf
        cnt += 1
for k in range(n):
    if k != n-1:
        print(A[k],"",end="")
    else:
        print(A[k])
print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

def selection_sort(a):
    size = len(a)
    cnt = 0
    for i in range(size):
        mini = i
        for j in range(i,size):
            if a[j] < a[mini]:
                mini = j
        if mini != i:
            a[i], a[mini] = a[mini], a[i]
            cnt += 1
    return (a, cnt)


_ = input()
l = [ int(s) for s in input().split() ]
(nl,swap) =  selection_sort(l)

print(*nl)
print(swap)

Submission status: Accepted

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

count=0

for i in range(0,n,1):
  minj=i
  for j in range(i+1,n,1):
    if A[minj] > A[j]:
      minj=j
  if i != minj:
    a= A[i]
    A[i]=A[minj]
    A[minj]=a
    count+=1
  
print(*A, sep=' ')
print(count)

Submission status: Accepted

Solution code:
n = int(input())
array = list(map(int, input().split()))
swaped = 0

for i in range(n):
    mini = array[i]
    mini_idx = i
    for j in range(i, n):
        if mini > array[j]:
            mini = array[j]
            mini_idx = j
    if i != mini_idx:
        array[i], array[mini_idx] = array[mini_idx], array[i]
        swaped += 1
print(' '.join([str(e) for e in array]))
print(swaped)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    n = int(input())
    aa = list(map(int, input().split()))
    ans=0
    for i in range(n):
        min_j=i
        for j in range(i+1,n):
            if aa[j]<aa[min_j]:min_j=j
        if min_j!=i:
            aa[i],aa[min_j]=aa[min_j],aa[i]
            ans+=1
    print(*aa)
    print(ans)

main()

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

print(*A)
print(cnt)

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):
        A_ = A[i:]
        j = A_.index(min(A_)) + i
        if A[j] < A[i]:
            temp = A[i]
            A[i] = A[j]
            A[j] = temp
            cnt += 1
                
    print(*A)
    print(cnt)
    
selectionSort(A, N)

Submission status: Accepted

Solution code:
def selection(l):
    count = 0
    for i in range(len(l)):
        minj = i
        for j in range(i,len(l)):
            if l[j] < l[minj]:
                minj = j
        if l[i] > l[minj]:
            a = l[i]
            l[i] = l[minj]
            l[minj] = a
            count += 1
    ans_l = map(str,l)
    return ans_l,count
n = int(input())
l = list(map(int,input().split()))
ans_l,count = selection(l)
print(' '.join(ans_l))
print(count)

Submission status: Accepted

Solution code:
n = int(input())
ary = list(map(int, input().split()))
cnt = 0

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

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

Submission status: Accepted

Solution code:
def selection_sort(arr, n):
    count = 0
    for i in range(n):  # 0からn−1まで
        min_idx = i
        for j in range(i, n):
            if arr[j] < arr[min_idx]:
                min_idx = j
        if i != min_idx:
            arr[i], arr[min_idx] = arr[min_idx], arr[i]
            count += 1
    return arr, count


def main():
    n = int(input())
    arr = list(map(int, input().split()))
    # n = 6
    # arr = [5, 6, 4, 2, 1, 3]
    arr, count = selection_sort(arr, n)
    print(" ".join(map(str, arr)))
    print(count)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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
print(*A)
print(cnt)

Submission status: Accepted

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

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

selection_sort(A)

Submission status: Accepted

Solution code:
N=int(input()) 
A=list(map(int,input().split()))
n=0
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[i]
        A[i]=A[minj]
        A[minj]=x
        n+=1
print(*A)
print(n)

Submission status: Accepted

Solution code:
def selectionSort(numlist):
    n = len(numlist)
    swaptime = 0
    for i in range(n):
        min_index = i
        for j in range(i + 1, n):
            if numlist[j] < numlist[min_index]:
                min_index = j
        if min_index != i:
            numlist[i], numlist[min_index] = numlist[min_index], numlist[i]
            swaptime += 1
    return swaptime

def print_numlist(numlist):
    if len(numlist) == 1:
        print(numlist[0])
    else:
        for i in range(0, len(numlist) - 1):
            print(numlist[i], end = ' ')
        print(numlist[-1])

if __name__ == '__main__':
    n = int(input())
    numlist = [int(i) for i in input().replace('\n', '').split(' ')]
    swapnum =  selectionSort(numlist)

    print_numlist(numlist)
    print(swapnum)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int, input().split()))
cnt=0
for i in range(n):
    if n==1:
        break
    elif l[i]!=min(l[i:n]):
        num=l[i:n].index(min(l[i:n]))+i
        l[i],l[num]=l[num],l[i]
        cnt+=1
print(*l)
print(cnt)

Submission status: Accepted

Solution code:
# selectionSort(A, N) // N個の要素を含む0-オリジンの配列A
#   for i が 0 から N-1 まで
#     minj = i
#    for j が i から N-1 まで
#       if A[j] < A[minj]
#         minj = j
#     A[i] と A[minj] を交換

n = int(input())
nums = list(map(int, input().split()))
count = 0
for i in range(0, n-1):
    minj = i
    for j in range(i, n):
        if nums[j] < nums[minj]:
            minj = j
            
    nums[i], nums[minj] = nums[minj], nums[i]
    if i != minj:
        count += 1
    
        


print(*nums)
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
change = 0
for i in range(n-1):
	minA = 1000
	doko = 0
	for u in range(i+1, n):
		if minA > a[u]:
			minA = a[u]
			doko = u
	if minA < a[i]:
		a[i], a[doko] = a[doko], a[i]
		change += 1
print(*a)
print(change)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
counter = 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:
    counter += 1
print(*A, sep=" ")
print(counter)

Submission status: Accepted

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

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

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

# 出力
B=list(map(str,A))
print(' '.join(B))
print(count)

Submission status: Accepted

Solution code:
n = int(input())
num_list = list(map(int, input().split()))
# n = 6
# num_list = [5,6,4,2,1,3]

num_swap = 0
for i in range(n):
    index = -1
    mini = num_list[i]
    for j in range(i, n):
        if mini > num_list[j]:
            index = j            
            mini = num_list[j]
    if index > -1:
        num_swap += 1
        num_list[i], num_list[index] = num_list[index], num_list[i]
print(' '.join(map(str, num_list)))
print(num_swap)

Submission status: Accepted

Solution code:
def selection_sort(a, n):
    number = 0
    for i in range(n):
        min_index = a[i:].index(min(a[i:])) + i
        a[min_index], a[i] = a[i], a[min_index]
        if i != min_index:
            number += 1
    return number


n = int(input())
*ls, = map(int, input().split())
num = selection_sort(ls, n)
print(*ls)
print(num)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = 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
    if minj != i:
      count += 1
    a[i],a[minj] = a[minj],a[i]
  return count
  
count = selectionSort(a,n)
print(*a)
print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in 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]:
        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()))
cnt=0
judge=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
   
for i in range(n):
    if i==0:
        print(a[i],end="")
    else:
        print("{: }".format(a[i]),end="")
print()
print(cnt)

Submission status: Accepted

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

def selection_sort(a):
    global cnt
    for i in range(len(a)):
        min_idx = i
        for j in range(i + 1, len(a)):
            if a[j] < a[min_idx]:
                min_idx = j
                #cnt += 1
        if i != min_idx:
            a[i], a[min_idx] = a[min_idx], a[i]
            cnt += 1

selection_sort(a)
print(*a)
print(cnt)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def func(n, A):
    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 (" ".join(map(str, A)))
    print (count)

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

Submission status: Accepted

Solution code:
#選択ソート
def print_output(_list):
    for index in range(len(_list)):
        if index == len(_list) - 1:
            print(_list[index])
        else:
            print(_list[index],end=' ')

target_length = int(input())
target_list = list(map(int,input().split()))
choice_count = 0
for i in range(0, target_length - 1):
    minj = i
    for j in range(i, len(target_list)):
        if target_list[j] < target_list[minj]:
            minj = j
    if i != minj:
        choice_count += 1
    tmp = target_list[i]
    target_list[i] = target_list[minj]
    target_list[minj] = tmp
print_output(target_list)
print(choice_count)

Submission status: Accepted

Solution code:
cnt = 0
def SelectionSort(L):
    global cnt
    for i in range(len(L)):
        minj = i
        for j in range(i+1, len(L)):
            if L[minj] > L[j]:
                minj = j
        if minj != i:
            cnt += 1
            L[i], L[minj] = L[minj], L[i]

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

SelectionSort(NUM_LIST)
STR = list(map(str, NUM_LIST))
print(" ".join(STR))
print(cnt)

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)


if __name__ == "__main__":
	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()))
cnt = 0
for i in range (0, N-1):
    min = i
    for j in range(i+1, N):
        if A[j] < A[min]:
            min = j
    if min == i:
        continue
    else:
        tmp = A[min]
        A[min] = A[i]
        A[i] = tmp
        cnt += 1

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

    for b in range(N):
      print(A[b], end=" \n"[b + 1 == N])
    print(cnt)


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

SelectionSort(A, N)

Submission status: Accepted

Solution code:
def SelectionSort(A):
    count = 0
    for i in range(len(A)):
        mini = i
        for j in range(i, len(A)):
            if A[j] < A[mini]:
                mini = j
        if A[i] != A[mini]:
            A[i], A[mini] = A[mini], A[i]
            count += 1
    return A, count

N = int(input())
A = list(map(int, input().split()))
selection_sort, count = SelectionSort(A)
print(*selection_sort)
print(count)

Submission status: Accepted

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


def selectionSort(li: list, n: int):
    count = 0
    for i in range(n-1):
        minj = i
        for j in range(i+1, n):
            if li[minj] > li[j]:
                minj = j
        li[i], li[minj] = li[minj], li[i]
        if minj != i:
            count += 1
    print(*li)
    print(count)


selectionSort(A, N)

Submission status: Accepted

Solution code:
def selection_sort(nums, n):
    count = 0
    for i in range(n):
        minj = i
        flag = False
        for j in range(i, n-1):
            if nums[minj] > nums[j+1]:
                minj = j+1
                flag = True
        
        if flag:
            tmp = nums[i]
            nums[i] = nums[minj]
            nums[minj] = tmp
            count += 1


    nums_str = map(str, nums)
    print(" ".join(nums_str))
    print(count)

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

Submission status: Accepted

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

def SelectionSort(A):
    count = 0
    for i in range(N):
        min = i
        for j in range(i, N):
            if A[j] < A[min]:
                min = j
        if A[i] > A[min]:
            A[i], A[min] = A[min], A[i]
            count += 1
    print(' '.join(map(str, A)))
    print(count)

SelectionSort(A)