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


def selectionSort(array):
    count = 0 
    for i in range(len(array)):
        minj = i 
        flag = False
        for j in range(i, len(array)):
            if array[j] < array[minj]:
                minj = j 
                flag = True
        if flag and i != minj:
            array[i], array[minj] = [array[minj], array[i]]
            count += 1
    
    show(array)
    print(count)
    

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

Solution (Python):
def Selection(A, N):
    c = 0
    i = 0
    while i < N:
        minj = i
        j = i
        while j < N:
            if A[j] < A[minj]:
                minj = j
            j += 1
        if i != minj:
            c += 1
            a = A[i]
            A[i] = A[minj]
            A[minj] = a
        i += 1
    return A, c

N = int(input())
A = list(map(int, input().split(' ')))
A, c = Selection(A, N)
print(' '.join(map(str, A)) + '\n' + str(c))

Solution (Python):
def selectionSort(A, N):
    swapnum= 0
    for i in range(N):
        minj = i
        for j in range(i,N):
            if A[j] < A[minj]:
                minj = j
        if i!=minj:
            temp = A[i]
            A[i] = A[minj]
            A[minj] = temp
            swapnum += 1
    return swapnum
def showlist(A):
    for i in range(len(A)):
        if i==len(A)-1:
            print(A[i])
        else:
            print(A[i], end=' ')
N = eval(input())
A = list(map(int, input().split()))
swapnum = selectionSort(A, N)
showlist(A)
print(swapnum)

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

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

cnt = 0
for i in range(int(n)):
    mink = i
    for v in range(i, int(n)):
        if nums[v] < nums[mink]:
            mink = v
    if mink != i:
        nums[i], nums[mink] = nums[mink], nums[i]
        cnt += 1

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

Solution (Python):

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

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

for i in range(n):
    flag = 0
    minj = i
    for j in range(i, n):
        if array[j] < array[minj]:
            flag = 1
            minj = j

    if flag == 1:
        count += 1
    array[minj], array[i] = array[i], array[minj]


print(*array)
print(count)

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

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

Solution (Python):
def selection_sort(alist):
    """Sort alist by selection sort.
    Returns the sorted list and number of swap operation.

    >>> selection_sort([5, 6, 4, 2, 1, 3])
    ([1, 2, 3, 4, 5, 6], 4)
    """
    size = len(alist)
    c = 0

    for i in range(size):
        mini = i
        for j in range(i, size):
            if alist[mini] > alist[j]:
                mini = j

        if i != mini:
            alist[i], alist[mini] = alist[mini], alist[i]
            c += 1

    return (alist, c)


def run():
    _ = int(input()) 
    nums = [int(i) for i in input().split()]

    (sortednums, count) = selection_sort(nums)

    print(" ".join([str(i) for i in sortednums]))
    print(count)


if __name__ == '__main__':
    run()

Solution (Python):
def insetSort(lst,n):
    count = 0
    for i in range(0,n):
        min = i
        for j in range(i,n):
            if lst[j] < lst[min]: min = j
        if lst[i] != lst[min]: count += 1
        lst[i],lst[min] = lst[min],lst[i]

    return lst,count

n = int(input())
lst = [int(i) for i in input().split()]
lst = insetSort(lst,n)

print(*lst[0])
print(lst[1])

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

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

    for e in A[:-1]:
        print(e, end=' ')
    print(A[-1])
    print(steps)
    return A

selection_sort(A, N)

Solution (Python):




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

cnt = 0 


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


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

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

Solution (Python):





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

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

print(' '.join([str(x) for x in A]))
print(exc_num)

Solution (Python):
def selection(a, n):
    cnt = 0
    for i in range(n):
        flag = 0
        minj = i
        for j in range(i+1,n):
            if a[j] < a[minj]:
                minj = j
                flag = 1
        if flag == 1:
            temp = a[minj]
            a[minj] = a[i]
            a[i] = temp
            cnt += 1
    return cnt

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

cnt = selection(a, n)   
ans = str(a[0])
for i in range(1,n):
    ans += ' '+str(a[i])           
print(ans)
print(cnt)

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

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

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

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

Solution (Python):
def ssort(a,n):
    count = 0
    for i in range(n):
        mini = i
        for j in range(i, n):
            if a[mini]>a[j]:
                mini = j
        if i!=mini:
            a[i], a[mini] = a[mini], a[i]
            count+=1
    print(*a)
    print(count)
n= int(input())
a = [int(x) for x in input().split()]
ssort(a,n)

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

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

Solution (Python):
N = int(input())
A = [int(x) for x 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(" ".join(map(str, A)))
print(cnt)

Solution (Python):
def selectionSort(A, N): 
    A = list(map(int, A.split(" ")))
    change_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:
            
            tmp = A[i]
            A[i] = A[minj]
            A[minj] = tmp
            change_count+=1
    
    
    print(" ".join(map(str,A)))
    print(change_count)
    


N = int(input())  
A = input("")   
selectionSort(A,N)

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

if __name__ == '__main__':
    n = int(input())
    A = [int(x) for x in input().split()]
    result_A, count = selection_sort(A)
    print(*result_A)
    print(count)

Solution (Python):


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


count = 0
flag = 0
for i in range(N):

    if N == 1:
        break

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

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

    flag = 0


for i in range(N):
    if i == N - 1:
        print("{0}".format(A[i]))
    else:
        print("{0} ".format(A[i]), end="")

print(count)

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(minj != i):
            count += 1
        a[i], a[minj] = a[minj], a[i]
    return [a, count]
n = int(input())
a = list(map(int,input().split(' ')))
sa , c = selectionSort(a, n)
str = " ".join([str(i) for i in sa])
print("{}\n{}".format(str,c))

Solution (Python):
def selection_sort(n):
    c = 0
    r = n[:]
    l = len(r)
    for i in range(0, l):
        mini = i
        for j in range(i, l):
            if (r[j] < r[mini]):
                mini = j
        if mini != i:
            r[i], r[mini] = r[mini], r[i]
            c += 1
    return r, c

if __name__ == '__main__':
    limit = int(input())
    n = list(map(int, input().split()))[:limit]
    r, c = selection_sort(n)
    print(' '.join(map(str, r)))
    print(c)

Solution (Python):
def swap(l, i, j):
    tmp = l[i]
    l[i] = l[j]
    l[j] = tmp
    return l

def selection_sort(l):
    cnt = 0
    for i in range(len(l)):
        minj = i
        for j in range(i + 1, len(l)):
            if l[j] < l[minj]:
                minj = j
        if i != minj:
            swap(l, i, minj)
            cnt += 1
    return l, cnt

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

    sl, cnt = selection_sort(l)
    print(' '.join(map(str, sl)))
    print(cnt)

Solution (Python):
def selection_sort(a,n):
    count=0
    for i in range(n):
        ch=False
        minj=i
        for j in range(i,n):
            if a[j] < a[minj]:
                minj = j
                ch = True
        if(ch):
            count+=1
        a[i],a[minj] = a[minj],a[i]
    return count

if __name__ == "__main__":
    n=int(input())
    a=list(map(int,input().split()))
    count=selection_sort(a,n)
    print(*a)
    print(count)

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

print(*A)
print(counter)

Solution (Python):
def print_A():
    for i, n in enumerate(A):
        if i == N - 1:
            print(A[i])
        else:
            print(A[i], end=' ')

N = int(input())
A = [int(n) for n in input().split()]
cnt = 0
flg = 0
for i in range(N):
    min_j = i
    for j in range(i, N):
        if A[j] < A[min_j]:
            min_j = j
            flg = 1
    if flg == 1:
        cnt += 1
        flg = 0
        w = A[i]; A[i] = A[min_j]; A[min_j] = w
print_A()
print(cnt)

Solution (Python):
N = int(input())
ary = [int(_) for _ in input().split()]


def selection_sort(ary, verbose=True):
    count = 0

    for i in range(0, N):
        minj = i

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

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

    print(' '.join([str(_) for _ in ary]))

    if verbose:
        print(count)


selection_sort(ary)

Solution (Python):


from math import sqrt, floor

def intinput():
    a = input().split()
    for i in range(len(a)):
        a[i] = int(a[i])
    return a

def show(a):
    
    _str = ""
    for i in range(len(a) - 1):
        _str += str(a[i]) + " "
    _str += str(a[len(a) - 1])
    print(_str)

def selection_sort(a):
    
    
    count = 0
    for i in range(len(a)):
        
        
        
        minj = i
        for j in range(i, len(a)):
            if a[j] < a[minj]: minj = j
        if a[i] > a[minj]: count += 1; a[i], a[minj] = a[minj], a[i]
    return count

def main():
    N = int(input())
    A = intinput()
    count = selection_sort(A)
    show(A); print(count)

if __name__ == "__main__":
    main()

Solution (Python):

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

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

B = " ".join([str(num) for num in A])
print(B)
print(times)

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


def print_list_split_whitespace(a):
    for x in a[:-1]:
        print(x, end=' ')
    print(a[-1])


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

counter = selection_sort(a, n)
print_list_split_whitespace(a)
print(counter)

Solution (Python):



if __name__ == '__main__':
    N = int(raw_input())
    l = map(int, raw_input().split())
    cnt = 0
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if l[j] < l[minj]:
                minj = j
        if i != minj:
            l[i], l[minj] = l[minj], l[i]
            cnt += 1
    print ' '.join(map(str, l))
    print cnt

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

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

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

Solution (Python):
def selection_sort(a):
    res = 0
    for i in range(len(a)):
        m = i
        for j in range(i, len(a)):
            if a[m] > a[j]:
                m = j
        if m != i:
            res += 1
            a[i], a[m] = a[m], a[i]
    return res


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


if __name__ == '__main__':
    main()

Solution (Python):
def print_list():
    for i,e in enumerate(A):
        if i == N - 1:
            print(e)
        else:
            print(e, end=" ")

N = int(input())
A = [int(e) for e in input().split()]
cnt = 0
for i in range(N):
    min_index = i
    for j in range(i, N):
        if A[min_index] > A[j]:
            min_index = j
    if i != min_index:
        w = A[i]; A[i] = A[min_index]; A[min_index] = w
        cnt += 1
print_list()
print(cnt)

Solution (Python):
from sys import stdin
def stdinput():
    return stdin.readline().strip()

def main():
    n = int(stdinput())
    A = list(map(int, stdinput().split()))

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

if __name__ == '__main__':
    main()

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

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

Solution (Python):
import sys

def print_arr(arr):
	for i in range(len(arr)):
		sys.stdout.write(str(arr[i]))
		if i != len(arr) - 1:
			sys.stdout.write(' ')
	print()

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

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

print_arr(arr)
print(swap_count)

Solution (Python):
import sys

def print_arr(arr):
	for i in range(len(arr)):
		sys.stdout.write(str(arr[i]))
		if i != len(arr) - 1:
			sys.stdout.write(' ')
	print()

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

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

print_arr(arr)
print(swap_count)

Solution (Python):
def print_arr(arr):
	arr_str = ''
	for i in range(len(arr)):
		arr_str += str(arr[i])
		if i != len(arr) - 1:
			arr_str += ' '
	print(arr_str)

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

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

print_arr(arr)
print(swap_count)

Solution (Python):
import sys

def print_arr(arr):
	for i in range(len(arr)):
		sys.stdout.write(str(arr[i]))
		if i != len(arr) - 1:
			sys.stdout.write(' ')
	print()

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

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

print_arr(arr)
print(swap_count)

Solution (Python):
import sys

def print_arr(arr):
	for i in range(len(arr)):
		sys.stdout.write(str(arr[i]))
		if i != len(arr) - 1:
			sys.stdout.write(' ')
	print()

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

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

print_arr(arr)
print(swap_count)

Solution (Python):
import sys

def print_arr(arr):
	for i in range(len(arr)):
		sys.stdout.write(str(arr[i]))
		if i != len(arr) - 1:
			sys.stdout.write(' ')
	print()

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

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

cnt = selection_sort(arr)
print_arr(arr)
print(cnt)

Solution (Python):
times = int(input())
numbers = list(map(int, input().split()))
count = 0
minj = 0

for i in range(times):
    minj = i
    for j in range(i, times, 1):
        if numbers[j] < numbers[minj]:
            minj = j

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

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

Solution (Python):
N = int(input())
arr = [int(n) for n in input().split()]

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

print(' '.join(map(str, arr)))
print(swap_cnt)

Solution (Python):
N = int(input())
arr = [int(n) for n in input().split()]

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

print(' '.join(map(str, arr)))
print(swap_cnt)

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

Solution (Python):
N = int(input())
arr = [int(n) for n in input().split()]

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

print(' '.join(map(str, arr)))
print(swap_cnt)

Solution (Python):
def selectionSort(A,N):
    n_swap = 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]
            n_swap += 1
    return n_swap, A

def run():
    n = int(input())
    a = list(map(int, input().split()))
    c, a = selectionSort(a,n)
    print(' '.join([str(i) for i in a]))
    print(c)

if __name__ == '__main__':
    run()

Solution (Python):
N = int(input())
l = input().split()
l = [int(s) for s in l]
count = 0
for j in range(N-1):
    min = l[j]
    for i in range(j,N):
        if( min > l[i]):
            min = l[i]
            a = i
    if(l[j]>min):
        l[j],l[a] = l[a],l[j]
        count += 1
for i in range(0,len(l)-1):
    print(l[i],end=' ')
print(l[N-1])
print(count)

Solution (Python):









A = []



LENGTH =int(input())
A = input().split()


i = 0
CHANGE_COUNT = 0

while i <= LENGTH -1:
    j = i + 1
    mini = i
    while j <= LENGTH -1:
        if int(A[j]) < int(A[mini]) :
            mini = j
        j += 1
    if mini != i:
        tmp = A[i]
        A[i] = A[mini]
        A[mini] = tmp
        CHANGE_COUNT += 1
    i += 1

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

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

if __name__ == "__main__":
    cnt = 0
    for i in range(N):
        tmp_idx = i
        for j in range(i+1, N):
            if A[tmp_idx] > A[j]:
                tmp_idx = j
        if tmp_idx != i:
            A[i], A[tmp_idx] = A[tmp_idx], A[i]
            cnt += 1

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

Solution (Python):
def selectionSort(A, N):
    swap = 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]
            swap += 1

    print(" ".join(list(map(str, A))))
    print(swap)

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

    selectionSort(A, N)

main()

Solution (Python):
def selectionSort(array):
    n = len(array)
    cnt = 0
    for i in range(n):
        minj = i
        for j in range(i, n):
            if array[j] < array[minj]:
                minj = j
        array[i], array[minj] = array[minj], array[i]
        if i != minj and array[i] != array[minj]:
            cnt += 1

    return (array, cnt)

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

resultArray, cnt = selectionSort(array)

print(*resultArray)
print(cnt)

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

Solution (Python):
n = int(input())
nlist = list(map(int, input().split()))
def SelectionSort(A, n):
    count = 0
    for i in range(n):
        mini = i
        a = A[i]
        for j in range(i, n):
            if(A[j] < A[mini]):
                mini = j
        if(mini != i):
            A[i] = A[mini]
            A[mini] = a
            count += 1
            
    for i in range(n):
        if(i == n-1):
            print('{}'.format(A[i]), end = '')
        else:
            print('{}'.format(A[i]), end = ' ')
    print()
    print(count)
SelectionSort(nlist, n)

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

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
print(" ".join([str(n) for n in a]))
print(c)

Solution (Python):


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

def selectionSort(A, N):
    """N個の要素を含む0-オリジンの配列A"""
    nswap = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if A[j] < A[minj]:
                minj = j
        if i != minj:
            swap(A, i, minj)  
            nswap += 1
    return nswap

N = int(input())
A = [int(t) for t in input().split()]
if len(A) != N: raise

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

Solution (Python):
input()
arr = list(map(int, input().split()))
count = 0
for i in range(len(arr)):
    min = arr[i]
    min_index = i
    for j in range(i+1, len(arr)):
        if arr[j] < min:
            min_index = j
            min = arr[j]
    if i != min_index:
        count += 1
        t = arr[i]
        arr[i] = min
        arr[min_index] = t
print(" ".join(map(str, arr)))
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 = input()
x = map(int, raw_input().split())
c = 0
for i in range(n):
    mini = i
    for j in range(i, n):
        if x[j] < x[mini]:
            mini = j
    if i != mini:
        x[i], x[mini] = x[mini], x[i]
        c += 1
for e in x:
    print e,
print
print c

Solution (Python):

from __future__ import division, print_function
from sys import stdin
stdin.readline()
data = [int(s) for s in stdin.readline().split()]
swap_times = 0
for i in range(len(data)):
    mini = i
    swap_flag = False
    for j in range(i, len(data)):
        if data[j] < data[mini]:
            mini = j
            swap_flag = True
    data[i], data[mini] = data[mini], data[i]
    swap_times += swap_flag
print(*data)
print(swap_times)

Solution (Python):

i = input()
n = map(int, raw_input().split())
cnt=0

for i in xrange(len(n)):
    min = i

    flag = 0
    for j in xrange(i, len(n)):
        if n[j] < n[min]:
            min=j

            flag = 1
    n[i],n[min] = n[min],n[i]
    cnt+=flag

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

Solution (Python):
n = input()
a = map(int, raw_input().split())
times = 0
for i in xrange(n):
	mini = i
	for j in range(i+1, n):
		if a[j] < a[mini]:
			mini = j
	if mini != i:
		a[i], a[mini] = a[mini], a[i]
		times += 1
print ' '.join(map(str, a))
print times

Solution (Python):


i = input()
n = map(int, raw_input().split())
cnt=0


for i in xrange(len(n)):

    min=i

    flag=0

    for j in xrange(i,len(n)):
        if n[j]<n[min]:


            min=j
            flag=1

    n[i],n[min]=n[min],n[i]
    cnt+=flag
print ' '.join(map(str,n))
print cnt

Solution (Python):
n = int(raw_input())
sec = map(int,raw_input().split())

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

Solution (Python):
n = input()
A = map(int, raw_input().split())
count = 0
for i in xrange(n):
    mini = i
    for j in xrange(i,n):
        if A[j] < A[mini]:
            mini = j
    if i < mini:
        key = A[mini]
        A[mini] = A[i]
        A[i] = key
        count += 1
print ' '.join(map(str, A))
print count

Solution (Python):
def SS(n, a):
    count = 0
    for i in range(n):
        mini = i
        for j in range(i,n):
            if a[j] < a[mini]:
                mini = j
        a[i], a[mini] = a[mini], a[i]
        count += 1 if i != mini else 0
    return count, a
    
n = int(raw_input())
a = map(int, raw_input().split())

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

Solution (Python):
def ss(n, a):
    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:
            a[i], a[mini] = a[mini], a[i]
            count += 1
    print " ".join(map(str, a))
    print count
    
n = int(raw_input())
a = map(int, raw_input().split())
ss(n, a)

Solution (Python):
n = int(raw_input())
a = map(int, raw_input().split())
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:
        a[i], a[mini] = a[mini], a[i]
        count += 1
print " ".join(map(str, a))
print count