Problem:


Selection Sort

Write a program of the Selection Sort algorithm which sorts a sequence A in ascending order. The algorithm should be based on the following pseudocode:

SelectionSort(A)
1 for i = 0 to A.length-1
2     mini = i
3     for j = i to A.length-1
4         if A[j] N, the number of elements in the sequence.

In the second line, N elements of the sequence are given separated by space characters.

Output

The output consists of 2 lines. 

In the first line, please print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character.

In the second line, please print the number of swap operations.

Constraints

1 ≤ N ≤ 100

Sample Input 1

6
5 6 4 2 1 3

Sample Output 1

1 2 3 4 5 6
4

Sample Input 2

6
5 2 4 6 1 3

Sample Output 2

1 2 3 4 5 6
3



Sample input:
6
5 6 4 2 1 3


Sample output:
1 2 3 4 5 6
4

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in 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:
        times += 1
    A[i], A[minj] = A[minj], A[i]
  


print(*A)
print(times)

Submission status: Accepted

Solution code:
def selectionSort():
    l = int(input())
    A = list(map(int, input().split(" ")))
    count = 0
    for i in range(l-1):
        mini = i
        for j in range(i, l-1):
            if (A[mini]>A[j+1]):
                mini = j+1
        if (i != mini):
            temp = A[mini]
            A[mini] = A[i]
            A[i] = temp
            count += 1
    print(" ".join(map(str, A)))
    print(count)
selectionSort()

Submission status: Accepted

Solution code:
# coding: utf-8
# Here your code !
S = int(input())
N = list(map(int,input().split(" ")))


def seleSort(n,s):
    c =0
    for i in range(s):
        minj = i
        for j in range(i+1,s):
            if n[j]<n[minj]:
                minj = j
                
        n[i],n[minj]=n[minj],n[i]
        if i!= minj:
            c+=1
    print(" ".join(list(map(str,n))))
    print(c)
        
        
seleSort(N,S)

Submission status: Accepted

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

swap_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]
        swap_count += 1
print(*A)
print(swap_count)

Submission status: Accepted

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

for i in range(n):
    mi = min(a[i:])
    if a[i] > mi:
        j = a[i:].index(mi)+i
        a[i], a[j] = a[j], a[i]
        cnt += 1

print(" ".join([str(i) for i in a]))
print(cnt)

Submission status: Accepted

Solution code:
N, A, cou   = int(input()), [int(temp) for temp in input().split()], 0

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
import os
import math


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


def selection_sort(A):
    swap_num = 0
    for i in range(N):
        min_i = i
        for j in range(i + 1, N):
            if A[j] < A[min_i]:
                min_i = j
        if i != min_i:
            A[i], A[min_i] = A[min_i], A[i]
            swap_num += 1
    print(*A)
    print(swap_num)

selection_sort(A)

Submission status: Accepted

Solution code:
#!/usr/bin/env python


def selectionSort(line, size):
    """Rerutn sorted list.

    >>> selectionSort([1, 2, 3, 4], 4)
    ([1, 2, 3, 4], 0)

    >>> selectionSort([5,6,4,2,1,3], 6)
    ([1, 2, 3, 4, 5, 6], 4)
    """
    count = 0
    for i in range(0, size):
        minj = i
        for j in range(i, size):
            if line[j] < line[minj]:
                minj = j
        if line[i] != line[minj]:
            tmp = line[i]
            line[i] = line[minj]
            line[minj] = tmp
            count += 1
    return line, count


def main():
    size = int(input())
    line = list(map(int, input().split()))
    ans_list, count = selectionSort(line, size)
    ans = " ".join(map(str, ans_list))
    print(ans)
    print(count)


if __name__ == '__main__':
    import doctest
    doctest.testmod()
    main()

Submission status: Accepted

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

x = 0

for i in range(N):
    minj = i
    y = 0
    for j in range(i,N):
        if A[j] < A[minj]:
            minj = j
            y = 1
            
    v = A[i]
    A[i] = A[minj]
    A[minj] = v
    x += y
    
for i in range(N-1):
    print A[i],
print A[-1]

print x

Submission status: Accepted

Solution code:
N = input()
A = map(int,raw_input().split())
x = 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:        
        v = A[i]
        A[i] = A[minj]
        A[minj] = v
        x += 1
    
for i in range(N-1):
    print A[i],
print A[-1]
print x

Submission status: Accepted

Solution code:
#3.4

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

swapCount = 0
for i in range(0, N):
    minIndex = i
    for j in range(i, N):
        if a[j] < a[minIndex]:
            minIndex = j
    
    if i != minIndex:
        tmp = a[i]
        a[i] = a[minIndex]
        a[minIndex] = tmp
        swapCount += 1

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

Submission status: Accepted

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

    return (a,count)

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

Submission status: Accepted

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

        if i != minj: 
            temp = a[minj]
            a[minj] = a[i]
            a[i] = temp
            cnt += 1
    print(" ".join(map(str,a)))
    print(cnt)

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

selectionSort(a,n)

Submission status: Accepted

Solution code:
def main():
    tmp = input()
    ls = list(map(int, input().split(' ')))
    count = 0
    
    for i in range(len(ls)):
        minj = i
        
        for j in range(i, len(ls)):
            if ls[j] < ls[minj]:
                minj = j
        ls[i], ls[minj] = ls[minj], ls[i]

        if minj != i:
            count += 1

    print(' '.join(map(str,ls)))
    print(count)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def selection_sort(A, n):
	count = 0

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

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

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

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

	selection_sort(A, n)

Submission status: Accepted

Solution code:
#ALDS1_2-B Sort 1 - Selection Sort
def selectionSort(A,N):
    c=0
    for i in range(N):
        minj = i
        j=i
        while j<N:
            if(int(A[j])<int(A[minj])):
                minj = j
            j+=1
        if(i!=minj):
            A[i],A[minj]=A[minj],A[i]
            c+=1
    return c

N=int(input())
A=input().split()
c=selectionSort(A,N)
S=""
for i in range(N-1):
    S+=A[i]+" "
S+=str(A[-1])
print(S)
print(c)

Submission status: Accepted

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

def selectSort(A, N):
 sw = 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]
   sw += 1
 return str(sw)

sw = selectSort(A, N)
print(" ".join(map(str, A)))
print(sw)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding=utf-8

def print_list(X):
    first_flag = True
    for val in X:
        if first_flag:
            print_line = str(val)
            first_flag = False
        else:
            print_line = print_line + ' ' + str(val)
    print(print_line)

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

    print_list(A)
    print(counter)

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

selection_sort(A, N)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
cnt = 0
chge = 0
for i in range(N):
    mn = A[i]
    for j in range(i+1, N):
        if mn > A[j]:
            mn = A[j]
            idx = j
            chge = 1
    if chge:
        A[idx] = A[i]
        A[i] = mn
        cnt += 1
        chge = 0

for i in range(N):
    if i == N - 1:
        print(A[i])
    else:
        print(str(A[i]), end = " ")
print(cnt)

Submission status: Accepted

Solution code:
N = int(raw_input())
lst = map(int, raw_input().split())
cnt = 0
for i in range(0, len(lst)):
    m = i
    for j in range(i, len(lst)):
        if lst[m] > lst[j]:
            m = j
    if m != i:
        lst[i], lst[m] = lst[m], lst[i]
        cnt+=1

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

Submission status: Accepted

Solution code:
raw_input()
lst=map(int, raw_input().split())

cnt = 0
for i in range(0, len(lst)):
    m = i
    for j in range(i, len(lst)):
        if lst[m] > lst[j]:
            m = j
    if m != i:
        lst[i], lst[m] = lst[m], lst[i]
        cnt+=1

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

Submission status: Accepted

Solution code:
n = int(input())
s = input().split(" ")
nums = list(map(int,s))
koukan = 0
for i in range(n):
    minj = i
    for j in range(i+1,n):
        if nums[j] < nums[minj]:
            minj = j
    w = nums[i]
    nums[i] = nums[minj]
    nums[minj] = w
    if i != minj:
        koukan += 1
for i in range(n):
    if i == n-1:
        print(nums[i])
    else:
        print(nums[i],end=" ")
print(koukan)

Submission status: Accepted

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

    A, count = selectionSort(A, N)

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

def selectionSort(A, N):
    count = 0

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

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

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

    return A, count

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 18 09:48:56 2017

@author: syaga
"""

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    num = 0
    for i in range(0, N):
        minj = i
        for j in range(i, N):
            if A[j] < A[minj]:
                minj = j
        temp = A[i]
        A[i] = A[minj]
        A[minj] = temp
        if minj != i:
            num += 1
    print(" ".join(map(str, A)))
    print(num)

Submission status: Accepted

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

c = 0

for i in range(n - 1):
    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
        c += 1

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

Submission status: Accepted

Solution code:
def selectSort(A, N):
      cnt = 0
      for i in range(0,N):
            mina = A[i]
            k = 0
            for j in range(i+1,N):
                  if int(mina) > int(A[j]):
                        mina = A[j]
                        k = j
            if int(A[i]) > int(mina):
                  swap = A[i]
                  A[i] = mina
                  A[k] = swap
                  cnt = cnt + 1
      for t in range(0,N):
            if t < N-1:
                  print(A[t], end=" ")
            else:
                  print(A[t])
      print(cnt)
            

if __name__ == "__main__":
      N = int(input())
      A = (input()).split()
      selectSort(A,N)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(A) for A in input().split()]
cnt = 0
for i in range(N):
    A_min = i
    for j in range(i, N):
        if A[A_min] > A[j]:
            A_min = j
    if i != A_min:
        t = A[i]
        A[i] = A[A_min]
        A[A_min] = t
        cnt += 1

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

print(cnt)

Submission status: Accepted

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


def main():
    N = int(input().rstrip())
    A = list(map(int, input().rstrip().split()))
    
    sw = selection_sort(A, N)
    
    print(' '.join(map(str, A)))
    print(sw)
    
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

def SelectionSort(N, 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 i != min_i:
            A[i], A[min_i] = A[min_i], A[i]
            cnt += 1
    return A, cnt
    
res = SelectionSort(N, A)
print(" ".join(map(str, res[0])))
print(res[1])

Submission status: Accepted

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



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

    A, count = selection(A, N)
    print (*A)
    print (count)

Submission status: Accepted

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

def print_list(A):
    for a in A[:-1]:
        print ("%d "%a, end="")
    print(A[-1])

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:
            v = A[i]
            A[i] = A[minj]
            A[minj] = v
            count += 1
    return A, count
        
A_sorted, c = SelectionSort(A, N)

print_list(A_sorted)
print(c)

Submission status: Accepted

Solution code:
import sys

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

def list_to_string(a):
    s = ""
    for n in a:
        s += str(n) + " "
    return s.strip()

num = int(sys.stdin.readline().strip())

array = map(lambda x: int(x), sys.stdin.readline().strip().split(" "))

swap_num = 0

for start_index in range(0, len(array)):
    min_index = start_index
    for index in range(start_index, len(array)):
        if array[min_index] > array[index]:
            min_index = index
    if min_index != start_index:
        swap(array, min_index, start_index)
        swap_num += 1
        

print list_to_string(array)
print str(swap_num)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
???????????????
?????????????????????????¨??????????????????§?????????????°????????????????????????????????????´???????????¢?????´???????????§??????

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] ?????????

"""
import math

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

def sSort(A, N):
    cnt = 0
    for i in range(0, N-1):
        minj = i
        for j in range(i,N):
            if A[j] < A[minj]:
                minj = j
        if minj != i:
            swap(A,i,minj)
            cnt += 1
    return cnt

# ?????????
N = int(input().strip())
A = list(map(int,input().strip().split()))
cnt = sSort(A,N)
print(" ".join(map(str,A)))
print(cnt)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

    a = list(A)
    ans, n = selectionSort(a, N)
    print(*ans, sep=' ')
    print(n)

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

main()

Submission status: Accepted

Solution code:
n = int(input())
data = [int(s) for s in input().split()]

change = 0
new = []
for idx in range(n):
	idx_min = data[idx:].index(min(data[idx:])) + idx
	if idx_min > idx:
		data[idx],data[idx_min] = data[idx_min],data[idx]
		change += 1

print(' '.join([str(d) for d in data]))
print(change)

Submission status: Accepted

Solution code:
def selectionSort(nums,n):
    k=0
    for i in range(n):
        minj=i
        for j in range(i,n):
            if nums[j]<nums[minj]:
                minj=j
        if nums[i]!=nums[minj]:
            nums[i],nums[minj]=nums[minj],nums[i]
            #print(nums)
            k+=1
    return nums,k
    
n=int(input())
nums=list(map(int,input().split()))

nums,k=selectionSort(nums,n)
print(*nums)
print(k)

Submission status: Accepted

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

counter=0
for i in range(n):
    minimum=i
    for j in range(i,n):
        if l[minimum]>l[j]:
            minimum=j
    if i!=minimum:
        l[minimum],l[i]=l[i],l[minimum]
        counter+=1
print(*l)
print(counter)

Submission status: Accepted

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

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


print(' '.join(map(str, k)))
print(m)

Submission status: Accepted

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

Submission status: Accepted

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

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

print count

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# Aizu Problem ALDS_1_2_C: Selection Sort
#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def printA(A):
    print(' '.join([str(a) for a in A]))

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

        
N = int(input())
A = [int(_) for _ in input().split()]
A, swaps = selection_sort(A)
printA(A)
print(swaps)

Submission status: Accepted

Solution code:
dummy = input()

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

#print(s_list)

count = 0
if len(s_list) == 1:
    for i,row in enumerate(s_list):
        if i == 0:
            print(row,end='')
        else:
            print('',row,end='')
    print()
    print(count)

else:
    for i in range(len(s_list)-1):

        min_i = i

        for j in range(i,len(s_list)):

            if s_list[j] < s_list[min_i]:
                min_i = j

        if s_list[i] != s_list[min_i]:
            count += 1
            min_num = s_list[min_i]

            s_list[min_i] = s_list[i]
            s_list[i]     = min_num

    for i,row in enumerate(s_list):
        if i == 0:
            print(row,end='')
        else:
            print('',row,end='')
    print()
    print(count)

Submission status: Accepted

Solution code:
def sort(a):
    n = len(a)
    nswaps = 0
    for i in range(n):
        minv = None
        minp = -1
        for j in range(i, n):
            if minv == None or minv > a[j]:
                minv = a[j]
                minp = j
        a[minp], a[i] = a[i], a[minp]
        if i != minp:
            nswaps += 1
    return nswaps

n = int(raw_input())
a = map(int, raw_input().split(' '))
nswaps = sort(a)
print ' '.join(map(str, a))
print nswaps

Submission status: Accepted

Solution code:
N = int(input())
A = [int(n) for n in input().split()]
cnt = 0
for i in range(N):
    min_x = A[i]
    exchange = "no"
    for j in range(i+1, N):
        if min_x > A[j]:
            min_x = A[j]
            index = j
            exchange = "ok"
    if exchange == "ok":
        A[index] = A[i]
        A[i] = min_x
        cnt += 1
for i in range(N):
    if i == N - 1:
        print(A[i])
    else:
        print(A[i], end=" ")
print(cnt)

Submission status: Accepted

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

for i in range(N):
    minj = i
    for j in range(i, N):
        if A[j] < A[minj]:
            # ????°???????index?????´??°
            minj = j

    if A[minj] != A[i]:
        cnt += 1
    # i???????????¨???
    buf = A[minj]
    A[minj] = A[i]
    A[i] = buf


print(' '.join(str(x) for x in A))
print(cnt)

Submission status: Accepted

Solution code:
def selectionSort(A, N):
    c = 0
    for i in range(N):
        minj = i
        for j in range(i, N):
            if A[j] < A[minj]:
                minj = j

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

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


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

selectionSort(L, N)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, 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 mini != i:
        count += 1
        t = a[i]
        a[i] = a[mini]
        a[mini] = t

print( *a )
print( count )

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

swapOperation = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def selsort(array):
    counter = 0
    for i in range(len(array)):
        minnum = i
        for c in range(i,len(array)):
            if array[c] < array[minnum]:
                minnum = c
        if minnum != i:
            x = array[i]
            array[i] = array[minnum]
            array[minnum] = x
            counter = counter + 1
    array.append(counter)

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

selsort(arr)
print(' '.join([str(i) for i in arr[:-1]]))
print(arr[-1])

Submission status: Accepted

Solution code:
def selections(N):
    count = 0
    for i in range(len(N)):
        mini = i
        for j in range(i+1, len(N)):
            if N[mini] > N[j]:
                mini = j
        if i != mini:
            N[mini], N[i] = N[i], N[mini]
            count += 1
    c = 1
    for i in N:
        print(i, end='')
        if c < len(N):
            print(' ', end='')
            c += 1
    print('')
    print(count)
    
n = int(input())
numbers = list(map(int, input().split()))

selections(numbers)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def selectionSort(numbers):
    n = len(numbers)
    count = 0
    for i in range(n):
        min_j = i
        for j in range(i + 1, n):
            if numbers[j] < numbers[min_j]:
                min_j = j
        if min_j != i:
            temp = numbers[i]
            numbers[i] = numbers[min_j]
            numbers[min_j] = temp
            count += 1
    return (" ".join(list(map(str, numbers))), count)


def inputInline():
    int(input())
    numbers = list(map(int, input().split(" ")))
    return numbers


result = selectionSort(inputInline())
print(result[0])
print(result[1])

Submission status: Accepted

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

def main():
	n = int(input())
	data = list(map(int, input().split()))

	c = selectionsort(data, n)
	print(*data)
	print(c)

if __name__ == "__main__":
	main()

Submission status: Accepted

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


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

count = selectionSort(a, n)

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

Submission status: Accepted

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

mini = 0
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]
    if mini != i:
        count += 1

for i in range(n):
    if i != n-1:
        print(a[i],end=" ")
    else:
        print(a[i])
print(count)

Submission status: Accepted

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


A, c = selectionSort(int(input()), list(map(int, input().split())))
print(*A)
print(c)

Submission status: Accepted

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


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

selection_sort(A, n)

Submission status: Accepted

Solution code:
def selectionSort(lst, n):
    cnt = 0
    for i in range(0, n):
        minj = i
        for j in range(i, n):
            if lst[j] < lst[minj]:
                minj = j

        if i != minj:
            tmp = lst[i]
            lst[i] = lst[minj]
            lst[minj] = tmp
            cnt += 1
    return (lst, cnt)


if __name__ == "__main__":
    n = int(input())
    lst = [int(n) for n in input().split()]
    result, cnt = selectionSort(lst, n)
    print(" ".join(str(n) for n in result))
    print(cnt)

Submission status: Accepted

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

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

A, counter = selectionSort(A, N)
print " ".join(map(str, A))
print counter

Submission status: Accepted

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


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

SelectionSort(A)

Submission status: Accepted

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

N = int(input())
A = list(map(int,input().split()))
X , cnt = selectionSort(A,N)
print(*X)
print(cnt)