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: Runtime Error

Solution code:
n=int(input())
nums=list(map(int,input()))
count=0
for i in range(n):
    minj=i
    for j in range(i,n):
        if nums[j] < nums[minj]:
            minj=j
    nums[i]=v
    nums[i]=nums[minj]
    nums[minj]=v
    count+=1
print(*nums)
print(count)

Submission status: Runtime Error

Solution code:
n=int(input())
nums=list(map(int,input()))
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: Runtime Error

Solution code:
# include <stdio.h>

int main(){
    int N = 0;
    scanf("%d", &N);
    int a[N];
    for(int i=0; i<N; ++i){
        scanf("%d", &a[i]);
    }
    // selection sort
    int swap = 0;
    for(int i=0; i<N-1; ++i){
        int min = i;
        for(int j=i+1; j<N; ++j){
            if(a[j] > a[min]){
                min = j;
            }
        }
        int tmp = a[i];
        a[i] = a[min];
        a[min] = tmp;
        swap++;
    }
    for(int i=0; i<N-1; ++i){
        printf("%d ", a[i]);
    }
    printf("%d\n", a[N-1]);
    printf("%d\n", swap);
}

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

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

swap_cnt = 0
for i in range(0, N):
    minj = i
    for j in range(i + 1, N - 1):
        if arr[j] < arr[minj]
            minj = j
        arr[i], arr[minj] = arr[minj], arr[i]
        swap_cnt += 1 if i != minj else 0

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def ss(n, a):
    count = 0
    for i in range(n):
        mini = i
        for j in range(i,n):
            if a[j] < a[mini]:
                mini = j
        if i != mini:
            a[i], a[mini] = a[mini], a[i]
            count += 1
    print " ".join(map(str, a))
    print count
    
ss(int(raw_input()), map(int, raw_inpu().split()))

Submission status: WA: Presentation Error

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

Submission status: WA: Presentation Error

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

Submission status: WA: Presentation Error

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

Submission status: WA: Presentation Error

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

Submission status: WA: Presentation Error

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

Submission status: WA: Presentation Error

Solution code:
n = int(input())
a = list(map(int,input()))
c = 0
min = 9
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]
        c += 1
    
print(*a)
print(c)

Submission status: WA: Presentation Error

Solution code:
n = int(input())
a = list(map(int,input()))
c = 0
min = 9
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]):
        a[i],a[minj] = a[minj],a[i]
        c += 1
    
print(*a)
print(c)

Submission status: WA: Presentation Error

Solution code:
n = int(input())
a = list(map(int,input()))
c = 0
min = 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]):
        a[i],a[minj] = a[minj],a[i]
        c += 1
    
print(*a)
print(c)

Submission status: WA: Presentation Error

Solution code:
n = int(input())
a = list(map(int,input()))
c = 0
min = 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]
        c += 1
    
print(*a)
print(c)

Submission status: WA: Presentation Error

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

Submission status: WA: Presentation Error

Solution code:
# insertionSort                                                
# divide list into ordered part and part wait to sort          
# select one from part wait to sort and insert into ordered part                                                              
global r, signals                                              
r, signals = 0, False                                          
def insertionSort(A):                                          
    global r, signals                                          
    # Get length of A                                          
    l = len(A)                                                 
    for i in range(l):                                         
        # Selected i-th element to swap from 0 to n            
        v = A[i]                                               
        # j is point to tail of order                          
        j = i - 1                                              
        # Move forward, Control order of sorted                
        while j >= 0 and A[j] > v:                             
            A[j+1] = A[j]                                      
            j -= 1                                             
            signals = True                                     
        A[j+1] = v                                             
        r += 1 if signals else 0                               
        signals = False                                        
    return A                                                   
                                                               
raw_input()                                                    
res, l = "", 0                                                 
for i in insertionSort(map(int, raw_input().split())):         
    res += (" " if l != 0 else "") + str(i)                    
    l += 1                                                     
print res, "\n", r

Submission status: WA: Presentation Error

Solution code:
def selections(N):
    count = 0
    for i in range(len(N)):
        mini = i
        for j in range(i+1, len(N)):
            if N[mini] > N[j]:
                mini = j
        if i != mini:
            N[mini], N[i] = N[i], N[mini]
            count += 1
    for n in N:
        print(str(n)+' ', end='')
    print('')
    print(count)

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

selections(numbers)

Submission status: WA: Presentation Error

Solution code:

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

for i in range(n):
    minij =i
    for j in range(i,n):
        if a[j] < a[minij]:
            minij = j
            a[i],a[minij] = a[minij],a[i]
            count+=1
for i in range(n):
    print("{0} ".format(a[i]),end="")
print()

print(count)

Submission status: WA: Presentation Error

Solution code:

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

for i in range(n):
    minij =i
    for j in range(i,n):
        if a[j] < a[minij]:
            minij = j
            a[i],a[minij] = a[minij],a[i]
            count+=1
for i in range(n):
    print(" {0}".format(a[i]),end="")
print()

print(count)