-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionsort.py
More file actions
96 lines (48 loc) · 1.2 KB
/
selectionsort.py
File metadata and controls
96 lines (48 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Selection Sort
# Implement Selection Sort and print the index which gets swapped at each step.
# Input Format
# The first line of input contains T - the number of test cases. It's followed by 2T lines. The first line of each test case contains N - the size of the array. The next line contains N integers - elements of the array.
# Output Format
# For each test case, print the index which gets swapped at each step, separated by space. Separate the output of different tests by a new line.
# Constraints
# 1 <= T <= 100
# 2 <= N <= 100
# -1000 <= ar[i] <= 1000
# Example
# Input
# 6
# 8
# 176 -272 -272 -45 269 -327 -945 176
# 2
# -274 161
# 7
# 274 204 -161 481 -606 -767 -351
# 2
# 154 -109
# 4
# 5 3 1 5
# 4
# 40 10 20 40
# Output
# 4 0 4 3 1 2 1
# 1
# 3 0 1 2 2 1
# 0
# 0 0 1
# 0 0 0
# Explanation
# Self Explanatory
def selectionsort(ar,n):
for i in range(n-1):
maxi=0
for j in range(1,n-i,1):
if(ar[j]>ar[maxi]):
maxi=j
print(maxi,end=" ")
ar[maxi],ar[n-i-1]=ar[n-i-1],ar[maxi]
print()
k=int(input())
for i in range(k):
n=int(input())
ar=list(map(int,input().split()))[:n]
selectionsort(ar,n)