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 selectionSort(A, N):
    count = 0
    for i in xrange(0, N):
        minj = i
        for j in xrange(i, N):
            if A[j] < A[minj]:
                minj = j
        if i != minj:
            A[i] , A[minj] = A[minj], A[i]
            count += 1
    return A, count

def main():
    n = input()
    a = map(int, raw_input().split())
    a, count = selectionSort(a, n)
    print " ".join(map(str, a))
    print count

main()

Solution (Python):
def selectionSort(nums, length):
  count = 0
  for i in xrange(length):
    minj = i
    for j in xrange(i, length):
      
      
      if nums[j] < nums[minj]:
        minj = j
    nums[i], nums[minj] = nums[minj], nums[i]
    count += 1 if i != minj else 0
  return [nums, count]

length = int(raw_input())
nums = map(int, raw_input().split())
ans = selectionSort(nums, length)

print " ".join(map(str, ans[0]))
print ans[1]

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

nswap=0
for i in range(n):
    min_v = a[i]
    min_p = i
    for j in range(i,n):
        if min_v > a[j]:
            min_v = a[j]
            min_p = j
    if min_p != i:
        a[i],a[min_p]=a[min_p],a[i]
        nswap+=1

print ' '.join([str(v) for v in a])
print nswap

Solution (Python):


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

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

ans_list, count = Selection_Sort(numbers, n)
ans_list = map(str, ans_list)

print(" ".join(ans_list))
print(count)

Solution (Python):
def selectionSort(A):
    N = len(A)
    cnt = 0
    for i in range(N):
        min_i = i
        for j in range(i,N):
            if A[j] < A[min_i]:
                min_i = j
        if min_i != i:
            A[i], A[min_i] = A[min_i], A[i]
            cnt += 1
    print(*A)
    print(cnt)

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

Solution (Python):
def SelectionSort(A):
    global count
    flag = False
    for i in range(len(A)):
        mini = i
        for j in range(i, len(A)):
            if A[j] < A[mini]:
                mini = j
        A[i], A[mini] = A[mini], A[i]
        if i is not mini:
            count += 1
        
N = input()
A = [int(i) for i in input().split()]
count = 0
SelectionSort(A)
print(*A)
print(count)

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

print(*a)
print(c)

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

    return a, c

if __name__ == "__main__":
    n = int(input())
    a_l = [int(a) for a in input().split()]

    as_l, c = selectionSort(a_l, n)
    print(' '.join(map(str, as_l)))
    print(c)

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

def main():
    N = int(raw_input())
    A = map(int,raw_input().split())
    newA, count = selectionSort(A, N)
    print ' '.join(map(str,newA))
    print count
    return 0

if __name__ == "__main__":
    main()

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

Solution (Python):
def selectionSort(A, N):
    cnum = 0
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j] < A[minj]:
                minj = j
        if i != minj:
            tmp = A[i]
            A[i] = A[minj]
            A[minj] = tmp
            cnum += 1
    print " ".join(str(t) for t in A)
    print cnum

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

Solution (Python):
def selectionSort(A, N):
    count = 0
    for i in range(N - 1):
        minValue = 100
        minIndex = 0
        for j in range(i, N):
            if A[j] < minValue:
                minValue = A[j]
                minIndex = j
        if minIndex != i:
            temp = A[i]
            A[i] = minValue
            A[minIndex] = temp
            count = count + 1

    for i in range(N):
        print A[i],
    print
    print count

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

selectionSort(A, N)

Solution (Python):
def trace(a,n) :
    li=[]
    for i in xrange(n):
        li.append(str(a[i]))
    print(" ".join(li))
    
def selectionsort(a,n):
    ct=0
    for i in xrange(n):
        if i==n-1:
            break
        else :
            mini=i
            for j in xrange(i,n,1):
                if a[j]<a[mini]:
                    mini=j
            if i!=mini:
                keep=a[i]
                a[i]=a[mini]
                a[mini]=keep
                ct+=1
    return ct
            
            
n=input()
a=map(int,raw_input().split())
ct=selectionsort(a,n)
trace(a,n)
print(ct)

Solution (Python):
N = int(raw_input())
A = map(int, raw_input().split())
count = 0
for i in range(0, N):
    min_j = i
    c = 0
    for j in range(i, N):
        if A[j] < A[min_j]:
            min_j = j
            c = 1
    A[i], A[min_j] = A[min_j], A[i]
    count += c
print " ".join(map(str, A))
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
        a[i], a[minj] = a[minj], a[i]
        count += 1 if i != minj else  0
    return count

def show(a, n):
    for i in range(n):
        if i == n-1:
            print "%d" % a[i]
        else:
            print "%d" % a[i],

n = input()
a = map(int, raw_input().split())
c = selectionSort(a, n)
show(a, n)
print "%d" % c

Solution (Python):


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


def InsertionSort(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:
            tmp = A[i]
            A[i] = A[minj]
            A[minj] = tmp
            c+=1

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

InsertionSort(A,N)

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

if __name__ == '__main__':
	n = int(input())
	R = list(map(int, input().split()))
	cnt = 0
	R = selectionSort(R, n)
	print(" ".join(map(str, R)))
	print(cnt)

Solution (Python):


N = int(raw_input())
A = map(int, raw_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 " ".join(map(str, A))
print count

Solution (Python):
import math

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
        temp = A[i]
        A[i] = A[minj]
        A[minj] = temp
    
print(' '.join(map(str, A)))
print(count)

Solution (Python):
def SelectionSort(A, n):
    sw = 0
    for i in xrange(n):
        minj = i
        for j in xrange(i, n):
            if A[j] < A[minj]:
                minj = j
        temp = A[minj]
        A[minj] = A[i]
        A[i] = temp
        if i != minj:
            sw += 1
    return sw

def printArray(A, n):
    for i in xrange(n):
        if i < n-1:
            print A[i],
        else:
            print A[i]

n = input()
arr = map(int, raw_input().split())
cnt = SelectionSort(arr, n)
printArray(arr, n) 
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, n):
        if a[j] < a[minj]:
            minj = j
    if i != minj:
        a[i], a[minj] = a[minj], a[i]
        count += 1

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

Solution (Python):
data_num = int(input())
data = [int(n) for n in input().split(' ')]
changed = 0

for i in range(0, data_num):
    minj = i
    for j in range(i, data_num):
        if data[j] < data[minj]:
            minj = j
    data[i], data[minj] = data[minj], data[i]
    if i != minj:
        changed += 1

print(' '.join([str(i) for i in data]))
print(changed)

Solution (Python):
def show_list(a):
    for x in a:
        print x,
    print

def selection_sort(a,n):
    count=0
    for i in xrange(n):
        minj = i
        for j in xrange(i,n):
            if a[j]<a[minj]:
                minj = j
        if minj!=i:  
            tmp = a[i]
            a[i]=a[minj]
            a[minj]=tmp
            count+=1
    show_list(a)
    print count


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


if __name__ == '__main__':
    main()

Solution (Python):
def selection_sort(array):
    for i in range(len(array)):
        j = min(range(i, len(array)), key=lambda x: array[x])
        array[i], array[j] = array[j], array[i]


class SegmentTree(object):

    def __init__(self, size, op=min, init=float("inf")):
        self.size = 1
        self.op = op
        self.init = init
        while self.size < size:
            self.size *= 2
        self.data = [init] * (self.size*2+2)

    def update(self, idx, value):
        k = idx + self.size - 1
        self.data[k] = value
        while k > 0:
            k = (k - 1) // 2
            self.data[k] = self.op(self.data[k*2+1], self.data[k*2+2])

    def __getitem__(self, val):
        return self.data[(self.size-1)+val]

    def find(self, start, end):
        def query(k, left, right):
            if right <= start or end <= left:
                return self.init
            if start <= left and right <= end:
                return self.data[k]
            vl = query(k*2+1, left, (left+right)//2)
            vr = query(k*2+2, (left+right)//2, right)
            return self.op(vl, vr)
        return query(0, 0, self.size)


def swap_count(array):
    count = 0
    rmq = SegmentTree(len(array), op=min, init=(float("inf"), float("inf")))
    for i, v in enumerate(array):
        rmq.update(i, (v, i))
    for i in range(len(array)):
        vi, _ = rmq[i]
        vj, idx = rmq.find(i, len(array))
        j = idx
        if rmq[i] <= rmq[j]:
            continue
        count += 1
        rmq.update(i, (vj, i))
        rmq.update(j, (vi, j))
    return count

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    print(*sorted(A))
    count = swap_count(A)
    print(swap_count(A))

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


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

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[minj] > A[j]:
				minj = j
		if i != minj:
			tmp = A[minj]
			A[minj] = A[i]
			A[i] = tmp
			count += 1
	return A,count
ans,c = selectionSort(a,N)
print(*ans)
print(c)

Solution (Python):



def pnt(s):
 for i in xrange(len(s)):
  print s[i],
 print

n=int(raw_input())
a=map(int,raw_input().split())

cnt=0
for i in xrange(n):
 mini = i
 for j in xrange(i + 1, n):
  if a[j] < a[mini]:
   mini = j
 if i != mini:
  a[i], a[mini] = a[mini], a[i]
 if i!=mini:
  cnt+=1
pnt(a)
print cnt

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

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

Solution (Python):


x = int(input())
y = list(map(int,input().split()))
z = 0

for i in range(x):
	minj = i
	for j in range(i,x):
		if y[j] < y[minj]:
			minj = j
	if not i == minj:
		y[i], y[minj] = y[minj], y[i]
		z += 1

print(" ".join(list(map(str,y))))
print(z)

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

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

    if i != minj:
        nb_exchange += 1
        data[i], data[minj] = data[minj], data[i]

print(" ".join(map(str, data)))
print(nb_exchange)

Solution (Python):

sample_input = list(range(3))
sample_input[0] = '''6
5 6 4 2 1 3'''
sample_input[1] = '''6
5 2 4 6 1 3'''
sample_input[2] = ''''''
give_sample_input = None
if give_sample_input is not None:
    sample_input_list = sample_input[give_sample_input].split('\n')
    def input():
        return sample_input_list.pop(0)
        

def swap_list_item(lst, i, j):
    tmp = lst[i]
    lst[i] = lst[j]
    lst[j] = tmp

num_of_data = int(input())
data_list = list(map(int, input().split()))
swap_count = 0
for i in range(num_of_data):
    minj = i
    for j in range(i, num_of_data):
        if data_list[minj] > data_list[j]:
            minj = j
    if not minj == i:
        swap_list_item(data_list, i, minj)
        swap_count += 1

result = ''
for number in data_list:
    result += str(number) + ' '
print(result.strip())
print(swap_count)

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

for i in range(n-1):
    mini = i
    for j in range(i,n):
        if array[j] < array[mini]:
            mini = j
    if mini != i :
        array[i],array[mini] = array[mini],array[i]
        count += 1

print(" ".join(list(map(str,array))))
print(count)

Solution (Python):



def selection_sort(A, N):
    change = 0
    for i in xrange(N):
        minj = i
        for j in xrange(i, N, 1):
            if A[j] < A[minj]:
                minj = j
        if minj != i:
            temp = A[minj]
            A[minj] = A[i]
            A[i] = temp
            change += 1
    return (A, change)


if __name__ == "__main__":
    N = input()
    A = map(int, raw_input().split())
    result, change = selection_sort(A, N)
    print ' '.join(map(str, result))
    print change

Solution (Python):
n = int(input())
A = input().split(' ')
count = 0

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

    for j in range(i, n):
        if int(A[j]) < int(A[minj]):
            minj = j

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

print(" ".join(A))
print(count)

Solution (Python):
def main():
  element_num = int(input())
  elements = [int(s) for s in input().split()]
  swap_count = 0

  for i in range(0, element_num - 1):
    min_val = elements[i + 1]
    min_index = i + 1
    for j in range(i + 1, element_num):
      if elements[j] < min_val:
        min_val = elements[j]
        min_index = j
    if elements[i] > elements[min_index]:
      elements[i], elements[min_index] = elements[min_index], elements[i]
      swap_count += 1

  print(' '.join(list(map(str, elements))))
  print(swap_count)

  return 0


if __name__ == '__main__':
  main()

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

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

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

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

A ,count = selectionSort(A)
print(' '.join([str(i) for i in A]))
print(count)

Solution (Python):
def selection_sort(array):
    count_swap = 0
    for i in range(len(array)):
        minj = i
        for j in range(i + 1,len(array)):
            if array[j] < array[minj]:
                minj = j
        if array[minj] is not array[i]:
            tmp = array[minj]
            array[minj] = array[i]
            array[i] = tmp
            count_swap += 1
    return count_swap


def print_array(array):
     print(str(array)[1:-1].replace(', ', ' '))


def main():
    N = int(input())
    array = [int(s) for s in input().split(' ')]
    count = selection_sort(array)
    print_array(array)
    print(count)

if __name__ == '__main__':
    main()

Solution (Python):
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
        a[i], a[minj] = a[minj], a[i]
        if i != minj:
            count += 1
    return count


def main():
    n = int(input())
    a = [int(x) for x in input().split(' ')]
    count = selectionSort(a, n)
    print(' '.join([str(x) for x in a]))
    print(count)


if __name__ == '__main__':
    main()

Solution (Python):


n = int(input())
A = list(map(int, input().split()))
mini = 1
c = 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]
    if mini != i:
        c += 1

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

Solution (Python):


n = int(input())
A = list(map(int, input().split()))
mini = 1
c = 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]
    if mini != i:
        c += 1

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

Solution (Python):
length = int(input())
targ = [int(n) for n in input().split(' ')]
ans = 0
for l in range(length):
    value = l
    for init in range(l + 1,length):
        if targ[value] > targ[init]:
            value = init
    if value != l:
        disp = targ[l]
        targ[l] = targ[value]
        targ[value] = disp
        ans += 1
print(' '.join([str(n) for n in targ]))
print(ans)

Solution (Python):

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

cnt = 0

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

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

Solution (Python):
N = int(input())
A = [int(a) for a in input().split(" ")]
swapnum = 0
for i in range(N):
    mini = i
    swap = False
    for j in range(i, N):
        if A[j] < A[mini]:
            mini = j
            swap = True
    if swap:
        tmp = A[i]
        A[i] = A[mini]
        A[mini] = tmp
        swapnum += 1
print(" ".join([str(a) for a in A]))
print(swapnum)

Solution (Python):


def selection_sort(a, n):
    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 a[i] != a[minj]:
            count += 1
    return a, count

def main():
    input_num = int(input())
    input_list = [int(i) for i in input().split()]

    ans_list, count = selection_sort(input_list, input_num)
    for i in range(input_num):
        if i != 0:
            print(" ", end="")
        print(ans_list[i], end='')
    print()
    print(count)

if __name__ == '__main__':
    main()

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

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

count = 0
def swap(a, b):
    t = a
    a = b
    b = t
    return [a, b]

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

l = selection_sort(A)
print(' '.join(str(x) for x in l))
print(count)

Solution (Python):
n = int(input())
li = list(map(int, input().split()))
cnt = 0

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

print(*li)
print(cnt)

Solution (Python):
import sys

def selection_sort(nums, n):
    cnt = 0
    
    for i in range(n):
        minj = i
        for j in range(i, n):
            if nums[j] < nums[minj]:
                minj = j

        nums[i], nums[minj] = nums[minj], nums[i]
        if i != minj:
            cnt += 1

    print(' '.join([str(i) for i in nums]))
    print(cnt)
    
def main():
    n = int(sys.stdin.readline().strip())
    data = sys.stdin.readline().strip().split(' ')
    nums = [int(i) for i in data]
    selection_sort(nums, n)

if __name__ == '__main__':
    main()

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

counter = 0
for i in xrange(N):
  minj = i
  for j in xrange(i, N):
    if A[j] < A[minj]:
      minj = j
  if i != minj:
    tmp = A[i]
    A[i] = A[minj]
    A[minj] = tmp
    counter += 1
print " ".join(map(str, A))
print counter

Solution (Python):
length = int(input())
eles = [int(l) for l in input().split()]
times = 0

for i in range(length-1):
    _min = i
    for j in range(i, length):
        if eles[j] < eles[_min]:
            _min = j
    if i != _min:
        eles[i], eles[_min] = eles[_min], eles[i]
        times += 1

print(*eles)
print(times)

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

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

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

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


print(" ".join(str(x) for x in m))
print(count)

Solution (Python):

def SS(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:
            count+=1
            swap=A[i]
            A[i]=A[minj]
            A[minj]=swap
    for i in range(len(A)):
        A[i]=str(A[i])
    print(" ".join(A))
    print(count)

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

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

print(' '.join([str(A[i]) for i in range(N)]))
print(counter)

Solution (Python):
N = int(raw_input())
nums = map(int, raw_input().split(" "))

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


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

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

Solution (Python):




class Solution:
    @staticmethod
    def selection_sort():
        
        array_length = int(input())
        unsorted_array = [int(x) for x in input().split()]

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

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


if __name__ == '__main__':
    solution = Solution()
    solution.selection_sort()

Solution (Python):




def selectionSort(arr, N): 
    counter = 0 
    for i in range(N):
        minj = i 
        for j in range(i,N):
            if arr[minj] > arr[j]:
                minj = j 
        if minj != i: counter += 1
        arr[i], arr[minj] = arr[minj],arr[i]

    return counter,arr

def main():

    N = int(raw_input())
    A = map(int, raw_input().split())
    
    counter,sortedA = selectionSort(A, N)
    print ' '.join(map(str,sortedA))
    print counter



if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
L = list(map(int, input().split()))
p = 0
for i in range(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]
        p += 1

print(*L)
print(p)

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

Solution (Python):





def selection_sort(target_list):
    list_length = len(target_list)
    change_count = 0
    for i in range(list_length):
        min_index = i
        for j in range(i, list_length):
            if target_list[j] < target_list[min_index]:
                min_index = j
        else:
            if not i == min_index:
                tmp = target_list[i]
                target_list[i] = target_list[min_index]
                target_list[min_index] = tmp
                change_count += 1
                
    return change_count
def main():
    
    n_list = int(input())
    target_list = [int(n) for n in input().split()]
    cc = selection_sort(target_list)
    print(*target_list)
    print(cc)
    
if __name__ == "__main__":
    main()

Solution (Python):

def print_array(A, N):
    for i in xrange(N - 1):
        print str(A[i]),

    print A[N - 1]

def selectionSort(A, N):
    swap_num = 0
    for i in xrange(0, N):
        minj = i
        for j in xrange(i, N):
            if A[j] < A[minj]:
                minj = j

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

    return swap_num

def main():
    N = int(raw_input())
    A = []

    for num in raw_input().split():
        A.append(int(num))

    temp = selectionSort(A, N)
    print_array(A, N)

    print temp


if __name__ == "__main__":
    main()

Solution (Python):
def selection_sort(numbers, n):
    """selection sort method

    Args:
        numbers: a list of numbers to be sorted
        n: len(numbers)

    Returns:
        sorted numberd, number of swapped times
    """
    counter = 0
    for i in range(0, n - 1):
        min_j = i
        for j in range(i + 1, n):
            if numbers[j] < numbers[min_j]:
                min_j = j
        if min_j != i:
            numbers[min_j], numbers[i] = numbers[i], numbers[min_j]
            counter += 1
    return numbers, counter


length = int(raw_input())
numbers = [int(x) for x in raw_input().split()]

numbers, number_of_swapped_times = selection_sort(numbers, length)
print(" ".join([str(x) for x in numbers]))
print(number_of_swapped_times)

Solution (Python):
def selection_sort(numbers, n):
    """selection sort method

    Args:
        numbers: a list of numbers to be sorted
        n: len(numbers)

    Returns:
        sorted numberd, number of swapped times
    """
    counter = 0
    for i in range(0, n - 1):
        min_j = i
        for j in range(i + 1, n):
            if numbers[j] < numbers[min_j]:
                min_j = j
        if min_j != i:
            numbers[min_j], numbers[i] = numbers[i], numbers[min_j]
            counter += 1
    return numbers, counter


length = int(raw_input())
numbers = [int(x) for x in raw_input().split()]

numbers, number_of_swapped_times = selection_sort(numbers, length)
print(" ".join([str(x) for x in numbers]))
print(number_of_swapped_times)

Solution (Python):


N=input()
A=map(int,raw_input().split())
num=0

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

for m in range(N-1):
	print str(A[m]),

print A[N-1]
print num

Solution (Python):
def do_selection_sort(data, size):
    assert size == len(data), "data size error"
    swap_count = 0
    for i in range(size):
        minj = i
        for j in range(i, size):
            if data[j] < data[minj]:
                minj = j
        temp = data[i]
        data[i] = data[minj]
        data[minj] = temp
        if data[i] != data[minj]:
            swap_count += 1
    return swap_count


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

    
    result = do_selection_sort(data, num)

    
    print('{0}'.format(' '.join(map(str, data))))
    print('{0}'.format(result))