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())
array = list(map(int,input().split()))
count = 0;

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

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

Submission status: Accepted

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


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


if __name__ == "__main__":
    N = input()
    A = map(int, raw_input().split())
    result, change = selection_sort(A, N)
    print ' '.join(map(str, result))
    print change

Submission status: Accepted

Solution code:
n = int(input())
A = 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:
        (A[i], A[minj]) = (A[minj], A[i])
        count += 1

print(" ".join(A))
print(count)

Submission status: Accepted

Solution code:
def main():
  element_num = int(input())
  elements = [int(s) for s in input().split()]
  swap_count = 0

  for i in range(0, element_num - 1):
    min_val = elements[i + 1]
    min_index = i + 1
    for j in range(i + 1, element_num):
      if elements[j] < min_val:
        min_val = elements[j]
        min_index = j
    if elements[i] > elements[min_index]:
      elements[i], elements[min_index] = elements[min_index], elements[i]
      swap_count += 1

  print(' '.join(list(map(str, elements))))
  print(swap_count)

  return 0


if __name__ == '__main__':
  main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

A ,count = selectionSort(A)
print(' '.join([str(i) for i in A]))
print(count)

Submission status: Accepted

Solution code:
def selection_sort(array):
    count_swap = 0
    for i in range(len(array)):
        minj = i
        for j in range(i + 1,len(array)):
            if array[j] < array[minj]:
                minj = j
        if array[minj] is not array[i]:
            tmp = array[minj]
            array[minj] = array[i]
            array[i] = tmp
            count_swap += 1
    return count_swap


def print_array(array):
     print(str(array)[1:-1].replace(', ', ' '))


def main():
    N = int(input())
    array = [int(s) for s in input().split(' ')]
    count = selection_sort(array)
    print_array(array)
    print(count)

if __name__ == '__main__':
    main()

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


def main():
    n = int(input())
    a = [int(x) for x in input().split(' ')]
    count = selectionSort(a, n)
    print(' '.join([str(x) for x in a]))
    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8

n = int(input())
A = list(map(int, input().split()))
mini = 1
c = 0
for i in range(n):
    mini = i
    for j in range(i, n):
        if A[j] < A[mini]:
            mini = j
    A[i], A[mini] = A[mini], A[i]
    if mini != i:
        c += 1

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

Submission status: Accepted

Solution code:
length = int(input())
targ = [int(n) for n in input().split(' ')]
ans = 0
for l in range(length):
    value = l
    for init in range(l + 1,length):
        if targ[value] > targ[init]:
            value = init
    if value != l:
        disp = targ[l]
        targ[l] = targ[value]
        targ[value] = disp
        ans += 1
print(' '.join([str(n) for n in targ]))
print(ans)

Submission status: Accepted

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

cnt = 0

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

print(' '.join([str(i) for i in A]))
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split(" ")]
swapnum = 0
for i in range(N):
    mini = i
    swap = False
    for j in range(i, N):
        if A[j] < A[mini]:
            mini = j
            swap = True
    if swap:
        tmp = A[i]
        A[i] = A[mini]
        A[mini] = tmp
        swapnum += 1
print(" ".join([str(a) for a in A]))
print(swapnum)

Submission status: Accepted

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

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

def main():
    input_num = int(input())
    input_list = [int(i) for i in input().split()]

    ans_list, count = selection_sort(input_list, input_num)
    for i in range(input_num):
        if i != 0:
            print(" ", end="")
        print(ans_list[i], end='')
    print()
    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

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

count = 0
def swap(a, b):
    t = a
    a = b
    b = t
    return [a, b]

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

l = selection_sort(A)
print(' '.join(str(x) for x in l))
print(count)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int, input().split()))
cnt = 0

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

print(*li)
print(cnt)

Submission status: Accepted

Solution code:
import sys

def selection_sort(nums, n):
    cnt = 0
    
    for i in range(n):
        minj = i
        for j in range(i, n):
            if nums[j] < nums[minj]:
                minj = j

        nums[i], nums[minj] = nums[minj], nums[i]
        if i != minj:
            cnt += 1

    print(' '.join([str(i) for i in nums]))
    print(cnt)
    
def main():
    n = int(sys.stdin.readline().strip())
    data = sys.stdin.readline().strip().split(' ')
    nums = [int(i) for i in data]
    selection_sort(nums, n)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

counter = 0
for i in xrange(N):
  minj = i
  for j in xrange(i, N):
    if A[j] < A[minj]:
      minj = j
  if i != minj:
    tmp = A[i]
    A[i] = A[minj]
    A[minj] = tmp
    counter += 1
print " ".join(map(str, A))
print counter

Submission status: Accepted

Solution code:
length = int(input())
eles = [int(l) for l in input().split()]
times = 0

for i in range(length-1):
    _min = i
    for j in range(i, length):
        if eles[j] < eles[_min]:
            _min = j
    if i != _min:
        eles[i], eles[_min] = eles[_min], eles[i]
        times += 1

print(*eles)
print(times)

Submission status: Accepted

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

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

Submission status: Accepted

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

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


print(" ".join(str(x) for x in m))
print(count)

Submission status: Accepted

Solution code:
#coding:UTF-8
def SS(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:
            count+=1
            swap=A[i]
            A[i]=A[minj]
            A[minj]=swap
    for i in range(len(A)):
        A[i]=str(A[i])
    print(" ".join(A))
    print(count)

if __name__=="__main__":
    N=int(input())
    a=input()
    A=a.split(" ")
    for i in range(len(A)):
        A[i]=int(A[i])
    SS(N,A)

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

print(' '.join([str(A[i]) for i in range(N)]))
print(counter)

Submission status: Accepted

Solution code:
N = int(raw_input())
nums = map(int, raw_input().split(" "))

count = 0
for i in range(0, len(nums)):
    mini = i
    for j in range(i, len(nums)):
        if nums[mini] > nums[j]:
            mini = j
    if i != mini:
        count += 1
    temp = nums[i]
    nums[i] = nums[mini]
    nums[mini] = temp


nums = map(str, nums)
print(" ".join(nums))
print(count)

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

Submission status: Accepted

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


class Solution:
    @staticmethod
    def selection_sort():
        # write your code here
        array_length = int(input())
        unsorted_array = [int(x) for x in input().split()]

        count = 0
        for i in range(array_length):
            min_j = i
            for j in range(i, array_length):
                if unsorted_array[j] < unsorted_array[min_j]:
                    min_j = j
            unsorted_array[i], unsorted_array[min_j] = unsorted_array[min_j], unsorted_array[i]
            if i != min_j:
                count += 1

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


if __name__ == '__main__':
    solution = Solution()
    solution.selection_sort()

Submission status: Accepted

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


def selectionSort(arr, N): 
    counter = 0 
    for i in range(N):
        minj = i 
        for j in range(i,N):
            if arr[minj] > arr[j]:
                minj = j 
        if minj != i: counter += 1
        arr[i], arr[minj] = arr[minj],arr[i]

    return counter,arr

def main():

    N = int(raw_input())
    A = map(int, raw_input().split())
    
    counter,sortedA = selectionSort(A, N)
    print ' '.join(map(str,sortedA))
    print counter

#def test():

if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(*L)
print(p)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_2_B&lang=jp
#???????????????
#?????????????????????????????¨????§???????????????????????¢????????????°????????????
#?¨???????????????°??¢??°???????¢????????????????

def selection_sort(target_list):
    list_length = len(target_list)
    change_count = 0
    for i in range(list_length):
        min_index = i
        for j in range(i, list_length):
            if target_list[j] < target_list[min_index]:
                min_index = j
        else:
            if not i == min_index:
                tmp = target_list[i]
                target_list[i] = target_list[min_index]
                target_list[min_index] = tmp
                change_count += 1
                
    return change_count
def main():
    
    n_list = int(input())
    target_list = [int(n) for n in input().split()]
    cc = selection_sort(target_list)
    print(*target_list)
    print(cc)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

def print_array(A, N):
    for i in xrange(N - 1):
        print str(A[i]),

    print A[N - 1]

def selectionSort(A, N):
    swap_num = 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:
            swap_num += 1
            temp = A[i]
            A[i] = A[minj]
            A[minj] = temp

    return swap_num

def main():
    N = int(raw_input())
    A = []

    for num in raw_input().split():
        A.append(int(num))

    temp = selectionSort(A, N)
    print_array(A, N)

    print temp


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def selection_sort(numbers, n):
    """selection sort method

    Args:
        numbers: a list of numbers to be sorted
        n: len(numbers)

    Returns:
        sorted numberd, number of swapped times
    """
    counter = 0
    for i in range(0, n - 1):
        min_j = i
        for j in range(i + 1, n):
            if numbers[j] < numbers[min_j]:
                min_j = j
        if min_j != i:
            numbers[min_j], numbers[i] = numbers[i], numbers[min_j]
            counter += 1
    return numbers, counter


length = int(raw_input())
numbers = [int(x) for x in raw_input().split()]

numbers, number_of_swapped_times = selection_sort(numbers, length)
print(" ".join([str(x) for x in numbers]))
print(number_of_swapped_times)

Submission status: Accepted

Solution code:
#python2.7

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

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

for m in range(N-1):
	print str(A[m]),

print A[N-1]
print num

Submission status: Accepted

Solution code:
def do_selection_sort(data, size):
    assert size == len(data), "data size error"
    swap_count = 0
    for i in range(size):
        minj = i
        for j in range(i, size):
            if data[j] < data[minj]:
                minj = j
        temp = data[i]
        data[i] = data[minj]
        data[minj] = temp
        if data[i] != data[minj]:
            swap_count += 1
    return swap_count


if __name__ == '__main__':
    # ??????????????\???
    num = int(input())
    data = [int(x) for x in input().split(' ')]
    #data = [5, 6, 4, 2, 1, 3]
    #data = [5, 2, 4, 6, 1, 3]

    # ???????????????
    result = do_selection_sort(data, num)

    # ???????????????
    print('{0}'.format(' '.join(map(str, data))))
    print('{0}'.format(result))

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:
#! /usr/bin/env python
# -*- coding: utf-8 -*-

''' ??????????????? '''

# ????????????????????°?????????N(N-1)/2????????§???
# ?¨???????:O(n^2)


def selection_sort(A, N):
    swap_num = 0  # ???????????°

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

    return (A, swap_num)


if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    # A = [5, 2, 4, 6, 1, 3]
    # N = len(A)
    array_sorted, swap_num = selection_sort(A, N)
    print(' '.join(map(str, array_sorted)))
    print(swap_num)

Submission status: Accepted

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


def selection_sort(n, a):
    cnt = 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[minj]
            a[minj] = a[i]
            a[i] = tmp
            cnt += 1
    return a, cnt


if __name__ == '__main__':
    n = int(input())
    a = [int(n) for n in input().split()]
    ans, cnt = selection_sort(n, a)
    print(' '.join(map(str, ans)))
    print(cnt)

Submission status: Accepted

Solution code:
def selection_sort(seq):
    l = len(seq)
    cnt = 0
    for i in range(l):
        mi = i
        for j in range(i+1, l):
            if seq[j] < seq[mi]:
                mi = j
        if i is not mi:
            seq[i], seq[mi] = seq[mi], seq[i]
            cnt += 1
    return seq, cnt

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

Submission status: Accepted

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

for i in range(N):
  mini = i

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

  if i != mini:
    A[i], A[mini] = A[mini], A[i]
    num_of_swp += 1

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

Submission status: Accepted

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

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

def show_list(list):
    i = 0;
    while(i < len(list)-1):
        print(list[i], end=" ")
        i = i + 1
    print(list[i])

num = int(input())
num_list = [int(x) for i, x in enumerate(input().split()) if i < num]
num_swap, num_list = selection_sort(num_list)
show_list(num_list)
print(num_swap)

Submission status: Accepted

Solution code:

num_length = int(input())
num_array = [int(x) for x in input().split()]


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

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

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

selectionsort(num_array, num_length)

Submission status: Accepted

Solution code:
def select_sort(array):
    N = len(array)
    count = 0
    
    for i in range(N - 1):
        min_i = i + array[i:].index(min(array[i:]))
        if i != min_i:
            count += 1
        array[i], array[min_i] = array[min_i], array[i]
        
    return array, count

n = int(input())
array = [int(i) for i in input().split()]
result = select_sort(array)

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

Submission status: Accepted

Solution code:
cnt, n, a = 0, int(input()), list(map(int, input().split()))
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(*a)
print(cnt)

Submission status: Accepted

Solution code:
def serect_sort(r, n):
    cnt = 0
    for i in range(0, n):
        minj = i
        for j in range(i, n):
            if r[j] < r[minj]:
                minj = j
        r[i],r[minj] = r[minj],r[i]
        if i != minj:
            cnt +=1
    print(*r)
    print(cnt)

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

Submission status: Accepted

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

n = int(input())
str = input()
str = str.split()
a = []
for i in range(n):
    a.append(int(str[i]))

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

    tmp = a[min]
    a[min] = a[i]
    a[i] = tmp
    if a[min] != a[i]:
        count += 1
for i in range(n-1):
    print(a[i],end = ' ')
print(a[n-1])
print(count)

Submission status: Accepted

Solution code:

def SelectionSort(A, N):
    swap_n = 0

    for i in range(N):
        min_j = i

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

        if i != min_j:
            A[i], A[min_j] = A[min_j], A[i]
            swap_n += 1

    return swap_n

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

swap_n = SelectionSort(A, N)

print(*A)
print(swap_n)

Submission status: Accepted

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

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

Submission status: Accepted

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

ans, cnt = selection_sort(int(input()), [int(s) for s in input().split()])
print(' '.join(str(s) for s in ans), cnt, sep='\n')

Submission status: Accepted

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


def selection_sort(seq, l):
    cnt = 0

    for head_i in range(l):
        flag = False

        min_i = head_i
        for target_i in range(head_i+1, l):
            if seq[target_i] < seq[min_i]:
                min_i = target_i
                flag = True
        if flag:
            seq[head_i], seq[min_i] = seq[min_i], seq[head_i]
            cnt += 1

    print(' '.join([str(n) for n in seq]))
    print(cnt)


def to_int(v):
    return int(v)


def to_seq(v):
    return [int(c) for c in v.split()]


if __name__ == '__main__':
    l = to_int(input())
    seq = to_seq(input())

    selection_sort(seq, l)

Submission status: Accepted

Solution code:
def selection(A,n):
  count = 0
  for i in range(0,n-1):
    min = i
    for j in range(i,n):
      if(A[j] < A[min]):
        min = j
    if(min!=i):
      A[i], A[min] = A[min], A[i]
      count+=1
  printList(A)
  print(count)

def printList(A):
  print(" ".join(str(x) for x in A))

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

Submission status: Accepted

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

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


if __name__ == "__main__":
    import os
    import sys
    if len(sys.argv) > 1:
        if sys.argv[1] == "-d":
            fd = os.open("input.txt", os.O_RDONLY)
            os.dup2(fd, sys.stdin.fileno())
            main()
    else:
        main()

Submission status: Accepted

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

Submission status: Accepted

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

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

count = selectionSort(A, N)

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

Submission status: Accepted

Solution code:
def selectionSort():
    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 not(minj == i):
            C += 1
    
    print(*A)
    print(C)
    
selectionSort()

Submission status: Accepted

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

for i in range(len(arrey)):
    minj = i
    change = False
    for j in range(i+1, len(arrey)):
        if arrey[minj] > arrey[j]:
            minj = j
            change = True
    if change:
        arrey[i], arrey[minj] = arrey[minj], arrey[i]
        count += 1

for i in range(len(arrey)):
    print(str(arrey[i])+' ',end='') if i != len(arrey)-1 else print(arrey[i])
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
c=0
a=list(map(int,input().split()))
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:
def selectionSort(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
        if i != mini:
            ret = a[i]
            a[i] = a[mini]
            a[mini] = ret
            count += 1
    print(" ".join(map(str,a)))
    print(count)
n = int(input())
a = list(map(int,input().split()))
selectionSort(a)

Submission status: Accepted

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

counter=0
for i in range(N):
	Ar = A[i:] #i?????????????´???\????????????????????????
	num = Ar.index(min(Ar)) #i?????????????´???\???????????????????????§???????°???????index?????????
	if num != 0:
		A[i], A[(i+num)] = A[(i+num)], A[i]
		counter += 1
print(" ".join(map(str,A)))
print(counter)

Submission status: Accepted

Solution code:
N = int(input())

A = [int(x) for x in input().split()]
num_c = 0
for i in range(0, N):
    minj = i
    for j in range(i, N):
        if A[j] < A[minj]:
            minj = j
    if(i != minj):
        A[i], A[minj] = A[minj], A[i]
        num_c += 1

print((" ").join(str(x) for x in A))
print(num_c)

Submission status: Accepted

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

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

Submission status: Accepted

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

if __name__ == '__main__':
    n = int(input())
    data = [int(i) for i in input().split()]
    result = selection_Sort(data,n)
    print(" ".join(map(str,data)))
    print(result)

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

Submission status: Accepted

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

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

print(' '.join(list(map(str, line))))
print(cnt)

Submission status: Accepted

Solution code:
data_num = int(input())
nums = list(map(int, input().split(" ")))
cnt = 0
for i in range(data_num):
    min_idx = i
    for j in range(i, data_num):
        if nums[j] < nums[min_idx]:
            min_idx = j

    if i != min_idx:
        nums[i], nums[min_idx] = nums[min_idx], nums[i]
        cnt += 1

print(" ".join(map(str,nums)) + "\n" + str(cnt))

Submission status: Accepted

Solution code:
def pretty_sequence(sequence):
    n = len(sequence)
    for i in range(n-1):
        print(format(sequence[i]) + ' ', end='')
    print(sequence[n - 1])

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


def selectionsort(seq, x):
    count = 0
    for i in range(x):
        minj = i
        for j in range(i, x):
            if seq[j] < seq[minj]:
                minj = j
        if seq[i] != seq[minj]:
            tmp_val = seq[i]
            seq[i] = seq[minj]
            seq[minj] = tmp_val
            count += 1
    return seq, count

sequence, count = selectionsort(sequence, n)
pretty_sequence(sequence)
print(count)