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] < A[mini]
5             mini = j
6     swap A[i] and A[mini] Note that, indices for array elements are based on 0-origin. Your program should also print the number of swap operations defined in line 6 of the pseudocode in the case where i ≠ mini. Input The first line of the input includes an integer 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

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
import sys
input = sys.stdin.readline


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


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

        
        

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

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

        

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


if __name__ == '__main__':
    main()

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

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

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

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

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

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

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

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

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

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
count = 0
for i in range(0, N):
    minj = i
    for j in range(i, N):
        if A[j] < A[minj]:
            minj = j
    A[i], A[minj] = A[minj], A[i]
    if A[i] != A[minj]:
        count += 1
print(' '.join([str(x) for x in A]))
print(count)

Solution (Python):

n=int(input())
a=[int(i) for i in input().split()]
k=0
for i in range(n):
    l=0
    min=i
    for j in range(n-i-1):
        if a[i+j+1]<a[min]:
            min=i+j+1
            l=1
    a[i],a[min]=a[min],a[i]
    if l==1:
        k+=1

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

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

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

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


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


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

Solution (Python):
N = int(input())

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

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

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

Solution (Python):


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

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

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

Solution (Python):
def selection_sort(a, n):
    cnt = 0
    for i in range(n):
        min_j = i
        for j in range(i, n):
            if a[j] < a[min_j]:
                min_j = j
        if i < min_j:
            a[i], a[min_j] = a[min_j], a[i]
            cnt += 1
    print(" ".join([str(x) for x in a]))
    print(cnt)


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


if __name__ == '__main__':
    main()

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

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

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

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

print(" ".join([str(v) for v in A]))
print(swap_count)

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

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

cnt = 0

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

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


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

Solution (Python):


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

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


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

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

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

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

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


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

count=selectionSort(l,n)

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

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

main(int(input()))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
count=0
for i in range(N-1):
    minj=i
    for j in range(i+1,N):
        if A[minj]>A[j]:
            minj=j
    if i<minj:
        A[minj],A[i]=A[i],A[minj]
        count+=1
for i in range(N):
    if i<N-1:
        print(A[i],end=' ')
    else:
        print(A[i])
print(count)

Solution (Python):


def selectionSort(A, N):
    c = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if A[j] < A[minj]:
                minj = j
        if i != minj:
            c += 1
            A[i], A[minj] = A[minj], A[i]
    print(' '.join(list(map(str, A))))
    print(c)


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

Solution (Python):

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

        if i != mini:
            sw += 1

    return sw



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



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

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

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

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

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

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

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

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

print(count)

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

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

Solution (Python):

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


if __name__ == "__main__":
    main()

Solution (Python):
if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    count = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if A[j] < A[minj]:
                minj = j
        if i != minj:
            A[i], A[minj] = A[minj], A[i]
            count += 1
    print(*A)
    print(count)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
cnt =0
for i in range(N):
	minj = i
	for j in range(i,N):
		if A[j]<A[minj]:
			minj = j
	if minj != i:
		cnt += 1
	v = A[i]
	A[i] = A[minj]
	A[minj] = v

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

Solution (Python):
num = int(input())

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

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

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

show()
print(count)

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

print(*a)
print(cnt)

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

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

print(*a)
print(co)

Solution (Python):
def test():

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

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

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


if __name__ == "__main__":
	test()

Solution (Python):



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

minj = 0
tmp = 0
cnt = 0

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

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

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

Solution (Python):
N = int(input())
*A, = map(int,input().split())
count = 0
for i in range(N):
    minj = i
    for j in range(i,N):
        if A[j] < A[minj]:
            minj = j
    if not minj == i:
        tmp = A[i]
        A[i] = A[minj]
        A[minj] = tmp
        count += 1
    
print(*A)
print(count)

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

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

A, cnt = SelectionSort(A)
print(" ".join([str(i) for i in A]))
print(cnt)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
count=0
for i in range(N):
    minj=i
    for j in range(i+1, N):
        if(A[j] <  A[minj]):
            minj=j

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

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

Solution (Python):

N = int(input())

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

num = 0

for i in range(N):

    minj = i

    for j in range(N-i):

        j += i

        if A[j] < A[minj]:

            minj = j

    if minj != i:
        num += 1

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

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

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

def SelectionSort(A):
    N = len(A)
    count = 0
    for i in range(N):
        minj = i
        for j in range(i,N):
            if A[j] < A[minj]:
                minj = j
        if i != minj:
            A[i], A[minj] = A[minj], A[i]
            count += 1
    for k in range(N-1):
        print(A[k], end=" ")
    print(A[N-1])
    print(count)

SelectionSort(A)

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

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

Solution (Python):



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


def linearsearch(subarray):
    
    min = subarray[0]
    min_index = 0;
    for i in range(1,len(subarray)):
        if min > subarray[i]:
            min = subarray[i]
            min_index = i
    return min_index


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

count,index,tmp = 0,0,0

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

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

printout(array,N)
print(count)

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

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

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

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


if __name__ == '__main__':
    main()

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

exchange_count = 0

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

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

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

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

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

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

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

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

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

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

    print(count)

if __name__ == "__main__":
    main()

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

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

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

Solution (Python):

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

Solution (Python):
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)

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

Solution (Python):
def selectionsort(a,n):
    count=0
    for i in range(n):
        minj=i
        for j in range(i,n):
            if a[j]<a[minj]:
                minj=j
        if i!=minj:
            m=a[i]
            a[i]=a[minj]
            a[minj]=m
            count+=1
    return a,count

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

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

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

Solution (Python):
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)

Solution (Python):
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)

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

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

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

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

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

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

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

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

Solution (Python):
n = int(input())

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

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

print(*A)
print(cnt)

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

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

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

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

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

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

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

Solution (Python):
def selectionSort(A, N):
    swap_count = 0

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

    return swap_count


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

    swap_count = selectionSort(numbers, N)

    print(*numbers)
    print(swap_count)

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

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

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

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


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

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

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

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

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

selection_sort = A[:]

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

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

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


if __name__ == '__main__':
    main()

Solution (Python):

def selection_sort(arr):
    swap_cnt = 0
    for i in range(0, len(arr)):
        min_index = i
        for j in range(i + 1, len(arr)):
            if arr[min_index] > arr[j]:
                min_index = j
        if min_index != i:
            arr[i], arr[min_index] = arr[min_index], arr[i]
            swap_cnt += 1
    return swap_cnt

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

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

c=0


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

print(*a)
print(c)