Problem:


Selection Sort

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

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

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

Output

The output consists of 2 lines. 

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

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

Constraints

1 ≤ N ≤ 100

Sample Input 1

6
5 6 4 2 1 3

Sample Output 1

1 2 3 4 5 6
4

Sample Input 2

6
5 2 4 6 1 3

Sample Output 2

1 2 3 4 5 6
3



Sample input:
6
5 6 4 2 1 3


Sample output:
1 2 3 4 5 6
4

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ALDS_2_B.
# バブルソート。
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):
    # 選択ソート。その番号以降の最小値を順繰りにもってくる。
    # 交換回数は少ないが比較回数の関係で結局O(n^2)かかる。
    count = 0
    for i in range(len(a)):
        # a[i]からa[len(a) - 1]のうちa[j]が最小っていうjを取る。
        # このときiとjが違うならカウントする。
        # というかa[i]とa[j]が違う時カウントでしたね。
        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()

Submission status: Accepted

Solution code:
#coding:utf-8
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
#-*- coding: utf-8 -*-

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 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]

# リストの宣言
A = []

# 入力の最初の行に、数列の長さを表す整数 N が与えられます。
# 2 行目に、N 個の整数が空白区切りで与えられます。
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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)  # A[i] と A[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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env 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)

Submission status: Accepted

Solution code:
#!/usr/bin/python
i = input()
n = map(int, raw_input().split())
cnt=0

for i in xrange(len(n)):
    min = i
#    flag = False
    flag = 0
    for j in xrange(i, len(n)):
        if n[j] < n[min]:
            min=j
#            flag = True
            flag = 1
    n[i],n[min] = n[min],n[i]
    cnt+=flag

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
i = input()
n = map(int, raw_input().split())
cnt=0

#リストの一番手前からみる。
for i in xrange(len(n)):
#いま見てる番目を記憶
    min=i
#フラグ判定を0で初期化
    flag=0
#リストの二番目から最後は一つ手前まで
    for j in xrange(i,len(n)):
        if n[j]<n[min]:
#より小さい値の順番目を記憶する。
#このifの中に入っていると入れ替え処理候補が有ったことになるのでフラグを立てる。
            min=j
            flag=1
#当初の比較のために取り出してきた値と発見した最小の値を入れ替える。
    n[i],n[min]=n[min],n[i]
    cnt+=flag
print ' '.join(map(str,n))
print cnt

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().strip().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:
        count += 1
        a[i], a[mini] = a[mini], a[i]
print(' '.join(map(str, a)))
print(count)

Submission status: Accepted

Solution code:
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 minj != i:
		k = a[i]
		a[i] = a[minj]
		a[minj] = k
		counter += 1
ans = list(map(str,a))
print(" ".join(ans))
print(counter)

Submission status: Accepted

Solution code:
#選択ソート
number=eval(input())
swap=0
arr=list(map(int, input().split(' ')))
for i in range(number-1):
    minI=arr.index(min(arr[i:]),i)
    if i!=minI:
        arr[i],arr[minI]=arr[minI],arr[i]
        swap+=1
print(' '.join(map(str, arr)))
print(swap)

Submission status: Accepted

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

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

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

for k in range(n-1):
    print(str(A[k])+" ",end = "")
print(str(A[n-1]))
print(count)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(n-1):
  minj=i
  for j in range(i+1,n):
    if a[j]<a[minj]:
      minj=j
  a[i],a[minj]=a[minj],a[i]
  if minj!=i:
    ans+=1

print(*a)
print(ans)

Submission status: Accepted

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


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

    answer_list, answer_count = selectionSort(A, N)

    print(*answer_list)
    print(answer_count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
count = 0
for i in range(n):
    min_value = i
    for j in range(i,n):
        if l[min_value] > l[j]:
            min_value = j
    t = l[i]
    r = l[min_value]
    if l[i] > l[min_value]:
        l[i] = r
        l[min_value] = t
        count +=1
for s in range(n-1):
        print(l[s],end=' ')
print(l[-1])
print(count)

Submission status: Accepted

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

def selectionSort(a:list, n:int):
    count = 0
    for i in range(n):
        flag = 0
        minj = i
        for j in range(i, n):
            if a[j] < a[minj]:
                minj = j
                flag = 1
        if flag:
            a[i], a[minj] = a[minj], a[i]
            count += 1
    return a, count

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(N):
	ptr = i
	for j in range(i + 1, N):
		if A[ptr] > A[j]:
			ptr = j
	if i != ptr:
		x = A[i]
		A[i] = A[ptr]
		A[ptr] = x
		ans += 1
s = str(A[0])
for i in range(1, N):
	s += " " + str(A[i])
print(s)
print(ans)

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
A = LI()
cnt = 0
#selectionSort(A, N)
for i in range(N):
    minj = i
    for j in range(i, N):
        if A[j] < A[minj]:
            minj = j
    a = A[i]
    b = A[minj]
    A[i] = b
    A[minj] = a
    if minj != i:
        cnt += 1
print(' '.join(map(str, A)))
print(cnt)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
A = list(map(int, input().split()))
count = 0
for i in range(N):
    min = i
    for j in range(i, N):
        if A[min] > A[j]:
            min = j
    A[i], A[min] = A[min], A[i]
    if min != i:
        count += 1


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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def selection_sort(a, n):
    switch = 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
            switch += 1
    print(' '.join(map(str, a)))
    print(switch)


def main():
    n = int(input())
    nums = input()
    nums = list(map(int, nums.split(' ')))
    selection_sort(nums, n)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

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
    return A,count

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

lst,change = selectionSort(lst)
print(" ".join(map(str,lst)))
print(change)

Submission status: Accepted

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

count = 0
for i in range(N):
    mini = min(S[i:N])
    for j in range(i,N):
        if S[j]==mini and i!=j and S[i]!=S[j]:
            S[i], S[j] = S[j], S[i]
            count += 1

for i in range(N):
    if i==N-1:
        print(S[i])
    else:
        print(S[i],end=' ')
print(count)

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int, input().split()))
cnt = 0
for i in range(len(L)-1):
    x = L[i+1:].index(min(L[i+1:])) + i + 1
    if L[i] > L[x]:
        L[i], L[x] = L[x], L[i]
        cnt += 1
print(' '.join(str(x) for x in L))
print(cnt)

Submission status: Accepted

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

cnt = 0

# selectionSort

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

    A[i],A[minj] = A[minj],A[i]

    if i != minj:
        cnt+=1
    

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

Submission status: Accepted

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

num_of_sorting = 0
for i in range(0, N-1):
  minj = i
  for j in range(i+1, N):
    if A[j] < A[minj]:
      minj = j
  if i is not minj:
    temp = A[i]
    A[i] = A[minj]
    A[minj] = temp
    num_of_sorting += 1
  
print(' '.join(map(str, A)))
print(f"{num_of_sorting}")

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def show(A, n):
    for i in range(n):
        if i != n-1:
            print(A[i], end=' ')
        else:
            print(A[i])


def selection_sort(A, n):
    count = 0
    for i in range(n-1):
        min_j = i
        for j in range(i+1, n):
            if A[min_j] > A[j]:
                min_j = j
        if min_j != i:
            A[i], A[min_j] = A[min_j], A[i]
            count += 1
    show(A, n)
    print(count)


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


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):
  min_j = i
  flag = False

  for j in range(i+1, N):
    if (A[min_j] > A[j]):
      min_j = j
      flag = True
  
  #print("min_j:{} j:{}".format(min_j, j))
  A[min_j], A[i] = A[i], A[min_j]

  if flag == True:
    count += 1

print(str(A).replace('[', '').replace(']', '').replace(',', ''))
print(count)

Submission status: Accepted

Solution code:
import sys

def print_data(data):
    last_index = len(data) - 1
    
    for idx, item in enumerate(data):
        print(item, end='')
        if idx != last_index:
            print(' ', end='')

    print('')
        
def main():
    N = int(input())
    data = list(map(int, input().split()))
    counter = 0
    
    for i in range(N):
        mini = i
        for j in range(i, N):
            if data[j] < data[mini]:
                mini = j
        if i == mini:
            pass
        else:
            hoge = data[mini]
            data[mini] = data[i]
            data[i] = hoge
            counter += 1
                  
        
    print_data(data)
    print(counter)
    
            
if __name__ == '__main__':
    main()

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