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: Wrong Answer

Solution code:
def selectionSort(A, N): # N個の要素を含む0-オリジンの配列A
    A = list(map(int, A.split(" ")))
    change_count = 0
    for i in range(N):
        minj = i
        for j in range(i,N):
            if A[j] < A[minj]:
                minj = j
        if i != minj:
            #A[i] と A[minj] を交換
            tmp = A[i]
            A[i] = A[minj]
            A[minj] = tmp
            change_count+=1
    
    # 回答
    print(" ".join(map(str,A)))
    print(change_count)
    #return True

# 入力
N = int(input("数列の長さを表す整数を入力して下さい  "))  # 数列の長さを表す整数（N）
A = input("N 個の整数が空白区切りで入力して下さい  ")   # N個の整数が空白区切り
selectionSort(A,N)

Submission status: Wrong Answer

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

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

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

print(count)

Submission status: Wrong Answer

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

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


count = 0
for i in range(N):

    if N == 1:
        break

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

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

print(count)

Submission status: Wrong Answer

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

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


count = 0
for i in range(N):

    if N == 1:
        break

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


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

print(count)

Submission status: Wrong Answer

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

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


count = 0
for i in range(N):

    if N == 1:
        break

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


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

print(count)

Submission status: Wrong Answer

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)
    for i in a:
        if i != a[-1]:
            print(i,end=" ")
        else:
            print(i)
    print(count)

Submission status: Wrong Answer

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
            a[i],a[minij] = a[minij],a[i]
    count+=1
for i in range(n):
    print("{0} ".format(a[i]),end="")
print()

print(count)

Submission status: Wrong Answer

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
            a[i],a[minij] = a[minij],a[i]
            count+=1
for i in range(n):
    if i != n-1:
        print("{0} ".format(a[i]),end="")
    else:
        print("{0}".format(a[i]))


print(count)

Submission status: Wrong Answer

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
            a[i],a[minij] = a[minij],a[i]
            count+=1


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

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
# リストの宣言
A = []

# 入力の最初の行に、数列の長さを表す整数 N が与えられます。
# 2 行目に、N 個の整数が空白区切りで与えられます。
LENGTH =int(input())
A = input().split()

# 変数
i = 0
j = 1
CHANGE = 0

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

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

Submission status: Wrong Answer

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
            count += 1
    numbers[i], numbers[minj] = numbers[minj], numbers[i]

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

Submission status: Wrong Answer

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 - 1):
        if arr[j] < arr[minj]:
            minj = j
        arr[i], arr[minj] = arr[minj], arr[i]
        swap_cnt += 1 if i != minj else 0

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

Submission status: Wrong Answer

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 - 1):
        if arr[j] < arr[minj]:
            minj = j
        arr[i], arr[minj] = arr[minj], arr[i]
        swap_cnt += 1 if i != minj else 0

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

Submission status: Wrong Answer

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 - 1):
        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: Wrong Answer

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 - 1):
        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: Wrong Answer

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 - 1):
        if arr[j] < arr[minj]:
            minj = j
        arr[i], arr[minj] = arr[minj], arr[i]
        swap_cnt += 1

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

Submission status: Wrong Answer

Solution code:
# リストの宣言
A = []

# 入力の最初の行に、数列の長さを表す整数 N が与えられます。
# 2 行目に、N 個の整数が空白区切りで与えられます。
LENGTH =int(input())
A = input().split()

# 変数
i = 0
CHANGE_COUNT = 0

while i <= LENGTH -1:
    print(" ".join(map(str, A)))
    print(CHANGE_COUNT)
    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: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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+1,n-1):
        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: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=0
for i in range(n-1):
    minj=i
    for j in range(i+1,n-1):
        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: Wrong Answer

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
    for j in xrange(i, len(n)):
        if n[j] < n[min]:
            min=j
            flag = True
    n[i],n[min] = n[min],n[i]
    cnt+=1

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

Submission status: Wrong Answer

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

#リストの一番手前からみる。
for i in range(len(n)):
#いま見てる番目を記憶
    min=i
#フラグ判定を0で初期化
    flag=0
#リストの二番目から最後は一つ手前まで
    for j in range(1,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: Wrong Answer

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

#リストの一番手前からみる。
for i in range(len(n)):
#いま見てる番目を記憶
    min=i
#フラグ判定を0で初期化
    flag=0
#リストの二番目から最後は一つ手前まで
    for j in range(1,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: Wrong Answer

Solution code:
n = input()
A = map(int, raw_input().split())
count = 0
for i in xrange(n):
    mini = min(A[i:])
    if i < A.index(mini)-1:
        A.insert(A.index(mini),A.pop(i))
        A.pop(A.index(mini))
        A.insert(i,mini)
        count += 1
print ' '.join(map(str, A))
print count

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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
    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: Wrong Answer

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:
n = int(input())
s = [int(x) for x in input().split()]

swap_cnt = 0

for i in range(n) :
    cmin = i
    for j in range(i + 1, n) :
        if s[j] < s[cmin] :
            cmin = j
            
    if i != cmin :
        s[i], s[cmin] = s[cmin], s[i]
        swap_cnt += 1
                
print(" ".join(map(str, s)))
print(swap_cnt)

Submission status: Accepted

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


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

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

Submission status: Accepted

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

count = 0
for i in range(len(a)):
    minj = i
    for j in range(i+1, len(a)):
        #print('taiget {} in list {}'.format(a[i], a[j:]))
        if a[j] < a[minj]:
            minj = j
    #print('change[{}] to [{}]'.format(a[i], a[minj]))
    if a[i] != a[minj]:
        a[i], a[minj] = a[minj], a[i]
        count += 1

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

Submission status: Accepted

Solution code:
def print_list(A):
    print(*A, sep=" ")

def swap(a, b):
    return b, a

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

def selection_sort(A, n):
    replace_num = 0
    for i in range(0, n):
        minj = find_minj(A, i, n)
        if A[i] > A[minj]:
            A[i], A[minj] = swap(A[i], A[minj])
            replace_num += 1
    print_list(A)
    print(replace_num)

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

selection_sort(A, n)

Submission status: Accepted

Solution code:
# Selection Sort

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

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

for i in range(length):
    if i < length - 1:
        print(array[i], end = " ")
    else:
        print(array[i])
print(count)

Submission status: Accepted

Solution code:
def selection_sort(a, n):
    sw_num = 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
            sw_num += 1

    return sw_num


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

sw_num = selection_sort(a, n)

print(' '.join([str(i) for i in a]))
print(sw_num)

Submission status: Accepted

Solution code:
def selectionSort(a, n):
  count = 0
  for i in range(0, n):
    minj = i
    for j in range(i, n):
      if a[j] < a[minj]:
        minj = j
    if i != minj:
      w = a[i]
      a[i] = a[minj]
      a[minj] = w
      count += 1
  print(' '.join(map(str, a)))
  print(count)

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

Submission status: Accepted

Solution code:
# inputList = list(map(int, '''6
# 5 6 4 2 1 3'''.split()))
# N = inputList.pop(0)


def formatting(nums):
    for i in range(len(nums)):
        if i != len(nums) - 1:
            print(nums[i], end=' ')
        else:
            print(nums[i])


def selectionSort(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

    formatting(A)
    print(count)



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

selectionSort(inputList, N)

Submission status: Accepted

Solution code:
n = int(input())
s = [ i for i in input().split()]
cnt = 0
for i in range(n-1):
    minj = i
    for j in range(i+1,n):
        if int(s[j]) < int(s[minj]):
            minj = j
    if i != minj:
        cnt += 1
        s[i],s[minj] = s[minj],s[i]
print(' '.join(s))
print(cnt)

Submission status: Accepted

Solution code:
def insection_sort(lst):
    lens=len(lst)
    ans=0
    for i in xrange(lens):
        mn=i
        for j in xrange(i+1,lens):
            if(lst[j]<lst[mn]):
                mn=j
        if(mn!=i):
            tmp=lst[i]
            lst[i]=lst[mn]
            lst[mn]=tmp
            ans+=1
    return ans
num=int(raw_input().strip())
arr=map(int,raw_input().strip().split())
ans=insection_sort(arr)
print " ".join(str(pp) for pp in arr)
print ans

Submission status: Accepted

Solution code:
def selection_sort(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 i != minj:
            cnt += 1
    return cnt

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

Submission status: Accepted

Solution code:
n=int(input())
num=list(map(int,input().split()))
count=0
for i in range(0,n):
	minj=i
	a=0
	for j in range(i+1,n):
		if num[minj]>num[j]:
			minj=j
	if minj!=i:
		count+=1
	a=num[minj]
	num[minj]=num[i]
	num[i]=a
print(' '.join(list(map(str,num))))
print(count)

Submission status: Accepted

Solution code:
def selection_sort(num_list):
    list_length = len(num_list)
    cnt = 0
    
    for i in range(list_length):
        min_element = i
        for j in range(i+1,list_length):
            if num_list[j] < num_list[min_element]:
                min_element = j
        if i != min_element:
            num_list[i],num_list[min_element] = num_list[min_element],num_list[i]
            cnt += 1
    return num_list,cnt

n = int(input())
num_list = list(map(int,input().split()))
ans_list,cnt = selection_sort(num_list)
str_ans_list = [str(i) for i in ans_list]
print('{}\n{}'.format(' '.join(str_ans_list),cnt))

Submission status: Accepted

Solution code:
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 minj != i:
        a[i], a[minj] = a[minj], a[i]
        counter += 1
print(*a)
print(counter)

Submission status: Accepted

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

    cnt = 0
    for i in range(n):
        min_idx = i
        for j in range(i+1, n):
            if array[min_idx] > array[j]:
                min_idx = j
        if min_idx != i:
            tmp = array[i]
            array[i] = array[min_idx]
            array[min_idx] = tmp
            cnt += 1

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


main()

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:
def showList(A, N):
    for i in range(N-1):
        print(A[i],end=' ')
    print(A[N-1])

def selectionSort(A, N):
    """
    選択ソート
    O(n^2)のアルゴリズム
    """
    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:
            t = A[minj]
            A[minj] = A[i]
            A[i] = t
            count += 1
    showList(A, N)
    print(count)
    

N = int(input())
A = [int(x) for x in input().split(' ')]
selectionSort(A, N)

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
        else:
            pass
    A[i], A[minj] = A[minj], A[i]
    if i != minj:
        count += 1
        
print(*A)
print(count)

Submission status: Accepted

Solution code:
def selectionSort(A):
    n = len(A)
    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:
            A[i], A[minj] = A[minj], A[i]
            cnt += 1
    print(*A)
    print(cnt)

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
array = list(map(int, input().split()))
cnt = 0 
for i in range(N):
	minij = i 
	for j in range(i, N):
		if array[j] < array[minij]:
			minij = j 
	if minij != i:
		array[i], array[minij] = array[minij], array[i]
		cnt += 1

print(' '.join(map(str, array)))
print( "%d" % (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 a[i] != a[minj]:
        cnt += 1
    tmp = a[i]
    a[i] = a[minj]
    a[minj] = tmp

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

Submission status: Accepted

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

swap_count = 0
for i in range(n):
    flag = False
    minj = i
    for j in range(i, n):
        if a[j] < a[minj]:
            minj = j
            flag = True
    if flag:
        a[i], a[minj] = a[minj], a[i]
        swap_count += 1

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

Submission status: Accepted

Solution code:
from typing import List


def selection_sort(A: List[int]) -> int:
    cnt = 0
    for i in range(len(A)):
        min_j = i
        for j in range(i, len(A)):
            if A[j] < A[min_j]:
                min_j = j
        if min_j != i:
            cnt += 1
        A[i], A[min_j] = A[min_j], A[i]
    return cnt


N = int(input())
A = [int(i) for i in input().split()]
cnt = selection_sort(A)
print(' '.join(map(str, A)))
print(cnt)

Submission status: Accepted

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


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

A, cnt = selection_sort(A, N)
string = list(map(str, A))
print(' '.join(string))
print(cnt)

Submission status: Accepted

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

count = 0
for i in range(n):
    mini = i + a[i:].index(min(a[i:]))
    if mini != i:
        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())
l=list(map(int,input().split()))

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

Submission status: Accepted

Solution code:
def selectionsort(l,n):
    c=0
    for i in range(n):
        minj=i
        for j in range(i,n):
            if int(l[j])<int(l[minj]):
               minj=j
        l[i],l[minj]=l[minj],l[i]
        if i!=minj:
          c+=1
    print(" ".join(l))
    print(c)
n=int(input())
l=list(input().split())
selectionsort(l,n)

Submission status: Accepted

Solution code:
# -*- coding: utf 8 -*-
# Selection Sort
# 2019. 4/22

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

    return A, sw

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

    A_sorted, sw = selectionSort(A, N)

    for i in range(0, N - 1):
        print(A[i], end=" ")
    print(A[N - 1])
    print(sw)

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = 0
for i in range(0, n):
	minj = min(range(i, n), key=lambda j: a[j])
	if i != minj:
		a[i], a[minj] = a[minj], a[i]
		count += 1
print(*a)
print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
count = 0
for i in range(n):
	minj = min(range(i, n), key=lambda j: a[j])
	if i != minj:
		a[i], a[minj] = a[minj], a[i]
		count += 1
print(*a)
print(count)

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
cnt=0
for i in range(n):
  minp=i
  for j in range(i,n):
    if arr[minp]>arr[j]:
      minp=j
  arr[minp],arr[i]=arr[i],arr[minp]
  if minp!=i:
    cnt+=1
print(' '.join(map(str,arr)))
print(cnt)

Submission status: Accepted

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

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

print(*a)
print(count)

Submission status: Accepted

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

chg = 0
for i in range(num-1):

	minv = i+1
	for j in range(i+2,num):
		if A[minv] > A[j]:
			minv = j

	if A[i] > A[minv]:
		A[i],A[minv] = A[minv],A[i]
		chg += 1
print(*A)
print(chg)

Submission status: Accepted

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

chg = 0
for i in range(num-1):

	minv = i
	for j in range(i+1,num):
		if A[minv] > A[j]:
			minv = j

	if A[i] > A[minv]:
		A[i],A[minv] = A[minv],A[i]
		chg += 1
print(*A)
print(chg)

Submission status: Accepted

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

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):
#    for j in range(i, n):
        if nums[j] < nums[minj]:
            minj = j
#            count += 1
    else:
        if nums[i] > nums[minj]:
            temp = nums[minj]
            nums[minj] = nums[i]
            nums[i] = temp
            count += 1

print(' '.join([str(i) for i in nums]))
print(count)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0

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

    print(*a)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def selection_sort(A):
    count = 0
    for i in range(len(A)):
        min_value = A[i]
        min_value_index = i
        # print('- i:', i, 'A[i]', A[i], '-')
        for j in range(i, len(A)):
            # print('j:', j, 'A[j]:', A[j])
            
            if A[j] < min_value:
                min_value = A[j]
                min_value_index = j
        # print('min_value', min_value, 'min_value_index', min_value_index)
        if i != min_value_index:
            count += 1
            A[i], A[min_value_index] = A[min_value_index], A[i]
            # print('swap!', A)
        
    return count

n = int(input())
A = list(map(int, input().split()))
count = selection_sort(A)
print(*A)
print(count)

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 i != minj:
            count +=1
    return A,count

N = int(input())
A = list(map(int,input().split()))
A,count = selectionSort(A,N)
print(" ".join(map(str,A)))
print("%d"%(count))

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, 1):
        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)