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 main():
    n = int(input())
    a = list(map(int,input().split()))
    res = 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:res+=1
        a[i],a[minj] = a[minj],a[i]
    print (' '.join(map(str,a)))
    print (res)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

cnt = 0
for i in range(N):
    min_idx = i
    # i番目以降での最小を探す
    for j in range(i, N):
        if A[j] < A[min_idx]:
            min_idx = j

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

print(*A)
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
table = list(map(int,input().split()))
count = 0
for i in range(N):
    minj = i
    for j in range(i,N):
        if table[j] <table[minj]:
            minj = j#最小の数がある位置
    table[i],table[minj] = table[minj],table[i]
    if i != minj:
        count += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def selectionSort(A, N):
    c = 0
    for i in range(N):
        minj = i
        for j in range(i,N):
            if A[j] < A[minj]:
                minj = j
        if A[i] != A[minj]:
            A[i],A[minj] = A[minj],A[i]
            c += 1
    return A,c
num = int(input())
li1 = [int(e) for e in input().split()]
li2 = selectionSort(li1,num)
print(*li2[0],sep=" ")
print(li2[1])

Submission status: Accepted

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

select_sort(int(input()), [int(i) for i in input().split()])

Submission status: Accepted

Solution code:
n = int(input())
numL = [0 for i in range(n)]

x = input()
for i in range(n):
    numL[i] = int(x.split()[i])

ctr = 0
for i in range(n-1):
    min = i
    for j in range(i+1,n):
        if numL[j] < numL[min]:
            min = j
    if min == i:
        pass
    else:
        ctr += 1
        tmp = numL[i]
        numL[i] = numL[min]
        numL[min] = tmp

print(*numL)
print(ctr)

Submission status: Accepted

Solution code:
def selectionSort(A,N):

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


N = int(raw_input())
A = map(int, raw_input().split())

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

Submission status: Accepted

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

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
        v = A[i]
        A[i] = A[minj]
        A[minj] = v

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

Submission status: Accepted

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


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

Submission status: Accepted

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

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

def selection_sort(A, N):
    num = 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]
            num += 1

    show(A)
    print(num)



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

Submission status: Accepted

Solution code:
from typing import MutableSequence


def selection_sort(a_list: MutableSequence, n: int) -> int:
    """単純選択ソート"""
    sw_cnt = 0
    for i in range(n - 1):
        min = i
        for j in range(i + 1, n):
            if a_list[j] < a_list[min]:
                min = j

        if i != min:
            sw_cnt += 1

        a_list[i], a_list[min] = a_list[min], a_list[i]

    print_list = [str(i) for i in a_list]
    print(' '.join(print_list))
    print(sw_cnt)


if __name__ == '__main__':
    n = int(input())
    input_list = [int(x) for x in input().split()]

    selection_sort(input_list, n)

Submission status: Accepted

Solution code:
def swap(A, i, j):
    temp = A[i]
    A[i] = A[j]
    A[j] = temp

def selection(A):
    N = len(A)
    count = 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 (min_j != i):
            swap(A, i, min_j)
            count += 1
    print(" ".join(map(str, A)))
    print(count)

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

Submission status: Accepted

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


def selectionSort(a, n):
    ans = 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:
            ans += 1

    print(*a)
    print(ans)


selectionSort(l, n)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

def selection(a:list, n:int) -> None:
    if not isinstance(a, list):
        return None

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

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

selection(a, n)

Submission status: Accepted

Solution code:
import sys
import math
from typing import Tuple, List
input = sys.stdin.readline

def  print_vec (A: list):
    print (" ".join(map (str, A)))

def swap (A: List, i: int, j: int) -> None:
    temp = A [i]
    A [i] = A [j]
    A [j] = temp
 

def selection_sort(A: List) -> Tuple[List, int]:
    swap_cout = 0
    for i in range(len(A)):
        mini = i
        for j in range(i, len(A)):
            if A[j] < A[mini]:
                mini = j
        if (mini != i):
            swap(A, i, mini)
            swap_cout+=1
    return (A, swap_cout)


if __name__ == "__main__":
    n = int(input())
    vec = list (map (int, input ().split ()))
    result =  selection_sort(vec)
    print_vec (result [0])
    print(result [1])

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(len(seq)):
    min = i
    for j in range(i, len(seq)):
        if seq[j] < seq[min]:
            min = j
    if seq[i] != seq[min]:
        seq[i], seq[min] = seq[min], seq[i]
        count += 1
    if seq == sorted(seq):
        print(*seq)
        print(count)
        break

Submission status: Accepted

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

for i in range(N):
    min = i
    for j in range(i, N):
        if data[j] < data[min]:
            min = j
    if min != i:
        data[i], data[min] = data[min], data[i]
        count += 1
            
print("%d" %data[0], end="")
for i in range(1, N):
    print(" %d" %data[i], end="")
print()
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+1,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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split(" ")))
f = 0
for j in range(n-1):
    m = l[j:].index(min(l[j:]))
    if m != 0:
        l[j], l[m + j] = l[m + j], l[j]
        f += 1
print(*l)
print(f)

Submission status: Accepted

Solution code:
def selectionsort(a, n):
    count = 0
    for i in range(n):
        min = i
        for j in range(i, n):
            if a[min] > a[j]:
                min = j
        if i != min:
            count += 1
            a[i], a[min] = a[min], a[i]
    return count

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

c = selectionsort(A, N)
print(*A)
print(c)

Submission status: Accepted

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

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

selectionSort(A, N)

Submission status: Accepted

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

count = 0
for i in range(n):
    minv = a[i]
    mini = i
    for j in range(i+1, n, 1):
        if minv > a[j]:
            minv = a[j]
            mini = j
    if i != mini:
        a[i], a[mini] = a[mini], a[i]
        count += 1
print(" ".join([str(i) for i in a]))
print(count)

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())

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

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

Submission status: Accepted

Solution code:
def SelectionSort(A, N):
    count = 0
    for i in range(N):
        flag = 0
        minj = i
        j = i - 1
        for j in range(i,N):
            if A[j] < A[minj]:
                flag = 1
                minj = j
            
        if flag == 1:
            a = A[minj]
            A[minj] = A[i]
            A[i] = a
            count += 1
 
    return(A, count)

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

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

Submission status: Accepted

Solution code:
import sys

def selection_sort(size, numbers):
    swap_count = 0

    for i in range(size):
        minindex = i
        for j in range(i + 1, size):
            if numbers[j] < numbers[minindex]:
                minindex = j

        if i != minindex:
            swap_count += 1
            numbers[i], numbers[minindex] = numbers[minindex], numbers[i]

    return swap_count

def main(stdin=None, stdout=None):
    if stdin is None:
        stdin = sys.stdin
    if stdout is None:
        stdout = sys.stdout

    size = int(stdin.readline().strip())
    numbers = [int(x) for x in stdin.readline().strip().split(" ")]

    swap_count = selection_sort(size, numbers)
    stdout.write(" ".join([str(x) for x in numbers]) + "\n")
    stdout.write(str(swap_count) + "\n")

main()

Submission status: Accepted

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

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

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):
        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)


main()

Submission status: Accepted

Solution code:
a = int(input())
b = list(map(int,input().split()))
A = []


def selectionSort(b,a):
    count = 0
    for i in range(len(b)-1):
        mini = i
        for j in range(i+1,len(b)):
            if b[mini]>b[j]:
                mini = j
        if b[mini]!=b[i]:
            b[i],b[mini]=b[mini],b[i]
            count += 1
    return b,count

A,B = selectionSort(b,a)
print(*A)
print(B)

Submission status: Accepted

Solution code:
# min(A): リストAの最小値を返す
# A.index(min(A)): リストAの最小値のindexを返す
# リストのスライス A[start:stop]でstart <= i < stopの値を返す．stopの値は含まれない
def SelectionSort(N, A):
   count = 0 # 交換回数カウント変数
   for i in range(0, N-1):
      minj = i
      for j in range(i+1, N): # 未ソート部分から最後まで
         if A[j] < A[minj]:
            minj = j # 最小値のindexを変更
      # print('Before: ', *A)
      if A[minj] < A[i]:
         A[i], A[minj] = A[minj], A[i]
         count+=1
      # print(' After: ', *A)
   return A, count

N = int(input())
A = list(map(int, input().split())) # 「1 2 3…」のような配列入力
ans, count = SelectionSort(N, A)
print(*ans)
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
for i in range(len(arr)-1):
  
  minj = i+1
  for j in range(i, len(arr)):
    if arr[j] < arr[minj]:
      minj = j
  
  tmpCur = arr[i]
  tmpMin = arr[minj]
  if tmpCur > tmpMin:
    cnt += 1
    arr[i], arr[minj] = tmpMin, tmpCur

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

Submission status: Accepted

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

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

print(" ".join([str(s) for s in A]))
print(cnt)

Submission status: Accepted

Solution code:
#初期data
input_number = int(input())
input_list = list(map(int, input().split()))
exchange_count = 0

#アルゴリズム
for i in range(input_number):
    min_data = input_list[i]
    min_number = -1
    for j in range(i+1, input_number):
        if min_data > input_list[j]:
            min_data = input_list[j]
            min_number = j
    if min_number != -1:
        input_list[i], input_list[min_number] = input_list[min_number], input_list[i]
        exchange_count += 1

#処理の実行
print(" ".join(map(str, input_list)))
print(exchange_count)

Submission status: Accepted

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

    ans = 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:
            ans += 1
    
    print(' '.join(map(str, A)))
    print(ans)
        

selection_sort()

Submission status: Accepted

Solution code:
def SelectionSort(seq):
    swp_cnt = 0
    
    for i in range(0, len(seq)):
        idx = i

        for j in range(i, len(seq)):
            if seq[idx] > seq[j]:
                idx = j

        if idx != i:
            seq[i], seq[idx] = seq[idx], seq[i]        
            swp_cnt += 1

    print(*seq)
    print(swp_cnt)

def main():
    n = input()
    seq = [int(a) for a in input().split()]
    SelectionSort(seq)

main()

Submission status: Accepted

Solution code:
# Algorithms and Data Structures 1
# Selection Sort
# Name: Ryuya Asada
# ID: s1260064


def SelectionSort(A: list):
    number_of_actual_swaps = 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:
            number_of_actual_swaps += 1

    print(*A, sep=" ")
    print(number_of_actual_swaps)


def main():
    length = int(input())
    A = [int(e) for e in input().split()]
    SelectionSort(A)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def selection(n, data):
    __count = 0
    for i in range(n):
        min_i = i
        for j in range(i+1, n):
            if data[min_i] > data[j]:
                min_i = j
        tmp = data[i]
        data[i] = data[min_i]
        data[min_i] = tmp
        if min_i != i:
            __count += 1

    return data, __count

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

if "__main__" == __name__:
    n = int(input())
    data = list(map(int, input().rsplit()))
    data, count = selection(n, data)
    show(data, count)

Submission status: Accepted

Solution code:
def show(A):
    N = len(A)
    for i in range(N):
        if i :
            print(' ', end = '')
        print(A[i], end = '')
    print()

def selectionSort(A, N):
    exchange = 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 min_j != i:
            A[i], A[min_j] = A[min_j], A[i]
            exchange += 1
    show(A)
    print(exchange)

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

selectionSort(A, N)

Submission status: Accepted

Solution code:
def printData(data) :
    for i, value in enumerate(data) :
        if i > 0 :
            print(" ", end = "")
        print(value, end = "")
    print()
    
def selectionSort(data, N) :
    count = 0
    for i in range(0, N) :
        sortIdx = i
        for j in range(i + 1, N) :
            if data[sortIdx] > data[j] :
                sortIdx = j
        if sortIdx != i :
            buf = data[sortIdx]
            data[sortIdx] = data[i]
            data[i] = buf
            count += 1
            
    printData(data)
    print(count)
    
N = int(input())
data = [int(char) for char in input().split()]
selectionSort(data, N)

Submission status: Accepted

Solution code:
def SelectionSort(A, N):

    changenum = 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:
            changenum += 1
            A[minj], A[i] = A[i], A[minj]
        
    
    print(' '.join([str(x) for x in A]))
    print(changenum)

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

SelectionSort(A, N)

Submission status: Accepted

Solution code:
#selection sort

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


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

Submission status: Accepted

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


if __name__ == "__main__":
    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 selection(A,N):
    global cnt
    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]
            cnt += 1
    return A

n = int(input())
a = [int(s) for s in input().split()]
cnt = 0
print(" ".join(map(str,selection(a,n))))
print(cnt)

Submission status: Accepted

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

def selection_sort(A, N):
    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
        if min_j != i:
            count += 1
        A[i], A[min_j] = A[min_j], A[i]
    return A, count

A, count = selection_sort(A, N)
print(' '.join(map(str, A)))
print(count)

Submission status: Accepted

Solution code:
#2_B
def selection_sort(a,n):
    count = 0
    for i in range(n):
        minj = i
        m = n-1-i+1
        for j in range(m):
            k = i + j
            if a[k] < a[minj]:
                minj = k
        
        if minj!=i:
            minnum = a[minj]
            a[minj] = a[i]
            a[i] = minnum
            count += 1
    return a, count

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

arr, count = selection_sort(a, n)
res = str(arr[0])
for i in range(n-1):
    res += " " + str(arr[i+1])

print(res)
print(count)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    a_lists = list(map(int, input().split()))
    cnt = 0
    for i in range(N):
        min_j = i
        for j in range(i, N):
            if a_lists[j] < a_lists[min_j]:
                min_j = j
        if min_j == i:
            continue
        a_lists[i], a_lists[min_j] = a_lists[min_j], a_lists[i]
        cnt += 1
    print(' '.join(map(str, a_lists)))
    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def selection_rort(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
        
    return a, count

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

a, count = selection_rort(a, n)
print(" ".join(map(str,a)))
print(count)

Submission status: Accepted

Solution code:
def selection_sort(A, N):
    c = 0
    for i in range(N-1):
        minj = A[i+1:].index(min(A[i+1:])) + i+1
        if A[i] > A[minj]:
            A[i], A[minj] = A[minj], A[i]
            c += 1
    return A, c

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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


def ss(array, length):
  cnt=0
  for i in range(length):
    minj = i
    v = array[i]
    for j in range(i, n):
      if array[minj]>array[j]:
        minj = j
    if array[i] != array[minj]:
      cnt+=1
    array[i] = array[minj]
    array[minj] = v
  return array, cnt

b, c = ss(a, n)

for i in range(n):
  if i==n-1:
    print(b[i], end='')
  else:
    print(b[i], end=' ')

print()
print(c)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def selection_sort(N, A):
    num = 0
    for i in range(N):
        minimum = A[i]
        flag = False
        for j in range(i+1, N):
            if A[j] < minimum:
                minimum = A[j]
                index = j
                flag = True
        if flag:
            A[i], A[index] = A[index], A[i]
            num += 1
    return [num, *A]

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    ans = selection_sort(N, A)
    print(*ans[1:])
    print(ans[0])

Submission status: Accepted

Solution code:
def selectionSort(a,n):
    r = 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]:
            r += 1
    return r


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

r = selectionSort(a,n)

print(*a)
print(r)

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):
        mini=i
        for j in range(i,N):
            if A[j]<A[mini]:
                mini=j
        if i!=mini:
            tmp = A[i]
            A[i]=A[mini]
            A[mini] = tmp
            count += 1


    print(' '.join(list(map(str, A))))
    print(count)

selectionSort(A,N)

Submission status: Accepted

Solution code:
def selection_sort(array, n):
    count = 0
    for i in range(n):
        index_min = i
        for j in range(i, n):
            if array[j] < array[index_min]:
                index_min = j
        if array[index_min] < array[i]:
            array[i], array[index_min] = array[index_min], array[i]
            count += 1
    return count


def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    count = selection_sort(a, n)
    print(*a)
    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
arr = [x for x in list(map(int, input().split()))]

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

print (" ".join([str(x) for x in arr]))
print (count)

Submission status: Accepted

Solution code:
import re

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

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

print(re.sub("[\[\]\,]","",str(a)))
print(num)

Submission status: Accepted

Solution code:
def selection_sort(A):
    a = list(A)
    swap_count = 0

    for i in range(0, len(A)-1):
        i_min = i
        for j in range(i+1, len(A)):
            if a[j] < a[i_min]:
                i_min = j
        if i is not i_min:
            a[i], a[i_min] = a[i_min], a[i]
            swap_count += 1
    
    print(' '.join(map(str, a)))
    print(swap_count)

input()
selection_sort(list(map(int, input().split())))