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(raw_input())
a = map(int, raw_input().split())

temp = 0
cnt = 0
for i in range(n):
	flag = 0
	min = i
	for j in range(i+1,n):
		if a[min] > a[j]:
			min = j
			flag = 1
	temp = a[i]
	a[i] = a[min]
	a[min] = temp
	cnt += flag

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

print cnt

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
count = 0
for i in range(N):
    m = i
    for j in range(i, N):
        if (A[j] < A[m]):
            m = j
    if (m != i):
        A[i], A[m] = A[m], 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 = i
  for j in range(i, N):
    if a[j] < a[minj]:
      minj = j
  if a[i] != a[minj]:
    a[minj], a[i] = a[i], a[minj]
    count += 1
print(*a)
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

    return A,count

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

A,count = selectionSort(A,N)

for i in range(len(A)):  # output
    if i == len(A) - 1:
        print(A[i])
    else:
        print(A[i], end=' ')

print(count)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

count = 0

for i in range(n - 1):
    minx = i
    for j in range(i + 1,n):
        if x[j] < x[minx]:
            minx = j
        
    if not i == minx:
        x[i],x[minx]=x[minx],x[i]
        count += 1

x = list(map(str,x))
print (" ".join(x))
print(count)

Submission status: Accepted

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

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

sortedA, nSwap = selectionSort(A)
print(' '.join(map(str, sortedA)))
print(nSwap)

Submission status: Accepted

Solution code:
def main() :
    n = int(input())
    nums = [int(i) for i in input().split()]
    exchange_count = 0


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

    print(" ".join(map(str, nums)))
    print(exchange_count)

if __name__ == '__main__' :
    main()

Submission status: Accepted

Solution code:
def main() :
    n = int(input())
    nums = [int(i) for i in input().split()]
    exchange_count = 0

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

    print(" ".join(map(str, nums)))
    print(exchange_count)

if __name__ == '__main__' :
    main()

Submission status: Accepted

Solution code:
def main() :
    n = int(input())
    nums = [int(i) for i in input().split()]
    exchange_count = 0

    for i in range(n) :
        min_idx = nums[i:].index(min(nums[i:]))
        if nums[i] != nums[min_idx + i] :
            exchange_count += 1
        nums[i], nums[min_idx + i] = nums[min_idx + i], nums[i]

    print(" ".join(map(str, nums)))
    print(exchange_count)

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

print(*a)
print(c)

Submission status: Accepted

Solution code:
def selectionSort(A, N):
    s = 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:
            s += 1
    t = str(A[0])
    for i in range(1,N):
        t = t + " " + str(A[i])
    print(t)
    print(s)

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

selectionSort(a, n)

Submission status: Accepted

Solution code:
NN=raw_input()
N=raw_input()
A = [int(i) for i in N.split()]
swap=0

for i in range(int(NN)):
  mini = i
  for j in range(i,int(NN)):
    if A[j] < A[mini]:
      mini = j
  if i != mini:
    t = A[i]
    A[i] = A[mini]
    A[mini] = t
    swap+=1
      
print ' '.join([str(k) for k in A])
print swap

Submission status: Accepted

Solution code:
n = int(input())
line = input()
arr= line.split(" ")
nums = []
for c in range(n):
    nums.append(int(arr[c]))

convert_n = 0
for i in range(len(nums)):
    minj = i
    for j in range(i, len(nums)):
        if nums[j] < nums[minj]:
            minj = j
    tmp = nums[i]
    nums[i] = nums[minj]
    nums[minj] = tmp
    if i != minj:
        convert_n += 1

n_list_str = ""
for num in nums:
    n_list_str += str(num)
    n_list_str += " "
print(n_list_str.rstrip(" "))
print(convert_n)

Submission status: Accepted

Solution code:
import sys

ERROR_INPUT = 'input is invalid'


def main():
    n = get_length()
    arr = get_array(length=n)

    sourtLi, count = selectionSort(li=arr, length=n)
    print(*sourtLi)
    print(count)
    return 0


def get_length():
    n = int(input())
    if n < 0 or n > 100:
        print(ERROR_INPUT)
        sys.exit(1)
    else:
        return n


def get_array(length):
    nums = input().split(' ')
    return [str2int(string=n) for n in nums]


def str2int(string):
    n = int(string)
    if n < 0 or n > 100:
        print(ERROR_INPUT)
        sys.exit(1)
    else:
        return n


def selectionSort(li, length):
    count = 0

    for i in range(0, length - 1):
        min_index = i
        for j in range(i, length):
            if li[j] < li[min_index]:
                min_index = j

        if i != min_index:
            li[i], li[min_index] = li[min_index], li[i]
            count += 1

    return li, count


main()

Submission status: Accepted

Solution code:
def selectsort(A):
	t=0
	for i in range(0,len(A)):
		mini=i
		for j in range(i, len(A)):
			if A[j]<A[mini]:
				mini=j
		if i!=mini:
			temp=A[i]
			A[i]=A[mini]
			A[mini]=temp
			t+=1
	return t		

input()
input=input()
A=[int(s) for s in input.split(' ')]
t=selectsort(A)
print (' '.join(map(str,A)))
print(t)

Submission status: Accepted

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

def selectionSort(a, n):
    count = 0
    for i in range(n):
        mini = i
        for j in range(i, n):
            if a[j] < a[mini]:
                mini = j
        if i != mini:
            a[i],a[mini] = a[mini],a[i]
            count += 1
    print(*a)
    print(count)

selectionSort(a,n)

Submission status: Accepted

Solution code:
#coding: UTF-8

import sys
import math

class Algo:
  @staticmethod
  def insertionSort(r, n):
    count = 0
    for i in range(0,n):
      minj = i
      for j in range(i,n):
        if r[j] < r[minj]:
          minj = j
      if r[i] != r[minj]:
        r[i], r[minj] = r[minj], r[i]
        count += 1
    
    for i in range(0,n):
      if i == n-1:
        print(r[i])
      else:
        print(r[i], " ", sep="", end="")
    print(count)

N = int(input())
R= list(map(int, input().split()))
Algo.insertionSort(R, N)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
i=input
N=int(i())
A=list(map(int,i().split()))
r=range
c=0
for i in r(N):
    m=i
    for j in r(i,N):
        if A[j] < A[m]:m=j
    if i!=m:A[i],A[m]=A[m],A[i];c+=1
print(*A)
print(c)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split(' ')))
sw=0
for i in range(n-1):
    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:
        sw=sw+1
print(" ".join(map(str,a)))
print(str(sw))

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

    print(*A)
    print(count)

arr_length = int(input())
arr_num = [int(i) for i in input().split(" ")]

selectionSort(arr_num, arr_length)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

def selection_sort(x):
    count = 0
    for i in range(0,len(l)-1):
        mini = i
        for j in range(i+1,len(l)):
            if(x[j] < x[mini]):
                mini = j
        if(mini != i):
            count += 1
            temp = x[i]
            x[i] = x[mini]
            x[mini] = temp
    for data in x:
        print data,
    print
    print count


l = []
for input in sys.stdin:
    l.append(input)
l = l[1:]
l = l[0].split()
for i in range(0,len(l)):
    l[i] = int(l[i])
selection_sort(l)

Submission status: Accepted

Solution code:
def selection(l):
	N = len(l)
	count = 0

	for i in range(N):
		minj = i

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

	return l, count




if __name__ == '__main__':
	n = int(input())
	l = input().split()

	for i in range(n):
		l[i] = int(l[i])

	l, c = selection(l)
	count = 0
	leng = len(l)
	for i in l:
		count += 1
		if count < leng:
			print(i, end =' ')
		else:
			print(i)
			print(c)

Submission status: Accepted

Solution code:
def sel_sort(A, N):
    ''' 選択ソート '''
    count = 0
    for n in range(N):
        minm = n
        for m in range(n, N):
            if A[m] < A[minm]:
                minm = m
            
        if minm != n:
            A[n], A[minm] = A[minm], A[n]
            count += 1
    
    return (A, count)


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

    ans = sel_sort(A, N)

    print(' '.join([str(x) for x in ans[0]]))
    print(ans[1])

Submission status: Accepted

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

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

A, sw = selectionSort(A)

print(*A)
print(sw)

Submission status: Accepted

Solution code:
n=int(input())
s=input().split(' ')
m=0

for i in range(n):
	minj=i
	for j in range(i,n):
		if int(s[j])<int(s[minj]):
			minj=j
	if i!=minj:
		a=s[minj]
		s[minj]=s[i]
		s[i]=a
		m+=1
			
print(' '.join(s))
print(m)

Submission status: Accepted

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


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

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

    return A, count

if __name__ == '__main__':

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

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

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
        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, N)

Submission status: Accepted

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

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

print(*arr)
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

def selection_sort(data):
    count = len(data);
    o = 0;
    for i in range(count):
        minI = i;
        for j in range(i + 1, count):
            if data[j] < data[minI]:
                minI = j;
        if minI != i:
            temp = data[i];
            data[i] = data[minI];
            data[minI] = temp;
            o += 1;
    show(data);
    print(o);

def show(data):
    print(" ".join(str(n) for n in data));

selection_sort(data);

Submission status: Accepted

Solution code:
def selectionSort(A,N):
    b = 0
    for i in range(0,N):
        minj = i
        for j in range(i,N):
            if A[minj] > A[j]:
                minj = j
        A[i],A[minj] = A[minj],A[i]
        if minj != i:
            b += 1
        
    print(*A)
    print(b)

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

selectionSort(A,N)

Submission status: Accepted

Solution code:
# coding: UTF-8
N = int(input())
A = list(map(int,input().split()))
count = 0
for i in range(N):
  j=i
  minj=i
  while j<= N-1:
    if A[j] < A[minj]:
      minj = j
    j += 1
  if i != minj:
    v=A[i]
    A[i]=A[minj]
    A[minj]=v
    count += 1
    
print(" ".join(map(str,A)))
print(count)

Submission status: Accepted

Solution code:
n, data = int(input()), list(map(int, input().split()))
idx, t = 0, 0
for i in range(n-1):
    for j in range(i, n):
        if j == i:
            idx = j
        elif data[idx] > data[j]:
            idx = j
    if data[i] > data[idx]:
        data[i], data[idx] = data[idx], data[i]
        t += 1
print(" ".join(map(str, data)))
print(t)

Submission status: Accepted

Solution code:
n, data = int(input()), list(map(int, input().split()))
idx, t = 0, 0
for i in range(n-1):
    for j in range(i+1, n):
        if j == i+1:
            idx = j
        elif data[idx] > data[j]:
            idx = j
    if data[i] > data[idx]:
        data[i], data[idx] = data[idx], data[i]
        t += 1
print(" ".join(map(str, data)))
print(t)

Submission status: Accepted

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


def selectionSort(nums):
    count=0
    for i in range(len(nums)):
        minj=i
        for j in range(i,len(nums)):
            if int(nums[j])<int(nums[minj]):
                minj=j
        if i!=minj:          
            tmp=nums[minj]
            nums[minj]=nums[i]
            nums[i]=tmp
            count+=1
    print(' '.join(nums))
    print(count)


selectionSort(nums)

Submission status: Accepted

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

if __name__ == '__main__':
    length = input()
    data = [int(x) for x in input().split()]
    data_s, c = sSort(data)
    print (' '.join([str(x) for x in data_s]))
    print (c)

Submission status: Accepted

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

Submission status: Accepted

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

import sys

def selection_sort(A):
    swap_count = 0
    for i in range(len(A)):
        mini = i
        min_val = min(A[i:])
        min_index = A[i:].index(min_val) + i

        if A[min_index] < A[i]:
            swap_count += 1
            A[min_index], A[i] = A[i], A[min_index]

    return swap_count


n = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))

swap_count = selection_sort(A)
s_list = list(map(str, A))
print(' '.join(s_list))
print(swap_count)

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,n):
        if nums[j] < nums[minj]:
            minj=j
    if not i==minj:
        v=nums[i]
        nums[i]=nums[minj]
        nums[minj]=v
        count+=1
print(*nums)
print(count)

Submission status: Accepted

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


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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
#選択ソート
def selectionSort(A,N):
    #入れ替え回数カウンター
    count = 0
    for i in range(N):
        #iを基準値とする
        minj = i
        
        for j in range(i,N,1):
            #数列の中からもっとも小さい数値のindexをminjとする
            if A[j] < A[minj]:
                minj = j
                
        #入れ替え(minjとiが異なる場合)
        if minj != i:
            count = count + 1
            tmp = A[i]
            A[i] = A[minj]
            A[minj] = tmp
    print(' '.join(map(str,A)))
    print(count)
    
N = int(input())
A = list(map(int,input().split()))   
selectionSort(A,N)

Submission status: Accepted

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

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

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


print(*array)
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

    return (alist, c)


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

    (sortednums, count) = selection_sort(nums)

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


if __name__ == '__main__':
    run()

Submission status: Accepted

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

    return lst,count

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

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

Submission status: Accepted

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

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

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

selection_sort(A, N)

Submission status: Accepted

Solution code:
# -*- coding: UTF-8 -*-
#Selection Sort

#標準入力から
N = int(input()) #要素数
A = list(map(int,input().split())) #ソート対象（スペース区切り→配列に展開）
#交換回数
cnt = 0 

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

#回答
print(' '.join(map(str,A)))
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#! python3
# selection_sort.py

# 各ステップで最小値を選択

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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個の整数が空白区切り
selectionSort(A,N)

Submission status: Accepted

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

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

Submission status: Accepted

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

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


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

    if N == 1:
        break

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

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

    flag = 0


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

print(count)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

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