Problem:


Selection Sort

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

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

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

Output

The output consists of 2 lines. 

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

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

Constraints

1 ≤ N ≤ 100

Sample Input 1

6
5 6 4 2 1 3

Sample Output 1

1 2 3 4 5 6
4

Sample Input 2

6
5 2 4 6 1 3

Sample Output 2

1 2 3 4 5 6
3



Sample input:
6
5 6 4 2 1 3


Sample output:
1 2 3 4 5 6
4

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def selection_sort(A):
    n = len(A)
    num_swaps = 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]
            num_swaps += 1
    return num_swaps


N = int(input())
A = [int(elem) for elem in input().split()]
num_swaps = selection_sort(A)
print(*A)
print(num_swaps)

Submission status: Accepted

Solution code:
#selection sort
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 i != minj:
        temp = nums[i] #iの値を一時保管
        nums[i] = nums[minj] #iの位置に最小値を入れて交換する
        nums[minj] = temp
        count = count + 1

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

Submission status: Accepted

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

Submission status: Accepted

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

c = 0
for i in range(n):
    mini = i
    for j in range(i, n):
        if a[j] < a[mini]:
            mini = j
    if a[i] != a[mini]:
        a[i], a[mini] = a[mini], a[i]
        c += 1

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

Submission status: Accepted

Solution code:
N = input()
n = map(int,raw_input().split(' '))
c = 0
for i in range(N-1):
	mini = i
	for j in range(i+1,N):
		if n[mini] > n[j]:
			mini = j
	if mini != i:
		temp = n[i]
		n[i] = n[mini]
		n[mini] = temp
		c+=1
print " ".join(str(i) for i in n)
print c

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = raw_input()
A = map(int, raw_input().split())
cnt = 0
for i in xrange(len(A)):
    mini = i
    for j in xrange(i, len(A)):
        if A[j] < A[mini]:
            mini = j
    if A[i]!=A[mini]:
	    cnt += 1
	    A[i], A[mini] = A[mini], A[i]
for x in A:
	print x,
print
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
    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)

Submission status: Accepted

Solution code:
def selectionSort(A, N): 
    m = 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:
            m += 1
    print ' '.join(map(str,A))
    print m

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

Submission status: Accepted

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


def swap(A, i, j):
    if i != j:
        tmp = A[j]
        A[j] = A[i]
        A[i] = tmp
        return 1
    else:
        return 0


def print_array(array):
    print(" ".join([str(x) for x in array]))


amount = int(input())
numbers = [0 for i in range(amount)]
numbers = list(map(int, input().split()))
count = selection_sort(numbers)
print(count)

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_B
# Selection Sort
# Result:
import sys

nums_len = int(sys.stdin.readline())
nums_str = sys.stdin.readline().split(' ')
nums = map(int, nums_str)

swap_count = 0
for i in range(0, nums_len):
    minj = i
    for j in range(i, nums_len):
        if nums[j] < nums[minj]:
            minj = j
    if minj != i:
        v = nums[i]
        nums[i] = nums[minj]
        nums[minj] = v
        swap_count += 1

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

Submission status: Accepted

Solution code:
def selectionSort( nums ):
	cnt = 0
	for i in range( 0, len( nums ) ):
		mini = i
		for j in range( i , len( nums ) ):
			if nums[j] < nums[ mini ]:
				mini = j
		if mini != i:
			cnt += 1
			nums[i], nums[ mini ] = nums[ mini ], nums[i]

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


n = int( raw_input( ) )
nums = [ int( val ) for val in raw_input( ).split( " " ) ]
selectionSort( nums )

Submission status: Accepted

Solution code:
def selectionSort( nums ):
	cnt = 0
	for i in range( 0, len( nums ) ):
		mini = i
		for j in range( i , len( nums ) ):
			if nums[j] < nums[ mini ]:
				mini = j
		if mini != i:
			cnt += 1
			nums[i], nums[ mini ] = nums[ mini ], nums[i]

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


n = int( input( ) )
nums = [ int( val ) for val in input( ).split( " " ) ]
selectionSort( nums )

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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(' '.join((str(x) for x in A)))
print(c)

Submission status: Accepted

Solution code:
#coding:utf-8
n = int(input())
numbers = list(map(int, input().split()))

def selectionSort(ary):
    count = 0
    for i in range(len(ary)):
        minj = i
        for j in range(i, len(ary)):
            if ary[minj] > ary[j]:
                minj = j
        if minj != i:
            ary[minj], ary[i] = ary[i], ary[minj]
            count += 1
    return (ary, count)

result = selectionSort(numbers)
print(*result[0])
print(result[1])

Submission status: Accepted

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

if __name__ == '__main__':
    n = int(input())
    array, count = selection_sort(list(map(int, input().split())))
    print(' '.join(map(str, array)))
    print(count)

Submission status: Accepted

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

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

print(*a)
print(count)

Submission status: Accepted

Solution code:
# coding: utf-8

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

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

if __name__ == '__main__':
	main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#coding:utf-8
#1_2_B
def selection_sort(ary, n):
    count = 0
    for i in range(n):
        minj = i
        for j in range(i, n):
            if ary[minj] > ary[j]:
                minj = j
        if ary[minj] != ary[i]:
            ary[minj], ary[i] = ary[i], ary[minj]
            count += 1
    print(*ary)
    print(count)

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

Submission status: Accepted

Solution code:
def selection_sort(a,n):
    m=0
    for i in range(0,n):
        minj=i
        for j in range(i,n):
            if(a[minj] > a[j]):
                minj=j
        if(minj!=i):
            m+=1
            #print("swap(%d<->%d)"%(a[ninj],a[i]))
            a[minj],a[i] = a[i],a[minj]
        
    b=list(map(str,a))
    print(" ".join(b))
    print(m)

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
A = list(map(int,s.split()))
flag = 1
cnt = 0
#####################################

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

#####################################
for k in range(0,len(A)-1):
    print(A[k],end=" ")
print(A[len(A)-1])
print (cnt)

Submission status: Accepted

Solution code:
n = int(raw_input())

s = raw_input()

count = 0

num_list= s.split(" ")
for i in range(n):
    num_list[i] = int(num_list[i])

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

print " ".join([str(a) for a in num_list])
print count

Submission status: Accepted

Solution code:
N = int(raw_input())
A = map(int, raw_input().split())
count = 0
for i in range(0, N):
    min_j = i
    c = 0
    for j in range(i, N):
        if A[j] < A[min_j]:
            min_j = j
            c = 1
    A[i], A[min_j] = A[min_j], A[i]
    count += c
print " ".join(map(str, A))
print count

Submission status: Accepted

Solution code:

numbers = []
n = int(raw_input())
s = raw_input()
count = 0

numbers = s.split(" ")
for i in range(int(n)):
	numbers[i] = int(numbers[i])

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

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

Submission status: Accepted

Solution code:
N = int(raw_input())
num_list = map(int, raw_input().split())
c = 0

for i in range(0,N,1):
    flag =0
    minj =i
    for j in range(i,N,1):
        if num_list[j] < num_list[minj]:
            minj = j
            flag = 1
    c += flag
    num_list[i], num_list[minj] = num_list[minj], num_list[i]
print " ".join(map(str,num_list))
print c

Submission status: Accepted

Solution code:
import sys

lines = sys.stdin.readlines()

N = lines[0]
A = lines[1].strip().split(" ")

count = 0
for i in range(len(A)):
    minj = i
    for j in range(i, len(A)):
        if int(A[j]) < int(A[minj]):
            minj = j
    if i != minj:
        a = A[i]
        b = A[minj]
        A[i] = b
        A[minj] = a
        count += 1

print " ".join(A)
print count

Submission status: Accepted

Solution code:
N = int(raw_input())
A = raw_input().split(" ")

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

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

print " ".join(A)
print count

Submission status: Accepted

Solution code:
import sys

def selectionSort(x_list, y):
  a = 0
  for i in range(y):
    minj = i
    for j in range(i, y):
      if x_list[j] < x_list[minj]:
        minj = j
    x_list[i], x_list[minj] = x_list[minj], x_list[i]
    if x_list[i] != x_list[minj]:
      a += 1
  return a

y = sys.stdin.readline()
y = int(y)
 
x = sys.stdin.readline()
x_list = x.split(" ")
 
for i in range(y):
  x_list[i] = int(x_list[i])
 
a = selectionSort(x_list, y)
 
for k in range(0, y):
  print x_list[k],
print
print a

Submission status: Accepted

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

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

count = 0 
count, num_list = selection_sort(num_list, count)
num_list = map(str, num_list)
print " ".join(num_list)
print count

Submission status: Accepted

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

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

count = 0 
count, num_list = selection_sort(num_list, count)
num_list = map(str, num_list)
print " ".join(num_list)
print count

Submission status: Accepted

Solution code:
def input_number():
    num = raw_input()
    array = num.strip().split(" ")
    int_array = map(int, array)

    return int_array


def main():
    n = raw_input()
    count = 0
    int_array = input_number()
    for i in range(int(n)):
        minj = i
        for j in range(i, int(n)):
            if int_array[j] < int_array[minj]:
                minj = j
        v = int_array[i]
        int_array[i] = int_array[minj]
        int_array[minj] = v
        if minj != i:
            count += 1
    array = map(str, int_array)
    print " ".join(array)
    print count


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 - 1):
    min_i = i
    for j in range(i + 1, n):
        if A[min_i] > A[j]:
            min_i = j
    if min_i != i:
        A[min_i], A[i] = A[i], A[min_i]
        cnt += 1

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

Submission status: Accepted

Solution code:
def selection_sort(List):
	cnt = 0
	l=len(List)
	for i in range(l):
		num_index=i
		for j in range(i,l):
			if List[num_index]>List[j]:
				num_index=j
		if num_index != i:
			temp=List[num_index]
			List[num_index]=List[i]
			List[i]=temp
			cnt+=1
	return cnt
N=input()
N_List=map(int,raw_input().split())
cnt=selection_sort(N_List)
print(" ".join(map(str,N_List)))
print(cnt)

Submission status: Accepted

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


def print_array(a):
    print(" ".join(map(str, a)))


def selectionsort(arr, n):
    a = arr[:]
    s = 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])
            s += 1
    return (a, s)


def main():
    n = int(input())
    arr = list(map(int, input().split()))
    (a, s) = selectionsort(arr, n)
    print_array(a)
    print(s)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# encode: utf-8

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

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

main()

Submission status: Accepted

Solution code:
def selectionSort(nums, length):
  count = 0
  for i in xrange(length):
    minj = i
    for j in xrange(i, length):
      # print "i = %d, j = %d, minj = %d, count = %d" % (i, j, minj, count)
      # print nums
      if nums[j] < nums[minj]:
        minj = j
    nums[i], nums[minj] = nums[minj], nums[i]
    count += 1 if i != minj else 0
  return [nums, count]

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

def Selection_Sort(A,n):
    count = 0
    for i in range(n):
        mini = i
        for j in range(i,n): #i以上の要素において最小のAをminiに格納
            if A[j] < A[mini]:
                mini = j
        if A[i] != A[mini]:
            A[i], A[mini] = A[mini], A[i]
            count += 1
    return A, count

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(*a)
print(c)

Submission status: Accepted

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

    return a, c

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

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

Submission status: Accepted

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

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

selectionSort(A, N)

Submission status: Accepted

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

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]
        count += 1 if i != minj else  0
    return count

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

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

Submission status: Accepted

Solution code:
#coding: UTF-8

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


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

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

InsertionSort(A,N)

Submission status: Accepted

Solution code:
def selectionSort(A, N):
	global cnt

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

	return A

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

Submission status: Accepted

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

N = int(raw_input())
A = map(int, raw_input().split())
count = 0
for i in range(N):
    minj = i
    for j in range(i, N):
        if A[j] < A[minj]:
            minj = j
    if i != minj:
        A[i], A[minj] = A[minj], A[i]
        count += 1
print " ".join(map(str, A))
print count

Submission status: Accepted

Solution code:
import math

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

count = 0

for i in range(0, N):
    minj = i
    for j in range(i, N):
        if A[j] < A[minj]:
            minj = j
    if i != minj:
        count += 1
        temp = A[i]
        A[i] = A[minj]
        A[minj] = temp
    
print(' '.join(map(str, A)))
print(count)

Submission status: Accepted

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

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

n = input()
arr = map(int, raw_input().split())
cnt = SelectionSort(arr, n)
printArray(arr, n) 
print cnt

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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


class SegmentTree(object):

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

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

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

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


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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
a = [int(x) for x in input().split()]
def selectionSort(A, N):
	count = 0
	for i in range(0,N):
		minj = i
		for j in range(i,N):
			if A[minj] > A[j]:
				minj = j
		if i != minj:
			tmp = A[minj]
			A[minj] = A[i]
			A[i] = tmp
			count += 1
	return A,count
ans,c = selectionSort(a,N)
print(*ans)
print(c)

Submission status: Accepted

Solution code:
#coding: utf-8
#ALDS1_2B

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_B

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

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

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_B&lang=jp
sample_input = list(range(3))
sample_input[0] = '''6
5 6 4 2 1 3'''
sample_input[1] = '''6
5 2 4 6 1 3'''
sample_input[2] = ''''''
give_sample_input = None
if give_sample_input is not None:
    sample_input_list = sample_input[give_sample_input].split('\n')
    def input():
        return sample_input_list.pop(0)
        
# main
def swap_list_item(lst, i, j):
    tmp = lst[i]
    lst[i] = lst[j]
    lst[j] = tmp

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

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